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.schema.LdapComparator;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  
28  /**
29   * A comparator for CSN SID.
30   *
31   * The SID is supposed to be an hexadecimal number between 0x0 and 0xfff
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class CsnSidComparator extends LdapComparator<String>
36  {
37      /** The serial version UID */
38      private static final long serialVersionUID = 2L;
39  
40      /** A logger for this class */
41      private static final Logger LOG = LoggerFactory.getLogger( CsnSidComparator.class );
42  
43  
44      /**
45       * The CsnSidComparator constructor. Its OID is the CsnSidMatch matching
46       * rule OID.
47       */
48      public CsnSidComparator( String oid )
49      {
50          super( oid );
51      }
52  
53  
54      /**
55       * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
56       */
57      public int compare( String sidStr1, String sidStr2 )
58      {
59          LOG.debug( "comparing CSN SID objects '{}' with '{}'", sidStr1, sidStr2 );
60  
61          // -------------------------------------------------------------------
62          // Handle some basis cases
63          // -------------------------------------------------------------------
64          if ( sidStr1 == null )
65          {
66              return ( sidStr2 == null ) ? 0 : -1;
67          }
68  
69          if ( sidStr2 == null )
70          {
71              return 1;
72          }
73  
74          int sid1 = 0;
75          int sid2 = 0;
76  
77          try
78          {
79              sid1 = Integer.parseInt( sidStr1, 16 );
80          }
81          catch ( NumberFormatException nfe )
82          {
83              return -1;
84          }
85  
86          try
87          {
88              sid2 = Integer.parseInt( sidStr2, 16 );
89          }
90          catch ( NumberFormatException nfe )
91          {
92              return 1;
93          }
94  
95          if ( sid1 > sid2 )
96          {
97              return 1;
98          }
99          else if ( sid2 > sid1 )
100         {
101             return -1;
102         }
103 
104         return 0;
105     }
106 }