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     * @param oid The Comparator's OID
054     */
055    public CsnComparator( String oid )
056    {
057        super( oid );
058    }
059
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    public int compare( Object csnObj1, Object csnObj2 )
066    {
067        LOG.debug( "comparing CSN objects '{}' with '{}'", csnObj1, csnObj2 );
068
069        if ( csnObj1 == csnObj2 )
070        {
071            return 0;
072        }
073
074        // -------------------------------------------------------------------
075        // Handle some basis cases
076        // -------------------------------------------------------------------
077        if ( csnObj1 == null )
078        {
079            return -1;
080        }
081
082        if ( csnObj2 == null )
083        {
084            return 1;
085        }
086
087        String csnStr1;
088        String csnStr2;
089
090        if ( csnObj1 instanceof StringValue )
091        {
092            csnStr1 = ( ( StringValue ) csnObj1 ).getValue();
093        }
094        else
095        {
096            csnStr1 = csnObj1.toString();
097        }
098
099        if ( csnObj2 instanceof StringValue )
100        {
101            csnStr2 = ( ( StringValue ) csnObj2 ).getValue();
102        }
103        else
104        {
105            csnStr2 = csnObj2.toString();
106        }
107
108        return csnStr1.compareTo( csnStr2 );
109    }
110}