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