diff --git a/src/main/java/net/kemitix/mon/Functor.java b/src/main/java/net/kemitix/mon/Functor.java index dbcd72f..0111ca1 100644 --- a/src/main/java/net/kemitix/mon/Functor.java +++ b/src/main/java/net/kemitix/mon/Functor.java @@ -24,21 +24,28 @@ package net.kemitix.mon; import java.util.function.Function; /** - * The Functor interface. + * The Functor is used for types that can be mapped over. * - * @param the type of the functor content + *

Implementations of Functor should satisfy the following laws:

+ * + * + * + * @param the type of the Functor * * @author Tomasz Nurkiewicz (?@?.?) */ public interface Functor { /** - * Map the content of the functor through the function. + * Applies the function to the value within the Functor, returning the result within a Functor. * - * @param f the function - * @param the type the functor maps to + * @param f the function to apply + * @param the type of the result of the function * - * @return the new functor + * @return a Functor containing the result of the function {@code f} applied to the value */ Functor map(Function f); } diff --git a/src/main/java/net/kemitix/mon/Mon.java b/src/main/java/net/kemitix/mon/Mon.java index eff8554..8abe82d 100644 --- a/src/main/java/net/kemitix/mon/Mon.java +++ b/src/main/java/net/kemitix/mon/Mon.java @@ -82,16 +82,8 @@ public class Mon implements Functor { return new Mon<>(v); } - /** - * Applies the function to the value within the Mon, returning a Mon containing the result. - * - * @param f the function to apply - * @param the type of the result of the function - * - * @return a Mon containing the result of the function {@code f} to the value - */ @Override - public Mon map(final Function f) { + public final Mon map(final Function f) { return Mon.of(f.apply(value)); } @@ -104,7 +96,7 @@ public class Mon implements Functor { * * @return a Mon containing the result of the function */ - public Mon flatMap(final Function> f) { + public final Mon flatMap(final Function> f) { return f.apply(value); } diff --git a/src/main/java/net/kemitix/mon/TypeAlias.java b/src/main/java/net/kemitix/mon/TypeAlias.java index 2dd5e6e..f5e7636 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,18 @@ 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 final R map(final Function f) { + return f.apply(value); + } + @Override public final int hashCode() { return value.hashCode(); @@ -68,8 +82,7 @@ public abstract class TypeAlias { * * @return the value */ - public final T getValue() { + public T getValue() { return value; } - } diff --git a/src/test/java/net/kemitix/mon/IdentityTest.java b/src/test/java/net/kemitix/mon/IdentityTest.java index 5a1b3f3..513a979 100644 --- a/src/test/java/net/kemitix/mon/IdentityTest.java +++ b/src/test/java/net/kemitix/mon/IdentityTest.java @@ -10,6 +10,15 @@ import org.junit.Test; */ public class IdentityTest implements WithAssertions { + @Test + public void functorLawMapIdEqualsId() { + //given + final String id = "id"; + //when + + //then + } + @Test public void canMapIdentityFromStringToInteger() { //given @@ -20,6 +29,10 @@ public class IdentityTest implements WithAssertions { assertIdentityContains(idInt, 3); } + private void assertIdentityContains(final Identity identity, final T expected) { + identity.map(id -> assertThat(id).isEqualTo(expected)); + } + @Test public void canFluentlyComposeFunctions() { //given @@ -34,8 +47,4 @@ public class IdentityTest implements WithAssertions { assertIdentityContains(idBytes, "par".getBytes()); } - private void assertIdentityContains(final Identity identity, final T expected) { - identity.map(id -> assertThat(id).isEqualTo(expected)); - } - } diff --git a/src/test/java/net/kemitix/mon/MonTest.java b/src/test/java/net/kemitix/mon/MonTest.java index 96272c3..3d8a660 100644 --- a/src/test/java/net/kemitix/mon/MonTest.java +++ b/src/test/java/net/kemitix/mon/MonTest.java @@ -18,6 +18,13 @@ public class MonTest { assertMonContains(wrap, "test"); } + private void assertMonContains( + final Mon wrap, + final T expected + ) { + wrap.map(value -> assertThat(value).isEqualTo(expected)); + } + @Test public void canMap() { //given @@ -144,8 +151,4 @@ public class MonTest { assertThat(one).isNotEqualTo(notAMon); assertThat(one).isNotEqualTo(null); } - - private void assertMonContains(final Mon wrap, final T expected) { - wrap.map(value -> assertThat(value).isEqualTo(expected)); - } } 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 { /**