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.ldap.model.entry.StringValue;
024import org.apache.directory.api.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    /**
050     * The CsnComparator constructor. Its OID is the CsnMatch matching
051     * rule OID.
052     */
053    public CsnComparator( String oid )
054    {
055        super( oid );
056    }
057
058
059    /**
060     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
061     */
062    public int compare( Object csnObj1, Object csnObj2 )
063    {
064        LOG.debug( "comparing CSN objects '{}' with '{}'", csnObj1, csnObj2 );
065
066        if ( csnObj1 == csnObj2 )
067        {
068            return 0;
069        }
070
071        // -------------------------------------------------------------------
072        // Handle some basis cases
073        // -------------------------------------------------------------------
074        if ( csnObj1 == null )
075        {
076            return ( csnObj2 == null ) ? 0 : -1;
077        }
078
079        if ( csnObj2 == null )
080        {
081            return 1;
082        }
083
084        String csnStr1 = null;
085        String csnStr2 = null;
086
087        if ( csnObj1 instanceof StringValue )
088        {
089            csnStr1 = ( ( StringValue ) csnObj1 ).getValue();
090        }
091        else
092        {
093            csnStr1 = csnObj1.toString();
094        }
095
096        if ( csnObj2 instanceof StringValue )
097        {
098            csnStr2 = ( ( StringValue ) csnObj2 ).getValue();
099        }
100        else
101        {
102            csnStr2 = csnObj2.toString();
103        }
104
105        return csnStr1.compareTo( csnStr2 );
106    }
107}