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 public IntegerComparator( String oid )
53 {
54 super( oid );
55 }
56
57
58
59
60
61 public int compare( Object v1, Object v2 )
62 {
63 if ( v1 instanceof String )
64 {
65 return compare( ( String ) v1, ( String ) v2 );
66 }
67 else
68 {
69 return compare( ( Long ) v1, ( Long ) v2 );
70 }
71 }
72
73
74
75
76
77 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RC_REF_COMPARISON",
78 justification = "false positive")
79 private int compare( Long backendValue, Long assertValue )
80 {
81 LOG.debug( "comparing Integer objects '{}' with '{}'", backendValue, assertValue );
82
83
84
85
86 if ( backendValue == assertValue )
87 {
88 return 0;
89 }
90
91
92
93
94 if ( ( backendValue == null ) || ( assertValue == null ) )
95 {
96 return ( backendValue == null ? -1 : 1 );
97 }
98
99 return backendValue.compareTo( assertValue );
100 }
101
102
103
104
105
106 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "ES_COMPARING_PARAMETER_STRING_WITH_EQ",
107 justification = "false positive")
108 private int compare( String backendValue, String assertValue )
109 {
110 LOG.debug( "comparing Integer objects '{}' with '{}'", backendValue, assertValue );
111
112
113
114
115 if ( backendValue == assertValue )
116 {
117 return 0;
118 }
119
120
121
122
123 if ( ( backendValue == null ) || ( assertValue == null ) )
124 {
125 return ( backendValue == null ? -1 : 1 );
126 }
127
128
129
130 try
131 {
132 backendValue = PrepareString.normalize( backendValue, PrepareString.StringType.NUMERIC_STRING );
133 }
134 catch ( IOException e )
135 {
136 throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, backendValue ) );
137 }
138 try
139 {
140 assertValue = PrepareString.normalize( assertValue, PrepareString.StringType.NUMERIC_STRING );
141 }
142 catch ( IOException e )
143 {
144 throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, assertValue ) );
145 }
146
147 BigInteger b1 = new BigInteger( backendValue );
148 BigInteger b2 = new BigInteger( assertValue );
149
150 return b1.compareTo( b2 );
151 }
152 }