@@ -1158,6 +1158,7 @@ public String toString() {
1158
1158
return encode ();
1159
1159
}
1160
1160
1161
+
1161
1162
@ Override
1162
1163
public boolean equals (Object o ) {
1163
1164
// null check
@@ -1182,72 +1183,58 @@ public boolean equals(Object o) {
1182
1183
1183
1184
Object thisValue = this .getValue (key );
1184
1185
Object otherValue = other .getValue (key );
1185
- // identity check
1186
- if (thisValue == otherValue ) {
1187
- continue ;
1186
+ if (thisValue != otherValue && !compareObjects (thisValue , otherValue )) {
1187
+ return false ;
1188
1188
}
1189
- // special case for numbers
1190
-
1191
- if (thisValue instanceof Number && otherValue instanceof Number ) {
1192
- if (thisValue .getClass () == otherValue .getClass ()) {
1193
- if (thisValue .equals (otherValue )) {
1194
- continue ;
1195
- }
1196
- } else {
1197
- // meaning that the numbers are different types
1198
- Number n1 = (Number ) thisValue ;
1199
- Number n2 = (Number ) otherValue ;
1200
- if ((thisValue instanceof Float || thisValue instanceof Double ) &&
1201
- (otherValue instanceof Float || otherValue instanceof Double )) {
1202
- // compare as floating point double
1203
- if (n1 .doubleValue () == n2 .doubleValue ()) {
1204
- // same value, check the next entry
1205
- continue ;
1206
- }
1207
- }
1208
-
1209
- if ((thisValue instanceof Integer || thisValue instanceof Long ) &&
1210
- (otherValue instanceof Integer || otherValue instanceof Long )) {
1211
- // compare as integer long
1212
- if (n1 .longValue () == n2 .longValue ()) {
1213
- // same value, check the next entry
1214
- continue ;
1215
- }
1216
- }
1217
-
1189
+ }
1190
+ // all checks passed
1191
+ return true ;
1192
+ }
1218
1193
1219
- // if its either integer or long and the other is float or double or vice versa,
1220
- // compare as floating point double
1221
- if ((thisValue instanceof Integer || thisValue instanceof Long ) &&
1222
- (otherValue instanceof Float || otherValue instanceof Double ) ||
1223
- (thisValue instanceof Float || thisValue instanceof Double ) &&
1224
- (otherValue instanceof Integer || otherValue instanceof Long )) {
1225
- // compare as floating point double
1226
- if (n1 .doubleValue () == n2 .doubleValue ()) {
1227
- // same value, check the next entry
1228
- continue ;
1229
- }
1230
- }
1231
- }
1194
+ static boolean compareObjects (Object o1 , Object o2 ) {
1195
+ if (o1 instanceof Number && o2 instanceof Number ) {
1196
+ if (o1 .getClass () == o2 .getClass ()) {
1197
+ return o1 .equals (o2 );
1198
+ } else {
1199
+ // meaning that the numbers are different types
1200
+ Number n1 = (Number ) o1 ;
1201
+ Number n2 = (Number ) o2 ;
1202
+ return compareNumbers (n1 , n2 );
1232
1203
}
1204
+ } else if (o1 instanceof CharSequence && o2 instanceof CharSequence && o1 .getClass () != o2 .getClass ()) {
1205
+ return Objects .equals (o1 .toString (), o2 .toString ());
1206
+ } else {
1207
+ return Objects .equals (o1 , o2 );
1208
+ }
1209
+ }
1233
1210
1234
- // special case for char sequences
1235
- if (thisValue instanceof CharSequence && otherValue instanceof CharSequence && thisValue .getClass () != otherValue .getClass ()) {
1236
- CharSequence s1 = (CharSequence ) thisValue ;
1237
- CharSequence s2 = (CharSequence ) otherValue ;
1238
-
1239
- if (Objects .equals (s1 .toString (), s2 .toString ())) {
1240
- // same value, check the next entry
1241
- continue ;
1242
- }
1243
- }
1244
- // fallback to standard object equals checks
1245
- if (!Objects .equals (thisValue , otherValue )) {
1246
- return false ;
1211
+ private static boolean compareNumbers (Number n1 , Number n2 ) {
1212
+ if (isDecimalNumber (n1 ) && isDecimalNumber (n2 )) {
1213
+ // compare as floating point double
1214
+ return n1 .doubleValue () == n2 .doubleValue ();
1215
+ } else if (isWholeNumber (n1 ) && isWholeNumber (n2 )) {
1216
+ // compare as integer long
1217
+ return n1 .longValue () == n2 .longValue ();
1218
+ } else if (isWholeNumber (n1 ) && isDecimalNumber (n2 ) ||
1219
+ isDecimalNumber (n1 ) && isWholeNumber (n2 )) {
1220
+ // if its either integer or long and the other is float or double or vice versa,
1221
+ // compare as floating point double
1222
+ return n1 .doubleValue () == n2 .doubleValue ();
1223
+ } else {
1224
+ if (isWholeNumber (n1 )) {
1225
+ return n1 .longValue () == n2 .longValue ();
1226
+ } else {
1227
+ return n1 .doubleValue () == n2 .doubleValue ();
1247
1228
}
1248
1229
}
1249
- // all checks passed
1250
- return true ;
1230
+ }
1231
+
1232
+ private static boolean isWholeNumber (Number thisValue ) {
1233
+ return thisValue instanceof Integer || thisValue instanceof Long ;
1234
+ }
1235
+
1236
+ private static boolean isDecimalNumber (Number thisValue ) {
1237
+ return thisValue instanceof Float || thisValue instanceof Double ;
1251
1238
}
1252
1239
1253
1240
@ Override
0 commit comments