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   *    http://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.ldap.model.entry.StringValue;
24  import org.apache.directory.api.ldap.model.schema.LdapComparator;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  
29  /**
30   * A comparator for CSN.
31   *
32   * The CSN are ordered depending on an evaluation of its component, in this order :
33   * - time, 
34   * - changeCount,
35   * - sid
36   * - modifierNumber
37   * 
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class CsnComparator extends LdapComparator<Object>
41  {
42      /** The serial version UID */
43      private static final long serialVersionUID = 2L;
44  
45      /** A logger for this class */
46      private static final Logger LOG = LoggerFactory.getLogger( CsnComparator.class );
47  
48  
49      /**
50       * The CsnComparator constructor. Its OID is the CsnMatch matching
51       * rule OID.
52       */
53      public CsnComparator( String oid )
54      {
55          super( oid );
56      }
57  
58  
59      /**
60       * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
61       */
62      public int compare( Object csnObj1, Object csnObj2 )
63      {
64          LOG.debug( "comparing CSN objects '{}' with '{}'", csnObj1, csnObj2 );
65  
66          if ( csnObj1 == csnObj2 )
67          {
68              return 0;
69          }
70  
71          // -------------------------------------------------------------------
72          // Handle some basis cases
73          // -------------------------------------------------------------------
74          if ( csnObj1 == null )
75          {
76              return ( csnObj2 == null ) ? 0 : -1;
77          }
78  
79          if ( csnObj2 == null )
80          {
81              return 1;
82          }
83  
84          String csnStr1 = null;
85          String csnStr2 = null;
86  
87          if ( csnObj1 instanceof StringValue )
88          {
89              csnStr1 = ( ( StringValue ) csnObj1 ).getValue();
90          }
91          else
92          {
93              csnStr1 = csnObj1.toString();
94          }
95  
96          if ( csnObj2 instanceof StringValue )
97          {
98              csnStr2 = ( ( StringValue ) csnObj2 ).getValue();
99          }
100         else
101         {
102             csnStr2 = csnObj2.toString();
103         }
104 
105         return csnStr1.compareTo( csnStr2 );
106     }
107 }