diff --git a/.travis.yml b/.travis.yml index 0d1d54a..1f3bfa4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,5 +14,3 @@ deploy: script: sh .travis-support/deploy.sh on: branch: master -env: - global: diff --git a/CHANGELOG b/CHANGELOG index 980a0fc..893b4f3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,14 @@ CHANGELOG ========= +0.2.0 +----- + +* `TypeAlias.getValue()` is not `final` +* Added `TypeAlias.map()` +* `Mon.map()` and `Mon.flatMap()` are `final` +* Codacy Quality clean up + 0.1.0 ----- diff --git a/README.md b/README.md index 954e59f..70333f7 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ TypeAlias for Java ## Usage +### TypeAlias + ```java class Goal extends TypeAlias { private Goal(final String goal) { diff --git a/pom.xml b/pom.xml index e8a56ca..67fe99e 100644 --- a/pom.xml +++ b/pom.xml @@ -10,19 +10,24 @@ 3.2.4 mon - 0.1.0 + 0.2.0 + + + 4.12 + 3.8.0 + junit junit - 4.12 + ${junit.version} test org.assertj assertj-core - 3.8.0 + ${assertj-core.version} test 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/Address.java b/src/test/java/net/kemitix/mon/Address.java index 8bfbd37..5e8b0cb 100644 --- a/src/test/java/net/kemitix/mon/Address.java +++ b/src/test/java/net/kemitix/mon/Address.java @@ -33,7 +33,7 @@ class Address { private final String street; - String getStreet() { + protected String getStreet() { return street; } } diff --git a/src/test/java/net/kemitix/mon/Customer.java b/src/test/java/net/kemitix/mon/Customer.java index 594abd7..f96f133 100644 --- a/src/test/java/net/kemitix/mon/Customer.java +++ b/src/test/java/net/kemitix/mon/Customer.java @@ -33,7 +33,7 @@ class Customer { private final Address address; - Address getAddress() { + protected Address getAddress() { return address; } diff --git a/src/test/java/net/kemitix/mon/IdentityTest.java b/src/test/java/net/kemitix/mon/IdentityTest.java index c93a2e9..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 @@ -17,7 +26,11 @@ public class IdentityTest implements WithAssertions { //when final Identity idInt = idString.map(String::length); //then - idInt.map(id -> assertThat(id).isEqualTo(3)); + assertIdentityContains(idInt, 3); + } + + private void assertIdentityContains(final Identity identity, final T expected) { + identity.map(id -> assertThat(id).isEqualTo(expected)); } @Test @@ -31,7 +44,7 @@ public class IdentityTest implements WithAssertions { .map(String::toLowerCase) .map(String::getBytes); //then - idBytes.map(bytes -> assertThat(bytes).isEqualTo("par".getBytes())); + assertIdentityContains(idBytes, "par".getBytes()); } } diff --git a/src/test/java/net/kemitix/mon/MonTest.java b/src/test/java/net/kemitix/mon/MonTest.java index 91b51f0..3d8a660 100644 --- a/src/test/java/net/kemitix/mon/MonTest.java +++ b/src/test/java/net/kemitix/mon/MonTest.java @@ -15,7 +15,14 @@ public class MonTest { //when final Mon wrap = Mon.of("test"); //then - wrap.map(value -> assertThat(value).isEqualTo("test")); + assertMonContains(wrap, "test"); + } + + private void assertMonContains( + final Mon wrap, + final T expected + ) { + wrap.map(value -> assertThat(value).isEqualTo(expected)); } @Test @@ -25,7 +32,7 @@ public class MonTest { //when final Mon updated = wrap.map(a -> a + " more"); //then - updated.map(value -> assertThat(value).isEqualTo("test more")); + assertMonContains(updated, "test more"); } @Test @@ -35,7 +42,7 @@ public class MonTest { //when final Mon result = wrap.map(String::length); //then - result.map(value -> assertThat(value).isEqualTo(4)); + assertMonContains(result, 4); } @Test @@ -52,7 +59,7 @@ public class MonTest { final Optional> longAndInvalid = factory.apply("value is too long"); //then assertThat(shortAndValid).isNotEmpty(); - shortAndValid.ifPresent(valid -> valid.map(value -> assertThat(value).contains("value okay"))); + shortAndValid.ifPresent(valid -> assertMonContains(valid, "value okay")); assertThat(longAndInvalid).isEmpty(); } @@ -76,7 +83,7 @@ public class MonTest { final Mon> nonFlatMapped = wrap.map(Mon::of); final Mon result = wrap.flatMap(Mon::of); //then - result.map(value -> assertThat(value).isEqualTo("test")); + assertMonContains(result, "test"); nonFlatMapped.map(inner -> assertThat(result).isEqualTo(inner)); } diff --git a/src/test/java/net/kemitix/mon/TypeAliasTest.java b/src/test/java/net/kemitix/mon/TypeAliasTest.java index 91687c3..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,8 +61,21 @@ public class TypeAliasTest { public void shouldHaveSameToStringAsAliasedType() throws Exception { //given final String value = "value"; + //when final AnAlias anAlias = AnAlias.of(value); - assertThat(anAlias.toString()).isEqualTo(value.toString()); + //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 { @@ -74,7 +89,7 @@ public class TypeAliasTest { super(value); } - static AnAlias of(final String value) { + protected static AnAlias of(final String value) { return new AnAlias(value); } }