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.shared.ldap.model.schema.comparators;
021
022
023import org.apache.directory.shared.ldap.model.entry.StringValue;
024import org.apache.directory.shared.ldap.model.schema.LdapComparator;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028
029/**
030 * A comparator for CSN.
031 *
032 * The CSN are ordered depending on an evaluation of its component, in this order :
033 * - time, 
034 * - changeCount,
035 * - sid
036 * - modifierNumber
037 * 
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class CsnComparator extends LdapComparator<Object>
041{
042    /** The serial version UID */
043    private static final long serialVersionUID = 2L;
044
045    /** A logger for this class */
046    private static final Logger LOG = LoggerFactory.getLogger( CsnComparator.class );
047
048    /**
049     * The CsnComparator constructor. Its OID is the CsnMatch matching
050     * rule OID.
051     */
052    public CsnComparator( String oid )
053    {
054        super( oid );
055    }
056
057
058    /**
059     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
060     */
061    public int compare( Object csnObj1, Object csnObj2 )
062    {
063        LOG.debug( "comparing CSN objects '{}' with '{}'", csnObj1, csnObj2 );
064        
065        if ( csnObj1 == csnObj2 )
066        {
067            return 0;
068        }
069
070        // -------------------------------------------------------------------
071        // Handle some basis cases
072        // -------------------------------------------------------------------
073        if ( csnObj1 == null )
074        {
075            return ( csnObj2 == null ) ? 0 : -1;
076        }
077        
078        if ( csnObj2 == null )
079        {
080            return 1;
081        }
082        
083        String csnStr1 = null;
084        String csnStr2 = null;
085        
086        if ( csnObj1 instanceof StringValue )
087        {
088            csnStr1 = ( ( StringValue ) csnObj1 ).getValue();
089        }
090        else
091        {
092            csnStr1 = csnObj1.toString();
093        }
094
095        if ( csnObj2 instanceof StringValue )
096        {
097            csnStr2 = ( (StringValue) csnObj2 ).getValue();
098        }
099        else
100        {
101            csnStr2 = csnObj2.toString();
102        }
103        
104        return csnStr1.compareTo( csnStr2 );
105    }
106}