From 4485fd1f9d4719f2fd754983bb5c3f3f80dd64f2 Mon Sep 17 00:00:00 2001 From: Simon F Date: Sat, 10 Dec 2022 07:52:54 +0000 Subject: [PATCH] minor optimizations and simplifications to JsonNullable (#43) --- .../jackson/nullable/JsonNullable.java | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/openapitools/jackson/nullable/JsonNullable.java b/src/main/java/org/openapitools/jackson/nullable/JsonNullable.java index c3e9ae2..43a521b 100644 --- a/src/main/java/org/openapitools/jackson/nullable/JsonNullable.java +++ b/src/main/java/org/openapitools/jackson/nullable/JsonNullable.java @@ -2,17 +2,18 @@ import java.io.Serializable; import java.util.NoSuchElementException; +import java.util.Objects; import java.util.function.Consumer; public class JsonNullable implements Serializable { private static final long serialVersionUID = 1L; - private static final JsonNullable UNDEFINED = new JsonNullable(null, false); + private static final JsonNullable UNDEFINED = new JsonNullable<>(null, false); - private T value; + private final T value; - private boolean isPresent; + private final boolean isPresent; private JsonNullable(T value, boolean isPresent) { this.value = value; @@ -22,8 +23,8 @@ private JsonNullable(T value, boolean isPresent) { /** * Create a JsonNullable representing an undefined value (not present). * - * @param - * @return + * @param a type wildcard + * @return an empty JsonNullable with no value defined */ public static JsonNullable undefined() { @SuppressWarnings("unchecked") @@ -39,7 +40,7 @@ public static JsonNullable undefined() { * @return the JsonNullable with the submitted value present. */ public static JsonNullable of(T value) { - return new JsonNullable(value, true); + return new JsonNullable<>(value, true); } /** @@ -94,21 +95,13 @@ public boolean equals(Object obj) { } JsonNullable other = (JsonNullable) obj; - return equals(value, other.value) && - equals(isPresent, other.isPresent); - } - - private static boolean equals(Object a, Object b) { - return (a == b) || (a != null && a.equals(b)); + return Objects.equals(value, other.value) && + isPresent == other.isPresent; } @Override public int hashCode() { - int result = 31 + (value == null ? 0 : value.hashCode()); - Boolean bool1 = Boolean.TRUE; - bool1.hashCode(); - result = 31 * result + (isPresent ? 1231 : 1237); - return result; + return Objects.hash(value, isPresent); } @Override