Merge pull request #6 from kemitix/release-0.2.0

Release 0.2.0
This commit is contained in:
Paul Campbell 2017-10-19 22:50:39 +01:00 committed by GitHub
commit 339449b64a
12 changed files with 94 additions and 34 deletions

View file

@ -14,5 +14,3 @@ deploy:
script: sh .travis-support/deploy.sh script: sh .travis-support/deploy.sh
on: on:
branch: master branch: master
env:
global:

View file

@ -1,6 +1,14 @@
CHANGELOG 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 0.1.0
----- -----

View file

@ -21,6 +21,8 @@ TypeAlias for Java
## Usage ## Usage
### TypeAlias
```java ```java
class Goal extends TypeAlias<String> { class Goal extends TypeAlias<String> {
private Goal(final String goal) { private Goal(final String goal) {

11
pom.xml
View file

@ -10,19 +10,24 @@
<version>3.2.4</version> <version>3.2.4</version>
</parent> </parent>
<artifactId>mon</artifactId> <artifactId>mon</artifactId>
<version>0.1.0</version> <version>0.2.0</version>
<properties>
<junit.version>4.12</junit.version>
<assertj-core.version>3.8.0</assertj-core.version>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.12</version> <version>${junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.assertj</groupId> <groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId> <artifactId>assertj-core</artifactId>
<version>3.8.0</version> <version>${assertj-core.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -24,21 +24,28 @@ package net.kemitix.mon;
import java.util.function.Function; import java.util.function.Function;
/** /**
* The Functor interface. * The Functor is used for types that can be mapped over.
* *
* @param <T> the type of the functor content * <p>Implementations of Functor should satisfy the following laws:</p>
*
* <ul>
* <li>map id == id</li>
* <li>map (f . g) == map f . map g</li>
* </ul>
*
* @param <T> the type of the Functor
* *
* @author Tomasz Nurkiewicz (?@?.?) * @author Tomasz Nurkiewicz (?@?.?)
*/ */
public interface Functor<T> { public interface Functor<T> {
/** /**
* 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 f the function to apply
* @param <R> the type the functor maps to * @param <R> 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
*/ */
<R> Functor<R> map(Function<T, R> f); <R> Functor<R> map(Function<T, R> f);
} }

View file

@ -82,16 +82,8 @@ public class Mon<T> implements Functor<T> {
return new Mon<>(v); 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 <R> the type of the result of the function
*
* @return a Mon containing the result of the function {@code f} to the value
*/
@Override @Override
public <R> Mon<R> map(final Function<T, R> f) { public final <R> Mon<R> map(final Function<T, R> f) {
return Mon.of(f.apply(value)); return Mon.of(f.apply(value));
} }
@ -104,7 +96,7 @@ public class Mon<T> implements Functor<T> {
* *
* @return a Mon containing the result of the function * @return a Mon containing the result of the function
*/ */
public <R> Mon<R> flatMap(final Function<T, Mon<R>> f) { public final <R> Mon<R> flatMap(final Function<T, Mon<R>> f) {
return f.apply(value); return f.apply(value);
} }

View file

@ -21,6 +21,8 @@
package net.kemitix.mon; package net.kemitix.mon;
import java.util.function.Function;
/** /**
* Type Alias for other types. * Type Alias for other types.
* *
@ -45,6 +47,18 @@ public abstract class TypeAlias<T> {
this.value = value; 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 final <R> R map(final Function<T, R> f) {
return f.apply(value);
}
@Override @Override
public final int hashCode() { public final int hashCode() {
return value.hashCode(); return value.hashCode();
@ -68,8 +82,7 @@ public abstract class TypeAlias<T> {
* *
* @return the value * @return the value
*/ */
public final T getValue() { public T getValue() {
return value; return value;
} }
} }

View file

@ -33,7 +33,7 @@ class Address {
private final String street; private final String street;
String getStreet() { protected String getStreet() {
return street; return street;
} }
} }

View file

@ -33,7 +33,7 @@ class Customer {
private final Address address; private final Address address;
Address getAddress() { protected Address getAddress() {
return address; return address;
} }

View file

@ -10,6 +10,15 @@ import org.junit.Test;
*/ */
public class IdentityTest implements WithAssertions { public class IdentityTest implements WithAssertions {
@Test
public void functorLawMapIdEqualsId() {
//given
final String id = "id";
//when
//then
}
@Test @Test
public void canMapIdentityFromStringToInteger() { public void canMapIdentityFromStringToInteger() {
//given //given
@ -17,7 +26,11 @@ public class IdentityTest implements WithAssertions {
//when //when
final Identity<Integer> idInt = idString.map(String::length); final Identity<Integer> idInt = idString.map(String::length);
//then //then
idInt.map(id -> assertThat(id).isEqualTo(3)); assertIdentityContains(idInt, 3);
}
private <T> void assertIdentityContains(final Identity<T> identity, final T expected) {
identity.map(id -> assertThat(id).isEqualTo(expected));
} }
@Test @Test
@ -31,7 +44,7 @@ public class IdentityTest implements WithAssertions {
.map(String::toLowerCase) .map(String::toLowerCase)
.map(String::getBytes); .map(String::getBytes);
//then //then
idBytes.map(bytes -> assertThat(bytes).isEqualTo("par".getBytes())); assertIdentityContains(idBytes, "par".getBytes());
} }
} }

View file

@ -15,7 +15,14 @@ public class MonTest {
//when //when
final Mon<String> wrap = Mon.of("test"); final Mon<String> wrap = Mon.of("test");
//then //then
wrap.map(value -> assertThat(value).isEqualTo("test")); assertMonContains(wrap, "test");
}
private <T> void assertMonContains(
final Mon<T> wrap,
final T expected
) {
wrap.map(value -> assertThat(value).isEqualTo(expected));
} }
@Test @Test
@ -25,7 +32,7 @@ public class MonTest {
//when //when
final Mon<String> updated = wrap.map(a -> a + " more"); final Mon<String> updated = wrap.map(a -> a + " more");
//then //then
updated.map(value -> assertThat(value).isEqualTo("test more")); assertMonContains(updated, "test more");
} }
@Test @Test
@ -35,7 +42,7 @@ public class MonTest {
//when //when
final Mon<Integer> result = wrap.map(String::length); final Mon<Integer> result = wrap.map(String::length);
//then //then
result.map(value -> assertThat(value).isEqualTo(4)); assertMonContains(result, 4);
} }
@Test @Test
@ -52,7 +59,7 @@ public class MonTest {
final Optional<Mon<String>> longAndInvalid = factory.apply("value is too long"); final Optional<Mon<String>> longAndInvalid = factory.apply("value is too long");
//then //then
assertThat(shortAndValid).isNotEmpty(); assertThat(shortAndValid).isNotEmpty();
shortAndValid.ifPresent(valid -> valid.map(value -> assertThat(value).contains("value okay"))); shortAndValid.ifPresent(valid -> assertMonContains(valid, "value okay"));
assertThat(longAndInvalid).isEmpty(); assertThat(longAndInvalid).isEmpty();
} }
@ -76,7 +83,7 @@ public class MonTest {
final Mon<Mon<String>> nonFlatMapped = wrap.map(Mon::of); final Mon<Mon<String>> nonFlatMapped = wrap.map(Mon::of);
final Mon<String> result = wrap.flatMap(Mon::of); final Mon<String> result = wrap.flatMap(Mon::of);
//then //then
result.map(value -> assertThat(value).isEqualTo("test")); assertMonContains(result, "test");
nonFlatMapped.map(inner -> assertThat(result).isEqualTo(inner)); nonFlatMapped.map(inner -> assertThat(result).isEqualTo(inner));
} }

View file

@ -2,6 +2,8 @@ package net.kemitix.mon;
import org.junit.Test; import org.junit.Test;
import java.util.function.Function;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public class TypeAliasTest { public class TypeAliasTest {
@ -59,8 +61,21 @@ public class TypeAliasTest {
public void shouldHaveSameToStringAsAliasedType() throws Exception { public void shouldHaveSameToStringAsAliasedType() throws Exception {
//given //given
final String value = "value"; final String value = "value";
//when
final AnAlias anAlias = AnAlias.of(value); 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<String, String> function = v -> v;
//when
final String value = anAlias.map(function);
//then
assertThat(value).isEqualTo("text");
} }
private static class AnAlias extends TypeAlias<String> { private static class AnAlias extends TypeAlias<String> {
@ -74,7 +89,7 @@ public class TypeAliasTest {
super(value); super(value);
} }
static AnAlias of(final String value) { protected static AnAlias of(final String value) {
return new AnAlias(value); return new AnAlias(value);
} }
} }