commit
339449b64a
12 changed files with 94 additions and 34 deletions
|
@ -14,5 +14,3 @@ deploy:
|
|||
script: sh .travis-support/deploy.sh
|
||||
on:
|
||||
branch: master
|
||||
env:
|
||||
global:
|
||||
|
|
|
@ -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
|
||||
-----
|
||||
|
||||
|
|
|
@ -21,6 +21,8 @@ TypeAlias for Java
|
|||
|
||||
## Usage
|
||||
|
||||
### TypeAlias
|
||||
|
||||
```java
|
||||
class Goal extends TypeAlias<String> {
|
||||
private Goal(final String goal) {
|
||||
|
|
11
pom.xml
11
pom.xml
|
@ -10,19 +10,24 @@
|
|||
<version>3.2.4</version>
|
||||
</parent>
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ class Address {
|
|||
|
||||
private final String street;
|
||||
|
||||
String getStreet() {
|
||||
protected String getStreet() {
|
||||
return street;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ class Customer {
|
|||
|
||||
private final Address address;
|
||||
|
||||
Address getAddress() {
|
||||
protected Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Integer> idInt = idString.map(String::length);
|
||||
//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
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,7 +15,14 @@ public class MonTest {
|
|||
//when
|
||||
final Mon<String> wrap = Mon.of("test");
|
||||
//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
|
||||
|
@ -25,7 +32,7 @@ public class MonTest {
|
|||
//when
|
||||
final Mon<String> 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<Integer> 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<Mon<String>> 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<Mon<String>> nonFlatMapped = wrap.map(Mon::of);
|
||||
final Mon<String> result = wrap.flatMap(Mon::of);
|
||||
//then
|
||||
result.map(value -> assertThat(value).isEqualTo("test"));
|
||||
assertMonContains(result, "test");
|
||||
nonFlatMapped.map(inner -> assertThat(result).isEqualTo(inner));
|
||||
}
|
||||
|
||||
|
|
|
@ -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<String, String> function = v -> v;
|
||||
//when
|
||||
final String value = anAlias.map(function);
|
||||
//then
|
||||
assertThat(value).isEqualTo("text");
|
||||
}
|
||||
|
||||
private static class AnAlias extends TypeAlias<String> {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue