Skip to content

Commit

Permalink
Added hashcode and equals to KeyValue
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-schnell committed Feb 25, 2024
1 parent f6387b6 commit 8f0c540
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
55 changes: 34 additions & 21 deletions core/src/main/java/org/fuin/objects4j/core/KeyValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}

Expand All @@ -74,7 +80,7 @@ public final String getKey() {
*
* @return Value.
*/
public final Object getValue() {
public Object getValue() {
return value;
}

Expand All @@ -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{" +
Expand All @@ -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 <code>keyValue</code>
* 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) {
Expand Down Expand Up @@ -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 <code>String</code> - 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 <code>String</code> - Cannot be {@literal null}.
* @return String with replaced variables. Unknown variables will remain unchanged.
*/
public static String replaceVars(final String str, final Map<String, String> vars) {
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/java/org/fuin/objects4j/core/KeyValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 8f0c540

Please sign in to comment.