TypeAlias.map(): added

This commit is contained in:
Paul Campbell 2017-10-07 18:56:38 +01:00
parent 1046b6c5ae
commit 4c85e56c80
2 changed files with 30 additions and 1 deletions

View file

@ -21,6 +21,8 @@
package net.kemitix.mon;
import java.util.function.Function;
/**
* Type Alias for other types.
*
@ -45,6 +47,19 @@ public abstract class TypeAlias<T> {
this.value = value;
}
/**
* Map the TypeAlias into another value.
*
* @param f the function to create the new value
* @param <R> the type of the new value
*
* @return a TypeAlias
*/
public <R> R map(final Function<T, R> f) {
return f.apply(value);
}
@Override
public final int hashCode() {
return value.hashCode();
@ -71,5 +86,4 @@ public abstract class TypeAlias<T> {
public final T getValue() {
return value;
}
}

View file

@ -2,6 +2,8 @@ package net.kemitix.mon;
import org.junit.Test;
import java.util.function.Function;
import static org.assertj.core.api.Assertions.assertThat;
public class TypeAliasTest {
@ -59,10 +61,23 @@ public class TypeAliasTest {
public void shouldHaveSameToStringAsAliasedType() throws Exception {
//given
final String value = "value";
//when
final AnAlias anAlias = AnAlias.of(value);
//then
assertThat(anAlias.toString()).isEqualTo(value);
}
@Test
public void shouldMapTypeAlias() {
//given
final AnAlias anAlias = AnAlias.of("text");
final Function<String, String> function = v -> v;
//when
final String value = anAlias.map(function);
//then
assertThat(value).isEqualTo("text");
}
private static class AnAlias extends TypeAlias<String> {
/**