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 class for the bitStringMatch matchingRule (RFC 4517, par. 4.2.1)
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  public class BitStringComparator extends LdapComparator<String>
35  {
36      /** The serial version UID */
37      private static final long serialVersionUID = 2L;
38  
39      /** A logger for this class */
40      private static final Logger LOG = LoggerFactory.getLogger( BitStringComparator.class );
41  
42  
43      /**
44       * The BitStringComparator constructor. Its OID is the IntegerOrderingMatch matching
45       * rule OID.
46       * 
47       * @param oid The Comparator's OID
48       */
49      public BitStringComparator( String oid )
50      {
51          super( oid );
52      }
53  
54  
55      /**
56       * {@inheritDoc}
57       */
58      public int compare( String bs1, String bs2 )
59      {
60          if ( LOG.isDebugEnabled() )
61          {
62              LOG.debug( I18n.msg( I18n.MSG_13743_COMPARING_BITSTRING, bs1, bs2 ) );
63          }
64  
65          // First, shortcut the process by comparing
66          // references. If they are equals, then bs1 and bs2
67          // reference the same object
68          if ( bs1 == bs2 )
69          {
70              return 0;
71          }
72  
73          // Then, deal with one of bs1 or bs2 being null
74          // Both can't be null, because then they would
75          // have been caught by the previous test
76          if ( ( bs1 == null ) || ( bs2 == null ) )
77          {
78              return bs1 == null ? -1 : 1;
79          }
80  
81          // We have to get rid of 0 from left of each BitString
82          char[] array1 = bs1.toCharArray();
83          char[] array2 = bs2.toCharArray();
84  
85          int pos1 = bs1.indexOf( '1' );
86          int pos2 = bs2.indexOf( '1' );
87  
88          if ( pos1 == -1 )
89          {
90              if ( pos2 == -1 )
91              {
92                  return 0;
93              }
94              else
95              {
96                  return -1;
97              }
98          }
99          else if ( pos2 == -1 )
100         {
101             return 1;
102         }
103 
104         int length1 = array1.length - pos1;
105         int length2 = array2.length - pos2;
106 
107         if ( length1 == length2 )
108         {
109             for ( int i = 0; i < length1; i++ )
110             {
111                 int i1 = i + pos1;
112                 int i2 = i + pos2;
113 
114                 if ( array1[i1] < array2[i2] )
115                 {
116                     return -1;
117                 }
118                 else if ( array1[i1] > array2[i2] )
119                 {
120                     return 1;
121                 }
122             }
123 
124             return 0;
125         }
126 
127         if ( length1 < length2 )
128         {
129             return -1;
130         }
131         else
132         {
133             return 1;
134         }
135     }
136 }