issue #335 review changes

This commit is contained in:
Crossy147 2016-02-21 12:10:08 +01:00
parent 80ff7bb217
commit 81e8d354a9
3 changed files with 20 additions and 4 deletions

View File

@ -17,7 +17,7 @@ together step by step. Binding functions can be described as passing one's outpu
basing on the 'same type' contract. Formally, monad consists of a type constructor M and two basing on the 'same type' contract. Formally, monad consists of a type constructor M and two
operations: operations:
bind - that takes monadic object and a function from plain object to monadic value and returns monadic value bind - that takes monadic object and a function from plain object to monadic value and returns monadic value
return - that takse plain type object and returns this object wrapped in a monadic value. return - that takes plain type object and returns this object wrapped in a monadic value.
![alt text](./etc/monad.png "Monad") ![alt text](./etc/monad.png "Monad")

View File

@ -1,7 +1,23 @@
package com.iluwatar.monad; package com.iluwatar.monad;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* The Monad pattern defines a monad structure, that enables chaining operations
* in pipelines and processing data step by step.
* Formally, monad consists of a type constructor M and two operations:
* <br>bind - that takes monadic object and a function from plain object to the
* monadic value and returns monadic value.
* <br>return - that takes plain type object and returns this object wrapped in a monadic value.
* <p>
* In the given example, the Monad pattern is represented as a {@link Validator} that takes an instance
* of a plain object with {@link Validator#of(Object)}
* and validates it {@link Validator#validate(Function, Predicate, String)} against given predicates.
* <p>As a validation result {@link Validator#get()} it either returns valid object {@link Validator#t}
* or throws a list of exceptions {@link Validator#exceptions} collected during validation.
*/
public class App { public class App {
/** /**

View File

@ -24,7 +24,7 @@ public class MonadTest {
public void testForInvalidAge() { public void testForInvalidAge() {
thrown.expect(IllegalStateException.class); thrown.expect(IllegalStateException.class);
User john = new User("John", 17, Sex.MALE, "john@qwe.bar"); User john = new User("John", 17, Sex.MALE, "john@qwe.bar");
Validator.of(tom).validate(User::getName, Objects::nonNull, "name cannot be null") Validator.of(john).validate(User::getName, Objects::nonNull, "name cannot be null")
.validate(User::getAge, age -> age > 21, "user is underaged") .validate(User::getAge, age -> age > 21, "user is underaged")
.get(); .get();
} }
@ -32,11 +32,11 @@ public class MonadTest {
@Test @Test
public void testForValid() { public void testForValid() {
User sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org"); User sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org");
User validated = Validator.of(tom).validate(User::getName, Objects::nonNull, "name cannot be null") User validated = Validator.of(sarah).validate(User::getName, Objects::nonNull, "name cannot be null")
.validate(User::getAge, age -> age > 21, "user is underaged") .validate(User::getAge, age -> age > 21, "user is underaged")
.validate(User::getSex, sex -> sex == Sex.FEMALE, "user is not female") .validate(User::getSex, sex -> sex == Sex.FEMALE, "user is not female")
.validate(User::getEmail, email -> email.contains("@"), "email does not contain @ sign") .validate(User::getEmail, email -> email.contains("@"), "email does not contain @ sign")
.get(); .get();
Assert.assertSame(validated, tom); Assert.assertSame(validated, sarah);
} }
} }