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 java.io.IOException;
24 import java.io.Serializable;
25 import java.math.BigInteger;
26
27 import org.apache.directory.api.i18n.I18n;
28 import org.apache.directory.api.ldap.model.schema.LdapComparator;
29 import org.apache.directory.api.ldap.model.schema.PrepareString;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39 public class IntegerComparator extends LdapComparator<Object> implements Serializable
40 {
41
42 private static final long serialVersionUID = 2L;
43
44
45 private static final Logger LOG = LoggerFactory.getLogger( IntegerComparator.class );
46
47
48
49
50
51
52
53
54 public IntegerComparator( String oid )
55 {
56 super( oid );
57 }
58
59
60
61
62
63 @Override
64 public int compare( Object v1, Object v2 )
65 {
66 if ( v1 instanceof String )
67 {
68 return compare( ( String ) v1, ( String ) v2 );
69 }
70 else
71 {
72 return compare( ( Long ) v1, ( Long ) v2 );
73 }
74 }
75
76
77
78
79
80 private int compare( Long backendValue, Long assertValue )
81 {
82 LOG.debug( "comparing Integer objects '{}' with '{}'", backendValue, assertValue );
83
84
85
86
87 if ( backendValue == assertValue )
88 {
89 return 0;
90 }
91
92
93
94
95 if ( ( backendValue == null ) || ( assertValue == null ) )
96 {
97 return backendValue == null ? -1 : 1;
98 }
99
100 return backendValue.compareTo( assertValue );
101 }
102
103
104
105
106
107 private int compare( String backendValue, String assertValue )
108 {
109 LOG.debug( "comparing Integer objects '{}' with '{}'", backendValue, assertValue );
110
111
112
113
114 if ( backendValue == assertValue )
115 {
116 return 0;
117 }
118
119
120
121
122 if ( ( backendValue == null ) || ( assertValue == null ) )
123 {
124 return backendValue == null ? -1 : 1;
125 }
126
127
128
129 try
130 {
131 backendValue = PrepareString.normalize( backendValue, PrepareString.StringType.NUMERIC_STRING );
132 }
133 catch ( IOException ioe )
134 {
135 throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, backendValue ), ioe );
136 }
137 try
138 {
139 assertValue = PrepareString.normalize( assertValue, PrepareString.StringType.NUMERIC_STRING );
140 }
141 catch ( IOException ioe )
142 {
143 throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, assertValue ), ioe );
144 }
145
146 BigInteger b1 = new BigInteger( backendValue );
147 BigInteger b2 = new BigInteger( assertValue );
148
149 return b1.compareTo( b2 );
150 }
151 }