Merge pull request #11 from kemitix/non-monad-typealias

Restore public access to TypeAlias.getValue()
This commit is contained in:
Paul Campbell 2017-12-09 19:29:05 +00:00 committed by GitHub
commit 283e7a2f46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 40 deletions

View file

@ -4,7 +4,7 @@ CHANGELOG
0.4.0 0.4.0
----- -----
* * Restore public access for `TypeAlias.getValue()`
0.3.0 0.3.0
----- -----

View file

@ -26,6 +26,9 @@ import java.util.function.Function;
/** /**
* Type Alias for other types. * Type Alias for other types.
* *
* <p>{@link #toString()}, {@link #equals(Object)} and {@link #hashCode()} are all transparent, returning the value
* for the type being aliased.</p>
*
* @param <T> the type of the alias * @param <T> the type of the alias
* *
* @author Paul Campbell (pcampbell@kemitix.net) * @author Paul Campbell (pcampbell@kemitix.net)
@ -38,20 +41,13 @@ 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( protected TypeAlias(final T value) {
final T value,
final Class<? super T> type
) {
this.value = value; this.value = value;
this.type = type;
} }
/** /**
@ -72,14 +68,13 @@ public abstract class TypeAlias<T> {
} }
@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) {
if (((TypeAlias) o).type.equals(type)) { final TypeAlias other = (TypeAlias) o;
return ((TypeAlias<T>) o).map(getValue()::equals); final Object otherValue = other.getValue();
} else { final Class<?> otherValueClass = otherValue.getClass();
return false; return otherValueClass.equals(getValue().getClass())
} && otherValue.equals(getValue());
} }
return map(o::equals); return map(o::equals);
} }
@ -94,7 +89,7 @@ public abstract class TypeAlias<T> {
* *
* @return the value * @return the value
*/ */
private T getValue() { public T getValue() {
return value; return value;
} }
} }

View file

@ -1,22 +1,29 @@
package net.kemitix.mon; package net.kemitix.mon;
import org.assertj.core.util.Strings;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.function.Function; import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public class TypeAliasTest { public class TypeAliasTest {
@Test @Test
public void shouldCreateATypeAliasAndGetTheValue() throws Exception { public void shouldCreateATypeAliasAndGetTheValue() {
//given //given
final String value = "value"; final String value = "value";
//when //when
final TypeAlias<String> typeAlias = givenTypeAlias(value); final TypeAlias<String> typeAlias = givenTypeAlias(value);
//then //then
assertThat(typeAlias.<Boolean>map(value::equals)).isTrue(); assertThat(typeAlias.getValue()).isSameAs(value);
}
private TypeAlias<String> givenTypeAlias(final String value) {
return new TypeAlias<String>(value) {
};
} }
@Test @Test
@ -25,38 +32,34 @@ public class TypeAliasTest {
final Iterable<String> iterable = Collections.emptyList(); final Iterable<String> iterable = Collections.emptyList();
//when //when
final TypeAlias<Iterable<String>> typeAlias = final TypeAlias<Iterable<String>> typeAlias =
new TypeAlias<Iterable<String>>(iterable, Iterable.class) { new TypeAlias<Iterable<String>>(iterable) {
}; };
//then //then
assertThat(typeAlias.<Boolean>map(iterable::equals)).isTrue(); assertThat(typeAlias.getValue()).isSameAs(iterable);
}
private TypeAlias<String> givenTypeAlias(final String value) {
return new TypeAlias<String>(value, String.class) {
};
} }
@Test @Test
public void shouldCreateAnAliasedTypeAndGetTheValue() throws Exception { public void shouldCreateATypeAliasSubclassAndGetTheValue() {
//given //given
final String value = "value"; final String value = "value";
//when //when
final AnAlias anAlias = AnAlias.of(value); final AnAlias anAlias = AnAlias.of(value);
//then //then
assertThat(anAlias.<Boolean>map(value::equals)).isTrue(); assertThat(anAlias.getValue()).isSameAs(value);
} }
@Test @Test
public void shouldNotBeEqualWhenValueTypesAreDifferent() { public void shouldNotBeEqualWhenValueTypesAreDifferent() {
//given //given
final TypeAlias<String> stringTypeAlias = givenTypeAlias("1"); final TypeAlias<String> stringTypeAlias = givenTypeAlias("1");
final TypeAlias<Integer> integerTypeAlias = new TypeAlias<Integer>(1, Integer.class){}; final TypeAlias<Integer> integerTypeAlias = new TypeAlias<Integer>(1) {
};
//then //then
assertThat(stringTypeAlias).isNotEqualTo(integerTypeAlias); assertThat(stringTypeAlias).isNotEqualTo(integerTypeAlias);
} }
@Test @Test
public void shouldBeEqualWhenValuesAreTheSame() throws Exception { public void shouldBeEqualWhenValuesAreTheSame() {
//given //given
final String value = "value"; final String value = "value";
final AnAlias anAlias1 = AnAlias.of(value); final AnAlias anAlias1 = AnAlias.of(value);
@ -66,7 +69,16 @@ public class TypeAliasTest {
} }
@Test @Test
public void shouldBeEqualToUnAliasedValue() throws Exception { public void shouldNotBeEqualWhenValuesAreNotTheSame() {
//given
final AnAlias valueA = AnAlias.of("value a");
final AnAlias valueB = AnAlias.of("value b");
//then
assertThat(valueA).isNotEqualTo(valueB);
}
@Test
public void shouldBeEqualToRawValue() {
//given //given
final String value = "value"; final String value = "value";
final AnAlias anAlias = AnAlias.of(value); final AnAlias anAlias = AnAlias.of(value);
@ -75,7 +87,7 @@ public class TypeAliasTest {
} }
@Test @Test
public void shouldHaveHashCodeOfValue() throws Exception { public void shouldHaveHashCodeOfValue() {
//given //given
final String value = "value"; final String value = "value";
final AnAlias anAlias = AnAlias.of(value); final AnAlias anAlias = AnAlias.of(value);
@ -84,24 +96,24 @@ public class TypeAliasTest {
} }
@Test @Test
public void shouldHaveSameToStringAsAliasedType() throws Exception { public void shouldHaveSameToStringAsAliasedType() {
//given //given
final String value = "value"; final List<Integer> value = Arrays.asList(1, 2, 3);
//when //when
final AnAlias anAlias = AnAlias.of(value); final TypeAlias<List<Integer>> anAlias = new TypeAlias<List<Integer>>(value) {
};
//then //then
assertThat(anAlias.toString()).isEqualTo(value); assertThat(anAlias.toString()).isEqualTo(value.toString());
} }
@Test @Test
public void shouldMapTypeAlias() { public void shouldMapTypeAlias() {
//given //given
final AnAlias anAlias = AnAlias.of("text"); final AnAlias anAlias = AnAlias.of("text");
final Function<String, String> function = v -> v;
//when //when
final String value = anAlias.map(function); final String value = anAlias.map(Strings::quote);
//then //then
assertThat(value).isEqualTo("text"); assertThat(value).isEqualTo("'text'");
} }
private static class AnAlias extends TypeAlias<String> { private static class AnAlias extends TypeAlias<String> {
@ -112,7 +124,7 @@ public class TypeAliasTest {
* @param value the value * @param value the value
*/ */
protected AnAlias(final String value) { protected AnAlias(final String value) {
super(value, String.class); super(value);
} }
protected static AnAlias of(final String value) { protected static AnAlias of(final String value) {