diff --git a/flow-server/src/main/java/com/vaadin/flow/component/Key.java b/flow-server/src/main/java/com/vaadin/flow/component/Key.java index c0c79531d08..48f4e3a3c6f 100644 --- a/flow-server/src/main/java/com/vaadin/flow/component/Key.java +++ b/flow-server/src/main/java/com/vaadin/flow/component/Key.java @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; import java.util.stream.Stream; /** @@ -2725,8 +2726,42 @@ static Key of(String key, String... additionalKeys) { List keys = new ArrayList<>(additionalKeys.length + 1); keys.add(key); Collections.addAll(keys, additionalKeys); - List unmodifiableKeyList = Collections.unmodifiableList(keys); - return () -> unmodifiableKeyList; + return new Key() { + + @Override + public List getKeys() { + return Collections.unmodifiableList(keys); + } + + @Override + public String toString() { + List keys = getKeys(); + if (keys.size() == 1) { + return keys.get(0); + } + return keys.get(0) + ", additional keys : [" + keys.stream() + .skip(1).collect(Collectors.joining(", ")) + "]"; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj == this) { + return true; + } + if (!obj.getClass().equals(getClass())) { + return false; + } + return getKeys().equals(((Key) obj).getKeys()); + } + + @Override + public int hashCode() { + return Objects.hash(getKeys()); + } + }; } /** diff --git a/flow-server/src/test/java/com/vaadin/flow/component/KeyTest.java b/flow-server/src/test/java/com/vaadin/flow/component/KeyTest.java index c1771b42de8..b852d1483ad 100644 --- a/flow-server/src/test/java/com/vaadin/flow/component/KeyTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/component/KeyTest.java @@ -15,6 +15,8 @@ */ package com.vaadin.flow.component; +import java.util.Collections; +import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import org.junit.Assert; @@ -47,4 +49,60 @@ public void listenerWithMultipleKeyValues() { Assert.assertFalse(fired.get()); } + @Test + public void of_toString_returnsKeys() { + Key key = Key.of("foo"); + + Assert.assertEquals("foo", key.toString()); + + key = Key.of("foo", "bar"); + + Assert.assertEquals("foo, additional keys : [bar]", key.toString()); + } + + @Test + public void of_equals_stringRepresentationsEqual_toKeysAreEqual() { + Key key1 = Key.of("foo"); + Key key2 = Key.of("foo"); + + Assert.assertEquals(key1, key2); + } + + @Test + public void of_equals_stringRepresentationsNotEqual_toKeysAreNotEqual() { + Key key1 = Key.of("foo"); + Key key2 = Key.of("bar"); + + Assert.assertNotEquals(key1, key2); + } + + @Test + public void of_equals_secondKeyHasAdditionalKeys_toKeysAreNotEqual() { + Key key1 = Key.of("foo"); + Key key2 = Key.of("foo", "bar"); + + Assert.assertNotEquals(key1, key2); + } + + @Test + public void of_equals_differentClasses_toKeysAreNotEqual() { + Key key1 = Key.of("foo"); + Key key2 = new Key() { + + @Override + public List getKeys() { + return Collections.singletonList("foo"); + } + }; + + Assert.assertNotEquals(key1, key2); + } + + @Test + public void of_equalKeys_hasSameHashCode() { + Key key1 = Key.of("foo", "bar"); + Key key2 = Key.of("foo", "bar"); + Assert.assertEquals(key1.hashCode(), key2.hashCode()); + } + }