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       * @param oid The Comparator's OID
54       */
55      public CsnComparator( String oid )
56      {
57          super( oid );
58      }
59  
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public int compare( Object csnObj1, Object csnObj2 )
66      {
67          LOG.debug( "comparing CSN objects '{}' with '{}'", csnObj1, csnObj2 );
68  
69          if ( csnObj1 == csnObj2 )
70          {
71              return 0;
72          }
73  
74          // -------------------------------------------------------------------
75          // Handle some basis cases
76          // -------------------------------------------------------------------
77          if ( csnObj1 == null )
78          {
79              return -1;
80          }
81  
82          if ( csnObj2 == null )
83          {
84              return 1;
85          }
86  
87          String csnStr1;
88          String csnStr2;
89  
90          if ( csnObj1 instanceof StringValue )
91          {
92              csnStr1 = ( ( StringValue ) csnObj1 ).getValue();
93          }
94          else
95          {
96              csnStr1 = csnObj1.toString();
97          }
98  
99          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 }