Merge pull request #5 from kemitix/polish

Polish
This commit is contained in:
Paul Campbell 2017-10-19 19:31:38 +01:00 committed by GitHub
commit df7ef1c5c4
6 changed files with 65 additions and 26 deletions

View file

@ -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 <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 (?@?.?)
*/
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 <R> the type the functor maps to
* @param f the function to apply
* @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);
}

View file

@ -82,16 +82,8 @@ public class Mon<T> implements Functor<T> {
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
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));
}
@ -104,7 +96,7 @@ public class Mon<T> implements Functor<T> {
*
* @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);
}

View file

@ -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<T> {
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
public final int hashCode() {
return value.hashCode();
@ -68,8 +82,7 @@ public abstract class TypeAlias<T> {
*
* @return the value
*/
public final T getValue() {
public T getValue() {
return value;
}
}

View file

@ -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 <T> void assertIdentityContains(final Identity<T> 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 <T> void assertIdentityContains(final Identity<T> identity, final T expected) {
identity.map(id -> assertThat(id).isEqualTo(expected));
}
}

View file

@ -18,6 +18,13 @@ public class MonTest {
assertMonContains(wrap, "test");
}
private <T> void assertMonContains(
final Mon<T> 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 <T> void assertMonContains(final Mon<T> wrap, final T expected) {
wrap.map(value -> assertThat(value).isEqualTo(expected));
}
}

View file

@ -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<String, String> function = v -> v;
//when
final String value = anAlias.map(function);
//then
assertThat(value).isEqualTo("text");
}
private static class AnAlias extends TypeAlias<String> {
/**