001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.model.schema.comparators;
021
022
023import org.apache.directory.api.i18n.I18n;
024import org.apache.directory.api.ldap.model.entry.Value;
025import org.apache.directory.api.ldap.model.schema.LdapComparator;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029
030/**
031 * A comparator for CSN.
032 *
033 * The CSN are ordered depending on an evaluation of its component, in this order :
034 * - time, 
035 * - changeCount,
036 * - sid
037 * - modifierNumber
038 * 
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041public class CsnComparator extends LdapComparator<Object>
042{
043    /** The serial version UID */
044    private static final long serialVersionUID = 2L;
045
046    /** A logger for this class */
047    private static final Logger LOG = LoggerFactory.getLogger( CsnComparator.class );
048
049
050    /**
051     * The CsnComparator constructor. Its OID is the CsnMatch matching
052     * rule OID.
053     * 
054     * @param oid The Comparator's OID
055     */
056    public CsnComparator( String oid )
057    {
058        super( oid );
059    }
060
061
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    public int compare( Object csnObj1, Object csnObj2 )
067    {
068        if ( LOG.isDebugEnabled() )
069        {
070            LOG.debug( I18n.msg( I18n.MSG_13745_COMPARING_CSN, csnObj1, csnObj2 ) );
071        }
072
073        if ( csnObj1 == csnObj2 )
074        {
075            return 0;
076        }
077
078        // -------------------------------------------------------------------
079        // Handle some basis cases
080        // -------------------------------------------------------------------
081        if ( csnObj1 == null )
082        {
083            return -1;
084        }
085
086        if ( csnObj2 == null )
087        {
088            return 1;
089        }
090
091        String csnStr1;
092        String csnStr2;
093
094        if ( csnObj1 instanceof Value )
095        {
096            csnStr1 = ( ( Value ) csnObj1 ).getString();
097        }
098        else
099        {
100            csnStr1 = csnObj1.toString();
101        }
102
103        if ( csnObj2 instanceof Value )
104        {
105            csnStr2 = ( ( Value ) csnObj2 ).getString();
106        }
107        else
108        {
109            csnStr2 = csnObj2.toString();
110        }
111
112        return csnStr1.compareTo( csnStr2 );
113    }
114}