1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
30
31
32
33 public class BitStringComparator extends LdapComparator<String>
34 {
35
36 private static final long serialVersionUID = 2L;
37
38
39 private static final Logger LOG = LoggerFactory.getLogger( BitStringComparator.class );
40
41
42
43
44
45
46 public BitStringComparator( String oid )
47 {
48 super( oid );
49 }
50
51
52
53
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
62
63
64 if ( bs1 == bs2 )
65 {
66 return 0;
67 }
68
69
70
71
72 if ( ( bs1 == null ) || ( bs2 == null ) )
73 {
74 return ( bs1 == null ? -1 : 1 );
75 }
76
77
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 }