TypeAlias requires explicit type class as parameter

TypeAlias makes use of map() and getValue() internally.
This commit is contained in:
Paul Campbell 2017-11-26 10:04:03 +00:00
parent daa9775d6a
commit 770467c1d5
2 changed files with 48 additions and 9 deletions

View file

@ -38,13 +38,20 @@ public abstract class TypeAlias<T> {
*/ */
private final T value; private final T value;
private final Class<? super T> type;
/** /**
* Constructor. * Constructor.
* *
* @param value the value * @param value the value
* @param type the type of the value
*/ */
protected TypeAlias(final T value) { protected TypeAlias(
final T value,
final Class<? super T> type
) {
this.value = value; this.value = value;
this.type = type;
} }
/** /**
@ -56,31 +63,37 @@ public abstract class TypeAlias<T> {
* @return a TypeAlias * @return a TypeAlias
*/ */
public final <R> R map(final Function<T, R> f) { public final <R> R map(final Function<T, R> f) {
return f.apply(value); return f.apply(getValue());
} }
@Override @Override
public final int hashCode() { public final int hashCode() {
return value.hashCode(); return getValue().hashCode();
} }
@Override @Override
@SuppressWarnings("unchecked")
public final boolean equals(final Object o) { public final boolean equals(final Object o) {
if (o instanceof TypeAlias) { if (o instanceof TypeAlias) {
return value.equals(((TypeAlias) o).value); if (((TypeAlias) o).type.equals(type)) {
return ((TypeAlias<T>) o).map(getValue()::equals);
} else {
return false;
} }
return value.equals(o); }
return map(o::equals);
} }
@Override @Override
public final String toString() { public final String toString() {
return value.toString(); return getValue().toString();
} }
/** /**
* Get the value of the type alias. * Get the value of the type alias.
* *
* @return the value * @return the value
*
* @deprecated try using {@link #map(Function)} * @deprecated try using {@link #map(Function)}
*/ */
@Deprecated @Deprecated

View file

@ -2,6 +2,7 @@ package net.kemitix.mon;
import org.junit.Test; import org.junit.Test;
import java.util.Collections;
import java.util.function.Function; import java.util.function.Function;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -13,12 +14,28 @@ public class TypeAliasTest {
//given //given
final String value = "value"; final String value = "value";
//when //when
final TypeAlias<String> typeAlias = new TypeAlias<String>(value) { final TypeAlias<String> typeAlias = givenTypeAlias(value);
};
//then //then
assertThat(typeAlias.<Boolean>map(value::equals)).isTrue(); assertThat(typeAlias.<Boolean>map(value::equals)).isTrue();
} }
@Test
public void shouldCreateATypeAliasWithNestedGenericTypes() {
//given
final Iterable<String> iterable = Collections.emptyList();
//when
final TypeAlias<Iterable<String>> typeAlias =
new TypeAlias<Iterable<String>>(iterable, Iterable.class) {
};
//then
assertThat(typeAlias.<Boolean>map(iterable::equals)).isTrue();
}
private TypeAlias<String> givenTypeAlias(final String value) {
return new TypeAlias<String>(value, String.class) {
};
}
@Test @Test
public void shouldCreateAnAliasedTypeAndGetTheValue() throws Exception { public void shouldCreateAnAliasedTypeAndGetTheValue() throws Exception {
//given //given
@ -29,6 +46,15 @@ public class TypeAliasTest {
assertThat(anAlias.<Boolean>map(value::equals)).isTrue(); assertThat(anAlias.<Boolean>map(value::equals)).isTrue();
} }
@Test
public void shouldNotBeEqualWhenValueTypesAreDifferent() {
//given
final TypeAlias<String> stringTypeAlias = givenTypeAlias("1");
final TypeAlias<Integer> integerTypeAlias = new TypeAlias<Integer>(1, Integer.class){};
//then
assertThat(stringTypeAlias).isNotEqualTo(integerTypeAlias);
}
@Test @Test
public void shouldBeEqualWhenValuesAreTheSame() throws Exception { public void shouldBeEqualWhenValuesAreTheSame() throws Exception {
//given //given
@ -86,7 +112,7 @@ public class TypeAliasTest {
* @param value the value * @param value the value
*/ */
protected AnAlias(final String value) { protected AnAlias(final String value) {
super(value); super(value, String.class);
} }
protected static AnAlias of(final String value) { protected static AnAlias of(final String value) {