From ccf3a1115755dcdea0d1c5e9051ce18350a9c17c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 19 Sep 2017 23:09:28 +0100 Subject: [PATCH 1/8] MonTest: apply stepdown rule to methods --- src/test/java/net/kemitix/mon/MonTest.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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)); - } } From fdc4cad4e4f999935b0e5b9825ccb4428a006b5b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 19 Sep 2017 23:10:31 +0100 Subject: [PATCH 2/8] Functor: rewrite map() javadoc --- src/main/java/net/kemitix/mon/Functor.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/kemitix/mon/Functor.java b/src/main/java/net/kemitix/mon/Functor.java index dbcd72f..20e064f 100644 --- a/src/main/java/net/kemitix/mon/Functor.java +++ b/src/main/java/net/kemitix/mon/Functor.java @@ -33,12 +33,12 @@ import java.util.function.Function; 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); } From 3d8ebfeed6a2f30611acbeea79380eae212d9f6f Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 19 Sep 2017 23:11:18 +0100 Subject: [PATCH 3/8] Mon: make map() and flatMap() final Also remove map() javadoc that is now in Functor. --- src/main/java/net/kemitix/mon/Mon.java | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) 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); } From cf723da38ae672e118248067f684531091286ec1 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 30 Sep 2017 15:41:08 +0100 Subject: [PATCH 4/8] Functor: clean up javadoc --- src/main/java/net/kemitix/mon/Functor.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/kemitix/mon/Functor.java b/src/main/java/net/kemitix/mon/Functor.java index 20e064f..0111ca1 100644 --- a/src/main/java/net/kemitix/mon/Functor.java +++ b/src/main/java/net/kemitix/mon/Functor.java @@ -24,9 +24,16 @@ 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:

+ * + *
    + *
  • map id == id
  • + *
  • map (f . g) == map f . map g
  • + *
+ * + * @param the type of the Functor * * @author Tomasz Nurkiewicz (?@?.?) */ From 1046b6c5ae179850237bbafdcf54c948a1ec0dc0 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 30 Sep 2017 16:46:49 +0100 Subject: [PATCH 5/8] entities: add static builder and constructors and make previous revision Optional --- src/test/java/net/kemitix/mon/IdentityTest.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) 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)); - } - } From 4c85e56c803612faed22b63e2617be799d6e879c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 7 Oct 2017 18:56:38 +0100 Subject: [PATCH 6/8] TypeAlias.map(): added --- src/main/java/net/kemitix/mon/TypeAlias.java | 16 +++++++++++++++- src/test/java/net/kemitix/mon/TypeAliasTest.java | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) 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 { /** From 1e8941b0303faf7bb5e923cbeedc1129adca93dc Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 19 Oct 2017 18:42:55 +0100 Subject: [PATCH 7/8] TypeAlias: allow getValue() to be overridden --- src/main/java/net/kemitix/mon/TypeAlias.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/kemitix/mon/TypeAlias.java b/src/main/java/net/kemitix/mon/TypeAlias.java index 2f18f13..ce3cb5f 100644 --- a/src/main/java/net/kemitix/mon/TypeAlias.java +++ b/src/main/java/net/kemitix/mon/TypeAlias.java @@ -83,7 +83,7 @@ public abstract class TypeAlias { * * @return the value */ - public final T getValue() { + public T getValue() { return value; } } From b5b11982e1afe0bd3a98e901bfe275aa44426dd5 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 19 Oct 2017 18:45:45 +0100 Subject: [PATCH 8/8] TypeAlias: prevent map() from being overridden --- src/main/java/net/kemitix/mon/TypeAlias.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/net/kemitix/mon/TypeAlias.java b/src/main/java/net/kemitix/mon/TypeAlias.java index ce3cb5f..f5e7636 100644 --- a/src/main/java/net/kemitix/mon/TypeAlias.java +++ b/src/main/java/net/kemitix/mon/TypeAlias.java @@ -47,7 +47,6 @@ public abstract class TypeAlias { this.value = value; } - /** * Map the TypeAlias into another value. * @@ -56,7 +55,7 @@ public abstract class TypeAlias { * * @return a TypeAlias */ - public R map(final Function f) { + public final R map(final Function f) { return f.apply(value); }