From 8f0c540ef3ca885798fda24297cdf040fa673893 Mon Sep 17 00:00:00 2001 From: Michael Schnell Date: Sun, 25 Feb 2024 17:16:56 +0100 Subject: [PATCH] Added hashcode and equals to KeyValue --- .../org/fuin/objects4j/core/KeyValue.java | 55 ++++++++++++------- .../org/fuin/objects4j/core/KeyValueTest.java | 17 ++++++ 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/org/fuin/objects4j/core/KeyValue.java b/core/src/main/java/org/fuin/objects4j/core/KeyValue.java index d0dd994..73c6bb8 100644 --- a/core/src/main/java/org/fuin/objects4j/core/KeyValue.java +++ b/core/src/main/java/org/fuin/objects4j/core/KeyValue.java @@ -22,35 +22,41 @@ import org.fuin.objects4j.common.ValueObject; import javax.annotation.concurrent.Immutable; +import java.io.Serial; +import java.io.Serializable; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.Objects; /** - * Container for a key and a value. + * Container for a key and a value. Equals and hashcode are based on the key only. */ @Immutable -public final class KeyValue implements ValueObject { +public final class KeyValue implements ValueObject, Serializable { + + @Serial + private static final long serialVersionUID = 1000L; @TrimmedNotEmpty - private String key; + private final String key; - private Object value; + private final Object value; /** * Protected default constructor for deserialization. */ protected KeyValue() { super(); + this.key = null; + this.value = null; } /** * Constructor with key and value. * - * @param key - * Key. - * @param value - * Value. + * @param key Key. + * @param value Value. */ public KeyValue(@NotNull @TrimmedNotEmpty final String key, final Object value) { super(); @@ -65,7 +71,7 @@ public KeyValue(@NotNull @TrimmedNotEmpty final String key, final Object value) * * @return Key. */ - public final String getKey() { + public String getKey() { return key; } @@ -74,7 +80,7 @@ public final String getKey() { * * @return Value. */ - public final Object getValue() { + public Object getValue() { return value; } @@ -83,13 +89,26 @@ public final Object getValue() { * * @return Value or "null". */ - public final String getValueString() { + public String getValueString() { if (value == null) { return "null"; } return value.toString(); } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + KeyValue keyValue = (KeyValue) o; + return Objects.equals(key, keyValue.key); + } + + @Override + public int hashCode() { + return Objects.hash(key); + } + @Override public String toString() { return "KeyValue{" + @@ -102,11 +121,8 @@ public String toString() { * Replaces all variables in the format "${NAME}" with the corresponding value. NAME is the name of a key from the keyValue * array. * - * @param message - * Message to replace. - * @param keyValue - * Array of key values or {@literal null}. - * + * @param message Message to replace. + * @param keyValue Array of key values or {@literal null}. * @return Replaced message. */ public static String replace(final String message, final KeyValue... keyValue) { @@ -143,11 +159,8 @@ private static String nullSafeAsString(final Object obj) { /** * Replaces all variables inside a string with values from a map. * - * @param str - * Text with variables (Format: ${key} ) - May be {@literal null} or empty. - * @param vars - * Map with key/values (both of type String - Cannot be {@literal null}. - * + * @param str Text with variables (Format: ${key} ) - May be {@literal null} or empty. + * @param vars Map with key/values (both of type String - Cannot be {@literal null}. * @return String with replaced variables. Unknown variables will remain unchanged. */ public static String replaceVars(final String str, final Map vars) { diff --git a/core/src/test/java/org/fuin/objects4j/core/KeyValueTest.java b/core/src/test/java/org/fuin/objects4j/core/KeyValueTest.java index f443b12..e83228e 100644 --- a/core/src/test/java/org/fuin/objects4j/core/KeyValueTest.java +++ b/core/src/test/java/org/fuin/objects4j/core/KeyValueTest.java @@ -17,7 +17,9 @@ */ package org.fuin.objects4j.core; +import nl.jqno.equalsverifier.EqualsVerifier; import org.fuin.objects4j.common.ConstraintViolationException; +import org.fuin.utils4j.Utils4J; import org.junit.jupiter.api.Test; import java.util.ArrayList; @@ -54,6 +56,21 @@ void testCreateInvalidKey() { } + @Test + void testEqualHashCode() { + EqualsVerifier.forClass(KeyValue.class).withIgnoredFields("value").verify(); + } + + @Test + void testSerializeDeserialize() { + final KeyValue original = new KeyValue("one", 1); + final KeyValue copy = Utils4J.deserialize(Utils4J.serialize(original)); + assertThat(copy).isEqualTo(original); + assertThat(copy.getKey()).isEqualTo(original.getKey()); + assertThat(copy.getValue()).isEqualTo(original.getValue()); + assertThat(copy.getValueString()).isEqualTo("1"); + } + @Test void testReplaceVarsNull() { assertThat(KeyValue.replace(null)).isNull();