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.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
31
32
33
34 public class BitStringComparator extends LdapComparator<String>
35 {
36
37 private static final long serialVersionUID = 2L;
38
39
40 private static final Logger LOG = LoggerFactory.getLogger( BitStringComparator.class );
41
42
43
44
45
46
47
48
49 public BitStringComparator( String oid )
50 {
51 super( oid );
52 }
53
54
55
56
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
66
67
68 if ( bs1 == bs2 )
69 {
70 return 0;
71 }
72
73
74
75
76 if ( ( bs1 == null ) || ( bs2 == null ) )
77 {
78 return bs1 == null ? -1 : 1;
79 }
80
81
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 }