View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.comparators;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.model.entry.Value;
25  import org.apache.directory.api.ldap.model.schema.LdapComparator;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  
30  /**
31   * A comparator for CSN.
32   *
33   * The CSN are ordered depending on an evaluation of its component, in this order :
34   * - time, 
35   * - changeCount,
36   * - sid
37   * - modifierNumber
38   * 
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public class CsnComparator extends LdapComparator<Object>
42  {
43      /** The serial version UID */
44      private static final long serialVersionUID = 2L;
45  
46      /** A logger for this class */
47      private static final Logger LOG = LoggerFactory.getLogger( CsnComparator.class );
48  
49  
50      /**
51       * The CsnComparator constructor. Its OID is the CsnMatch matching
52       * rule OID.
53       * 
54       * @param oid The Comparator's OID
55       */
56      public CsnComparator( String oid )
57      {
58          super( oid );
59      }
60  
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public int compare( Object csnObj1, Object csnObj2 )
67      {
68          if ( LOG.isDebugEnabled() )
69          {
70              LOG.debug( I18n.msg( I18n.MSG_13745_COMPARING_CSN, csnObj1, csnObj2 ) );
71          }
72  
73          if ( csnObj1 == csnObj2 )
74          {
75              return 0;
76          }
77  
78          // -------------------------------------------------------------------
79          // Handle some basis cases
80          // -------------------------------------------------------------------
81          if ( csnObj1 == null )
82          {
83              return -1;
84          }
85  
86          if ( csnObj2 == null )
87          {
88              return 1;
89          }
90  
91          String csnStr1;
92          String csnStr2;
93  
94          if ( csnObj1 instanceof Value )
95          {
96              csnStr1 = ( ( Value ) csnObj1 ).getString();
97          }
98          else
99          {
100             csnStr1 = csnObj1.toString();
101         }
102 
103         if ( csnObj2 instanceof Value )
104         {
105             csnStr2 = ( ( Value ) csnObj2 ).getString();
106         }
107         else
108         {
109             csnStr2 = csnObj2.toString();
110         }
111 
112         return csnStr1.compareTo( csnStr2 );
113     }
114 }