diff --git a/src/main/java/net/kemitix/mon/TypeAlias.java b/src/main/java/net/kemitix/mon/TypeAlias.java index 2dd5e6e..2f18f13 100644 --- a/src/main/java/net/kemitix/mon/TypeAlias.java +++ b/src/main/java/net/kemitix/mon/TypeAlias.java @@ -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 { this.value = value; } + + /** + * Map the TypeAlias into another value. + * + * @param f the function to create the new value + * @param the type of the new value + * + * @return a TypeAlias + */ + public R map(final Function f) { + return f.apply(value); + } + @Override public final int hashCode() { return value.hashCode(); @@ -71,5 +86,4 @@ public abstract class TypeAlias { public final T getValue() { return value; } - } diff --git a/src/test/java/net/kemitix/mon/TypeAliasTest.java b/src/test/java/net/kemitix/mon/TypeAliasTest.java index 069c6ab..b6fb753 100644 --- a/src/test/java/net/kemitix/mon/TypeAliasTest.java +++ b/src/test/java/net/kemitix/mon/TypeAliasTest.java @@ -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 function = v -> v; + //when + final String value = anAlias.map(function); + //then + assertThat(value).isEqualTo("text"); + } + private static class AnAlias extends TypeAlias { /**