Skip to content

Commit 9f86a61

Browse files
authored
Use Objects.equals() now that it is available (Java 8) (#303)
1 parent 03c8eef commit 9f86a61

File tree

5 files changed

+18
-44
lines changed

5 files changed

+18
-44
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ NOTE: Jackson 3.x components rely on 2.x annotations; there are no separate
1414
=== Releases ===
1515
------------------------------------------------------------------------
1616

17+
- Use `Objects.equals()` for comparison (with Java 8 baseline)
18+
1719
2.20-rc1 (04-Aug-2025)
1820

1921
#293: Improve duplicate Id with different associated object error message

src/main/java/com/fasterxml/jackson/annotation/JacksonInject.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.lang.annotation.Retention;
55
import java.lang.annotation.RetentionPolicy;
66
import java.lang.annotation.Target;
7+
import java.util.Objects;
78

89
/**
910
* Jackson-specific annotation used for indicating that value of
@@ -226,12 +227,9 @@ public boolean equals(Object o) {
226227
if (o.getClass() == getClass()) {
227228
Value other = (Value) o;
228229

229-
return (_id == null && other._id == null
230-
|| _id != null && _id.equals(other._id))
231-
&& (_useInput == null && other._useInput == null
232-
|| _useInput != null && _useInput.equals(other._useInput))
233-
&& (_optional == null && other._optional == null
234-
|| _optional != null && _optional.equals(other._optional));
230+
return Objects.equals(_id, other._id)
231+
&& Objects.equals(_useInput, other._useInput)
232+
&& Objects.equals(_optional, other._optional);
235233
}
236234
return false;
237235
}
@@ -243,7 +241,7 @@ public boolean equals(Object o) {
243241
*/
244242

245243
private static boolean _empty(Object id, Boolean useInput, Boolean optional) {
246-
return (id == null) && (useInput == null) && optional == null;
244+
return (id == null) && (useInput == null) && (optional == null);
247245
}
248246
}
249247
}

src/main/java/com/fasterxml/jackson/annotation/JsonFormat.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.lang.annotation.*;
44
import java.util.Locale;
5+
import java.util.Objects;
56
import java.util.TimeZone;
67

78
/**
@@ -900,22 +901,11 @@ public boolean equals(Object o) {
900901
|| !_features.equals(other._features)) {
901902
return false;
902903
}
903-
return _equal(_lenient, other._lenient)
904-
&& _equal(_timezoneStr, other._timezoneStr)
905-
&& _equal(_pattern, other._pattern)
906-
&& _equal(_timezone, other._timezone)
907-
&& _equal(_locale, other._locale);
908-
}
909-
910-
private static <T> boolean _equal(T value1, T value2)
911-
{
912-
if (value1 == null) {
913-
return (value2 == null);
914-
}
915-
if (value2 == null) {
916-
return false;
917-
}
918-
return value1.equals(value2);
904+
return Objects.equals(_lenient, other._lenient)
905+
&& Objects.equals(_timezoneStr, other._timezoneStr)
906+
&& Objects.equals(_pattern, other._pattern)
907+
&& Objects.equals(_timezone, other._timezone)
908+
&& Objects.equals(_locale, other._locale);
919909
}
920910
}
921911
}

src/main/java/com/fasterxml/jackson/annotation/JsonIncludeProperties.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,8 @@ public int hashCode() {
139139
public boolean equals(Object o) {
140140
if (o == this) return true;
141141
if (o == null) return false;
142-
return (o.getClass() == getClass()) && _equals(_included, ((Value) o)._included);
143-
}
144-
145-
private static boolean _equals(Set<String> a, Set<String> b)
146-
{
147-
return a == null ? (b == null)
148-
// keep this last just because it can be expensive
149-
: a.equals(b);
142+
return (o.getClass() == getClass())
143+
&& Objects.equals(_included, ((Value) o)._included);
150144
}
151145

152146
private static Set<String> _asSet(String[] v)

src/main/java/com/fasterxml/jackson/annotation/JsonTypeInfo.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.lang.annotation.Retention;
55
import java.lang.annotation.RetentionPolicy;
66
import java.lang.annotation.Target;
7+
import java.util.Objects;
78

89
/**
910
* Annotation used for configuring details of if and how type information is
@@ -536,20 +537,9 @@ private static boolean _equals(Value a, Value b)
536537
&& (a._inclusionType == b._inclusionType)
537538
&& (a._defaultImpl == b._defaultImpl)
538539
&& (a._idVisible == b._idVisible)
539-
&& _equal(a._propertyName, b._propertyName)
540-
&& _equal(a._requireTypeIdForSubtypes, b._requireTypeIdForSubtypes)
540+
&& Objects.equals(a._propertyName, b._propertyName)
541+
&& Objects.equals(a._requireTypeIdForSubtypes, b._requireTypeIdForSubtypes)
541542
;
542543
}
543-
544-
private static <T> boolean _equal(T value1, T value2)
545-
{
546-
if (value1 == null) {
547-
return (value2 == null);
548-
}
549-
if (value2 == null) {
550-
return false;
551-
}
552-
return value1.equals(value2);
553-
}
554544
}
555545
}

0 commit comments

Comments
 (0)