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     */
020    package org.apache.directory.shared.ldap.schema.comparators;
021    
022    
023    import java.util.Comparator;
024    
025    import org.apache.directory.shared.ldap.csn.Csn;
026    
027    
028    /**
029     * A comparator for CSN.
030     *
031     * The CSN are ordered depending on an evaluation of its component, in this order :
032     * - time, 
033     * - changeCount,
034     * - sid
035     * - modifierNumber
036     * 
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev$
039     */
040    public class CsnComparator implements Comparator<String>
041    {
042        /** A static instance of this comparator */
043        public static final Comparator<String> INSTANCE = new CsnComparator();
044        
045        
046        /**
047         * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
048         */
049        public int compare( String csnStr1, String csnStr2 )
050        {
051            // -------------------------------------------------------------------
052            // Handle some basis cases
053            // -------------------------------------------------------------------
054    
055            if ( csnStr1 == null )
056            {
057                return ( csnStr2 == null ) ? 0 : -1;
058            }
059            
060            if ( csnStr2 == null )
061            {
062                return 1;
063            }
064            
065            Csn csn1 = new Csn( csnStr1 );
066            Csn csn2 = new Csn( csnStr2 );
067            
068            if ( csn1.getTimestamp() != csn2.getTimestamp() )
069            {
070                return ( csn1.getTimestamp() < csn2.getTimestamp() ? -1 : 1 );
071            }
072            
073            if ( csn1.getChangeCount() != csn2.getChangeCount() )
074            {
075                return ( csn1.getChangeCount() < csn2.getChangeCount() ? -1 : 1 );
076            }
077            
078            if ( csn1.getReplicaId() != csn2.getReplicaId() )
079            {
080                return ( csn1.getReplicaId() < csn2.getReplicaId() ? -1 : 1 );
081            }
082            
083            if ( csn1.getOperationNumber() != csn2.getOperationNumber() )
084            {
085                return ( csn1.getOperationNumber() < csn2.getOperationNumber() ? -1 : 1 );
086            }
087            
088            return 0;
089        }
090    }