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