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       * @param oid The Comparator's OID
49       */
50      public CsnSidComparator( String oid )
51      {
52          super( oid );
53      }
54  
55  
56      /**
57       * {@inheritDoc}
58       */
59      public int compare( String sidStr1, String sidStr2 )
60      {
61          LOG.debug( "comparing CSN SID objects '{}' with '{}'", sidStr1, sidStr2 );
62  
63          // -------------------------------------------------------------------
64          // Handle some basis cases
65          // -------------------------------------------------------------------
66          if ( sidStr1 == null )
67          {
68              return ( sidStr2 == null ) ? 0 : -1;
69          }
70  
71          if ( sidStr2 == null )
72          {
73              return 1;
74          }
75  
76          int sid1 = 0;
77          int sid2;
78  
79          try
80          {
81              sid1 = Integer.parseInt( sidStr1, 16 );
82          }
83          catch ( NumberFormatException nfe )
84          {
85              return -1;
86          }
87  
88          try
89          {
90              sid2 = Integer.parseInt( sidStr2, 16 );
91          }
92          catch ( NumberFormatException nfe )
93          {
94              return 1;
95          }
96  
97          if ( sid1 > sid2 )
98          {
99              return 1;
100         }
101         else if ( sid2 > sid1 )
102         {
103             return -1;
104         }
105 
106         return 0;
107     }
108 }