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 org.apache.directory.shared.ldap.csn.Csn;
024    import org.apache.directory.shared.ldap.schema.LdapComparator;
025    import org.slf4j.Logger;
026    import 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     * @version $Rev$
040     */
041    public class CsnComparator extends LdapComparator<String>
042    {
043        /** A logger for this class */
044        private static final Logger LOG = LoggerFactory.getLogger( CsnComparator.class );
045    
046        /** The serialVersionUID */
047        private static final long serialVersionUID = 1L;
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( String csnStr1, String csnStr2 )
063        {
064            LOG.debug( "comparing CSN objects '{}' with '{}'", csnStr1, csnStr2 );
065    
066            // -------------------------------------------------------------------
067            // Handle some basis cases
068            // -------------------------------------------------------------------
069            if ( csnStr1 == null )
070            {
071                return ( csnStr2 == null ) ? 0 : -1;
072            }
073            
074            if ( csnStr2 == null )
075            {
076                return 1;
077            }
078            
079            Csn csn1 = new Csn( csnStr1 );
080            Csn csn2 = new Csn( csnStr2 );
081            
082            if ( csn1.getTimestamp() != csn2.getTimestamp() )
083            {
084                return ( csn1.getTimestamp() < csn2.getTimestamp() ? -1 : 1 );
085            }
086            
087            if ( csn1.getChangeCount() != csn2.getChangeCount() )
088            {
089                return ( csn1.getChangeCount() < csn2.getChangeCount() ? -1 : 1 );
090            }
091            
092            if ( csn1.getReplicaId() != csn2.getReplicaId() )
093            {
094                return ( csn1.getReplicaId() < csn2.getReplicaId() ? -1 : 1 );
095            }
096            
097            if ( csn1.getOperationNumber() != csn2.getOperationNumber() )
098            {
099                return ( csn1.getOperationNumber() < csn2.getOperationNumber() ? -1 : 1 );
100            }
101            
102            return 0;
103        }
104    }