Skip to content

Commit

Permalink
fix: implement toString, equals, hashCode for Key implementation used…
Browse files Browse the repository at this point in the history
… in "of" (#11837) (#11875)

fixes #8474 and #4626

Co-authored-by: Denis <denis@vaadin.com>
  • Loading branch information
vaadin-bot and Denis committed Sep 16, 2021
1 parent 741bc3c commit fce4791
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
39 changes: 37 additions & 2 deletions flow-server/src/main/java/com/vaadin/flow/component/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -2725,8 +2726,42 @@ static Key of(String key, String... additionalKeys) {
List<String> keys = new ArrayList<>(additionalKeys.length + 1);
keys.add(key);
Collections.addAll(keys, additionalKeys);
List<String> unmodifiableKeyList = Collections.unmodifiableList(keys);
return () -> unmodifiableKeyList;
return new Key() {

@Override
public List<String> getKeys() {
return Collections.unmodifiableList(keys);
}

@Override
public String toString() {
List<String> 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());
}
};
}

/**
Expand Down
58 changes: 58 additions & 0 deletions flow-server/src/test/java/com/vaadin/flow/component/KeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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());
}

}

0 comments on commit fce4791

Please sign in to comment.