e) throws Exception {
+ throw e.get();
+ }
+}
diff --git a/src/main/java/net/kemitix/mon/TypeAlias.java b/src/main/java/net/kemitix/mon/TypeAlias.java
index f5dc0c5..7676736 100644
--- a/src/main/java/net/kemitix/mon/TypeAlias.java
+++ b/src/main/java/net/kemitix/mon/TypeAlias.java
@@ -26,6 +26,9 @@ import java.util.function.Function;
/**
* Type Alias for other types.
*
+ * {@link #toString()}, {@link #equals(Object)} and {@link #hashCode()} are all transparent, returning the value
+ * for the type being aliased.
+ *
* @param the type of the alias
*
* @author Paul Campbell (pcampbell@kemitix.net)
@@ -38,20 +41,13 @@ public abstract class TypeAlias {
*/
private final T value;
- private final Class super T> type;
-
/**
* Constructor.
*
* @param value the value
- * @param type the type of the value
*/
- protected TypeAlias(
- final T value,
- final Class super T> type
- ) {
+ protected TypeAlias(final T value) {
this.value = value;
- this.type = type;
}
/**
@@ -72,14 +68,13 @@ public abstract class TypeAlias {
}
@Override
- @SuppressWarnings("unchecked")
public final boolean equals(final Object o) {
if (o instanceof TypeAlias) {
- if (((TypeAlias) o).type.equals(type)) {
- return ((TypeAlias) o).map(getValue()::equals);
- } else {
- return false;
- }
+ final TypeAlias other = (TypeAlias) o;
+ final Object otherValue = other.getValue();
+ final Class> otherValueClass = otherValue.getClass();
+ return otherValueClass.equals(getValue().getClass())
+ && otherValue.equals(getValue());
}
return map(o::equals);
}
@@ -94,7 +89,7 @@ public abstract class TypeAlias {
*
* @return the value
*/
- private T getValue() {
+ public T getValue() {
return value;
}
}
diff --git a/src/test/java/net/kemitix/mon/IdentityTest.java b/src/test/java/net/kemitix/mon/IdentityTest.java
index 513a979..db95452 100644
--- a/src/test/java/net/kemitix/mon/IdentityTest.java
+++ b/src/test/java/net/kemitix/mon/IdentityTest.java
@@ -10,15 +10,6 @@ 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
diff --git a/src/test/java/net/kemitix/mon/MaybeTest.java b/src/test/java/net/kemitix/mon/MaybeTest.java
new file mode 100644
index 0000000..ba68b3c
--- /dev/null
+++ b/src/test/java/net/kemitix/mon/MaybeTest.java
@@ -0,0 +1,114 @@
+package net.kemitix.mon;
+
+import org.junit.Test;
+
+import java.util.Objects;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Predicate;
+
+import static net.kemitix.mon.Maybe.just;
+import static net.kemitix.mon.Maybe.maybe;
+import static net.kemitix.mon.Maybe.nothing;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatNullPointerException;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+public class MaybeTest {
+
+ private static Predicate eq(final T value) {
+ return v -> Objects.equals(value, v);
+ }
+
+ @Test
+ public void justMustBeNonNull() {
+ assertThatNullPointerException().isThrownBy(() -> just(null))
+ .withMessage("value");
+ }
+
+ @Test
+ public void nothingReusesTheSameInstance() {
+ assertThat(nothing()).isSameAs(nothing());
+ }
+
+ @Test
+ public void equality() {
+ assertThat(just(1)).isEqualTo(just(1));
+ assertThat(just(1)).isNotEqualTo(just(2));
+ assertThat(just(1)).isNotEqualTo(nothing());
+ assertThat(nothing()).isEqualTo(nothing());
+ assertThat(just(1).equals("1")).isFalse();
+ }
+
+ @Test
+ public void maybeAllowsNull() {
+ assertThat(just(1)).isEqualTo(maybe(1));
+ assertThat(nothing()).isEqualTo(maybe(null));
+ }
+
+ @Test
+ public void map() {
+ assertThat(just(1).map(v -> v + 1)).isEqualTo(just(2));
+ assertThat(nothing().map(v -> v)).isEqualTo(nothing());
+ }
+
+ @Test
+ public void testHashCode() {
+ assertThat(just(1).hashCode()).isEqualTo(Objects.hashCode(1));
+ }
+
+ @Test
+ public void orElseGet() {
+ assertThat(just(1).orElseGet(() -> -1)).isEqualTo(1);
+ assertThat(nothing().orElseGet(() -> -1)).isEqualTo(-1);
+ }
+
+ @Test
+ public void orElse() {
+ assertThat(just(1).orElse(-1)).isEqualTo(1);
+ assertThat(nothing().orElse(-1)).isEqualTo(-1);
+ }
+
+ @Test
+ public void filter() {
+ assertThat(just(1).filter(eq(1))).isEqualTo(just(1));
+ assertThat(just(1).filter(eq(0))).isEqualTo(nothing());
+ assertThat(nothing().filter(eq(1))).isEqualTo(nothing());
+ }
+
+ @Test
+ public void toOptional() {
+ assertThat(just(1).toOptional()).isEqualTo(Optional.of(1));
+ assertThat(nothing()
+ .toOptional()).isEqualTo(Optional.empty());
+ }
+
+ @Test
+ public void fromOptional() {
+ assertThat(Maybe.fromOptional(Optional.of(1))).isEqualTo(just(1));
+ assertThat(Maybe.fromOptional(Optional.empty())).isEqualTo(nothing());
+ }
+
+
+ @Test
+ public void peek() {
+ final AtomicInteger ref = new AtomicInteger(0);
+ assertThat(just(1).peek(x -> ref.incrementAndGet())).isEqualTo(just(1));
+ assertThat(ref.get()).isEqualTo(1);
+
+ assertThat(nothing().peek(x -> ref.incrementAndGet())).isEqualTo(nothing());
+ assertThat(ref.get()).isEqualTo(1);
+ }
+
+ @Test
+ public void justOrThrow() {
+ assertThatCode(() -> just(1).orElseThrow(IllegalStateException::new)).doesNotThrowAnyException();
+ }
+
+ @Test
+ public void nothingOrThrow() {
+ assertThatThrownBy(() -> nothing().orElseThrow(IllegalStateException::new)).isInstanceOf(
+ IllegalStateException.class);
+ }
+}
diff --git a/src/test/java/net/kemitix/mon/TypeAliasTest.java b/src/test/java/net/kemitix/mon/TypeAliasTest.java
index 41c4222..08a5448 100644
--- a/src/test/java/net/kemitix/mon/TypeAliasTest.java
+++ b/src/test/java/net/kemitix/mon/TypeAliasTest.java
@@ -1,22 +1,29 @@
package net.kemitix.mon;
+import org.assertj.core.util.Strings;
import org.junit.Test;
+import java.util.Arrays;
import java.util.Collections;
-import java.util.function.Function;
+import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TypeAliasTest {
@Test
- public void shouldCreateATypeAliasAndGetTheValue() throws Exception {
+ public void shouldCreateATypeAliasAndGetTheValue() {
//given
final String value = "value";
//when
final TypeAlias typeAlias = givenTypeAlias(value);
//then
- assertThat(typeAlias.map(value::equals)).isTrue();
+ assertThat(typeAlias.getValue()).isSameAs(value);
+ }
+
+ private TypeAlias givenTypeAlias(final String value) {
+ return new TypeAlias(value) {
+ };
}
@Test
@@ -25,38 +32,34 @@ public class TypeAliasTest {
final Iterable iterable = Collections.emptyList();
//when
final TypeAlias> typeAlias =
- new TypeAlias>(iterable, Iterable.class) {
+ new TypeAlias>(iterable) {
};
//then
- assertThat(typeAlias.map(iterable::equals)).isTrue();
- }
-
- private TypeAlias givenTypeAlias(final String value) {
- return new TypeAlias(value, String.class) {
- };
+ assertThat(typeAlias.getValue()).isSameAs(iterable);
}
@Test
- public void shouldCreateAnAliasedTypeAndGetTheValue() throws Exception {
+ public void shouldCreateATypeAliasSubclassAndGetTheValue() {
//given
final String value = "value";
//when
final AnAlias anAlias = AnAlias.of(value);
//then
- assertThat(anAlias.map(value::equals)).isTrue();
+ assertThat(anAlias.getValue()).isSameAs(value);
}
@Test
public void shouldNotBeEqualWhenValueTypesAreDifferent() {
//given
final TypeAlias stringTypeAlias = givenTypeAlias("1");
- final TypeAlias integerTypeAlias = new TypeAlias(1, Integer.class){};
+ final TypeAlias integerTypeAlias = new TypeAlias(1) {
+ };
//then
assertThat(stringTypeAlias).isNotEqualTo(integerTypeAlias);
}
@Test
- public void shouldBeEqualWhenValuesAreTheSame() throws Exception {
+ public void shouldBeEqualWhenValuesAreTheSame() {
//given
final String value = "value";
final AnAlias anAlias1 = AnAlias.of(value);
@@ -66,7 +69,16 @@ public class TypeAliasTest {
}
@Test
- public void shouldBeEqualToUnAliasedValue() throws Exception {
+ public void shouldNotBeEqualWhenValuesAreNotTheSame() {
+ //given
+ final AnAlias valueA = AnAlias.of("value a");
+ final AnAlias valueB = AnAlias.of("value b");
+ //then
+ assertThat(valueA).isNotEqualTo(valueB);
+ }
+
+ @Test
+ public void shouldBeEqualToRawValue() {
//given
final String value = "value";
final AnAlias anAlias = AnAlias.of(value);
@@ -75,7 +87,7 @@ public class TypeAliasTest {
}
@Test
- public void shouldHaveHashCodeOfValue() throws Exception {
+ public void shouldHaveHashCodeOfValue() {
//given
final String value = "value";
final AnAlias anAlias = AnAlias.of(value);
@@ -84,24 +96,24 @@ public class TypeAliasTest {
}
@Test
- public void shouldHaveSameToStringAsAliasedType() throws Exception {
+ public void shouldHaveSameToStringAsAliasedType() {
//given
- final String value = "value";
+ final List value = Arrays.asList(1, 2, 3);
//when
- final AnAlias anAlias = AnAlias.of(value);
+ final TypeAlias> anAlias = new TypeAlias>(value) {
+ };
//then
- assertThat(anAlias.toString()).isEqualTo(value);
+ assertThat(anAlias.toString()).isEqualTo(value.toString());
}
@Test
public void shouldMapTypeAlias() {
//given
final AnAlias anAlias = AnAlias.of("text");
- final Function function = v -> v;
//when
- final String value = anAlias.map(function);
+ final String value = anAlias.map(Strings::quote);
//then
- assertThat(value).isEqualTo("text");
+ assertThat(value).isEqualTo("'text'");
}
private static class AnAlias extends TypeAlias {
@@ -112,7 +124,7 @@ public class TypeAliasTest {
* @param value the value
*/
protected AnAlias(final String value) {
- super(value, String.class);
+ super(value);
}
protected static AnAlias of(final String value) {