From d60aedb930662ee0815c4b850102cff095fb5517 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 26 Nov 2017 09:33:49 +0000 Subject: [PATCH 1/4] Deprecate TypeAlias.getValue() in favour of map() --- src/main/java/net/kemitix/mon/TypeAlias.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/net/kemitix/mon/TypeAlias.java b/src/main/java/net/kemitix/mon/TypeAlias.java index f5e7636..4027e37 100644 --- a/src/main/java/net/kemitix/mon/TypeAlias.java +++ b/src/main/java/net/kemitix/mon/TypeAlias.java @@ -81,7 +81,9 @@ public abstract class TypeAlias { * Get the value of the type alias. * * @return the value + * @deprecated try using {@link #map(Function)} */ + @Deprecated public T getValue() { return value; } From daa9775d6aacde21c853d523c3f5807e53f93350 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 26 Nov 2017 09:38:35 +0000 Subject: [PATCH 2/4] Update TypeAliasTests to not use deprecated TypeAlias.getValue() --- src/test/java/net/kemitix/mon/TypeAliasTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/net/kemitix/mon/TypeAliasTest.java b/src/test/java/net/kemitix/mon/TypeAliasTest.java index b6fb753..3e04110 100644 --- a/src/test/java/net/kemitix/mon/TypeAliasTest.java +++ b/src/test/java/net/kemitix/mon/TypeAliasTest.java @@ -16,7 +16,7 @@ public class TypeAliasTest { final TypeAlias typeAlias = new TypeAlias(value) { }; //then - assertThat(typeAlias.getValue()).isEqualTo(value); + assertThat(typeAlias.map(value::equals)).isTrue(); } @Test @@ -26,7 +26,7 @@ public class TypeAliasTest { //when final AnAlias anAlias = AnAlias.of(value); //then - assertThat(anAlias.getValue()).isEqualTo(value); + assertThat(anAlias.map(value::equals)).isTrue(); } @Test From 770467c1d5313516df29b0a48fd5fa5e4481a877 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 26 Nov 2017 10:04:03 +0000 Subject: [PATCH 3/4] TypeAlias requires explicit type class as parameter TypeAlias makes use of map() and getValue() internally. --- src/main/java/net/kemitix/mon/TypeAlias.java | 25 +++++++++++---- .../java/net/kemitix/mon/TypeAliasTest.java | 32 +++++++++++++++++-- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/kemitix/mon/TypeAlias.java b/src/main/java/net/kemitix/mon/TypeAlias.java index 4027e37..0aeda37 100644 --- a/src/main/java/net/kemitix/mon/TypeAlias.java +++ b/src/main/java/net/kemitix/mon/TypeAlias.java @@ -38,13 +38,20 @@ public abstract class TypeAlias { */ private final T value; + private final Class type; + /** * Constructor. * * @param value the value + * @param type the type of the value */ - protected TypeAlias(final T value) { + protected TypeAlias( + final T value, + final Class type + ) { this.value = value; + this.type = type; } /** @@ -56,31 +63,37 @@ public abstract class TypeAlias { * @return a TypeAlias */ public final R map(final Function f) { - return f.apply(value); + return f.apply(getValue()); } @Override public final int hashCode() { - return value.hashCode(); + return getValue().hashCode(); } @Override + @SuppressWarnings("unchecked") public final boolean equals(final Object o) { if (o instanceof TypeAlias) { - return value.equals(((TypeAlias) o).value); + if (((TypeAlias) o).type.equals(type)) { + return ((TypeAlias) o).map(getValue()::equals); + } else { + return false; + } } - return value.equals(o); + return map(o::equals); } @Override public final String toString() { - return value.toString(); + return getValue().toString(); } /** * Get the value of the type alias. * * @return the value + * * @deprecated try using {@link #map(Function)} */ @Deprecated diff --git a/src/test/java/net/kemitix/mon/TypeAliasTest.java b/src/test/java/net/kemitix/mon/TypeAliasTest.java index 3e04110..41c4222 100644 --- a/src/test/java/net/kemitix/mon/TypeAliasTest.java +++ b/src/test/java/net/kemitix/mon/TypeAliasTest.java @@ -2,6 +2,7 @@ package net.kemitix.mon; import org.junit.Test; +import java.util.Collections; import java.util.function.Function; import static org.assertj.core.api.Assertions.assertThat; @@ -13,12 +14,28 @@ public class TypeAliasTest { //given final String value = "value"; //when - final TypeAlias typeAlias = new TypeAlias(value) { - }; + final TypeAlias typeAlias = givenTypeAlias(value); //then assertThat(typeAlias.map(value::equals)).isTrue(); } + @Test + public void shouldCreateATypeAliasWithNestedGenericTypes() { + //given + final Iterable iterable = Collections.emptyList(); + //when + final TypeAlias> typeAlias = + new TypeAlias>(iterable, Iterable.class) { + }; + //then + assertThat(typeAlias.map(iterable::equals)).isTrue(); + } + + private TypeAlias givenTypeAlias(final String value) { + return new TypeAlias(value, String.class) { + }; + } + @Test public void shouldCreateAnAliasedTypeAndGetTheValue() throws Exception { //given @@ -29,6 +46,15 @@ public class TypeAliasTest { assertThat(anAlias.map(value::equals)).isTrue(); } + @Test + public void shouldNotBeEqualWhenValueTypesAreDifferent() { + //given + final TypeAlias stringTypeAlias = givenTypeAlias("1"); + final TypeAlias integerTypeAlias = new TypeAlias(1, Integer.class){}; + //then + assertThat(stringTypeAlias).isNotEqualTo(integerTypeAlias); + } + @Test public void shouldBeEqualWhenValuesAreTheSame() throws Exception { //given @@ -86,7 +112,7 @@ public class TypeAliasTest { * @param value the value */ protected AnAlias(final String value) { - super(value); + super(value, String.class); } protected static AnAlias of(final String value) { From 9d31ca2694d72a1c51ab74d52a2c43d2d444a519 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 26 Nov 2017 11:35:25 +0000 Subject: [PATCH 4/4] Make TypeAlias.getValue() private --- src/main/java/net/kemitix/mon/TypeAlias.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/net/kemitix/mon/TypeAlias.java b/src/main/java/net/kemitix/mon/TypeAlias.java index 0aeda37..f5dc0c5 100644 --- a/src/main/java/net/kemitix/mon/TypeAlias.java +++ b/src/main/java/net/kemitix/mon/TypeAlias.java @@ -93,11 +93,8 @@ public abstract class TypeAlias { * Get the value of the type alias. * * @return the value - * - * @deprecated try using {@link #map(Function)} */ - @Deprecated - public T getValue() { + private T getValue() { return value; } }