Java 11 migraiton: monad

This commit is contained in:
Anurag Agarwal 2020-04-12 22:45:54 +00:00
parent f1b27ef5c7
commit a142c06048
No known key found for this signature in database
GPG Key ID: CF5E14552DA23F13
5 changed files with 42 additions and 36 deletions

View File

@ -41,9 +41,8 @@ import org.slf4j.LoggerFactory;
* instance of a plain object with {@link Validator#of(Object)} and validates it {@link * instance of a plain object with {@link Validator#of(Object)} and validates it {@link
* Validator#validate(Function, Predicate, String)} against given predicates. * Validator#validate(Function, Predicate, String)} against given predicates.
* *
* <p>As a validation result {@link Validator#get()} it either returns valid object {@link * <p>As a validation result {@link Validator#get()} either returns valid object
* Validator#t} or throws a list of exceptions {@link Validator#exceptions} collected during * or throws {@link IllegalStateException} with list of exceptions collected during validation.
* validation.
*/ */
public class App { public class App {
@ -55,10 +54,10 @@ public class App {
* @param args command line args * @param args command line args
*/ */
public static void main(String[] args) { public static void main(String[] args) {
User user = new User("user", 24, Sex.FEMALE, "foobar.com"); var user = new User("user", 24, Sex.FEMALE, "foobar.com");
LOGGER.info(Validator.of(user).validate(User::getName, Objects::nonNull, "name is null") LOGGER.info(Validator.of(user).validate(User::getName, Objects::nonNull, "name is null")
.validate(User::getName, name -> !name.isEmpty(), "name is empty") .validate(User::getName, name -> !name.isEmpty(), "name is empty")
.validate(User::getEmail, email -> !email.contains("@"), "email doesn't containt '@'") .validate(User::getEmail, email -> !email.contains("@"), "email doesn't contains '@'")
.validate(User::getAge, age -> age > 20 && age < 30, "age isn't between...").get() .validate(User::getAge, age -> age > 20 && age < 30, "age isn't between...").get()
.toString()); .toString());
} }

View File

@ -28,10 +28,10 @@ package com.iluwatar.monad;
*/ */
public class User { public class User {
private String name; private final String name;
private int age; private final int age;
private Sex sex; private final Sex sex;
private String email; private final String email;
/** /**
* Constructor. * Constructor.

View File

@ -85,18 +85,21 @@ public class Validator<T> {
} }
/** /**
* Extension for the {@link Validator#validate(Function, Predicate, String)} method, dedicated for * Extension for the {@link Validator#validate(Predicate, String)} method, dedicated for objects,
* objects, that need to be projected before requested validation. * that need to be projected before requested validation.
* *
* @param projection function that gets an objects, and returns projection representing element to * @param projection function that gets an objects, and returns projection representing element to
* be validated. * be validated.
* @param validation see {@link Validator#validate(Function, Predicate, String)} * @param validation see {@link Validator#validate(Predicate, String)}
* @param message see {@link Validator#validate(Function, Predicate, String)} * @param message see {@link Validator#validate(Predicate, String)}
* @param <U> see {@link Validator#validate(Function, Predicate, String)} * @param <U> see {@link Validator#validate(Predicate, String)}
* @return this * @return this
*/ */
public <U> Validator<T> validate(Function<T, U> projection, Predicate<U> validation, public <U> Validator<T> validate(
String message) { Function<T, U> projection,
Predicate<U> validation,
String message
) {
return validate(projection.andThen(validation::test)::apply, message); return validate(projection.andThen(validation::test)::apply, message);
} }
@ -110,7 +113,7 @@ public class Validator<T> {
if (exceptions.isEmpty()) { if (exceptions.isEmpty()) {
return obj; return obj;
} }
IllegalStateException e = new IllegalStateException(); var e = new IllegalStateException();
exceptions.forEach(e::addSuppressed); exceptions.forEach(e::addSuppressed);
throw e; throw e;
} }

View File

@ -32,8 +32,7 @@ public class AppTest {
@Test @Test
public void testMain() { public void testMain() {
String[] args = {}; App.main(new String[]{});
App.main(args);
} }
} }

View File

@ -23,13 +23,12 @@
package com.iluwatar.monad; package com.iluwatar.monad;
import org.junit.jupiter.api.Test;
import java.util.Objects;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Objects;
import org.junit.jupiter.api.Test;
/** /**
* Test for Monad Pattern * Test for Monad Pattern
*/ */
@ -37,27 +36,33 @@ public class MonadTest {
@Test @Test
public void testForInvalidName() { public void testForInvalidName() {
User tom = new User(null, 21, Sex.MALE, "tom@foo.bar"); var tom = new User(null, 21, Sex.MALE, "tom@foo.bar");
assertThrows(IllegalStateException.class, () -> { assertThrows(
Validator.of(tom).validate(User::getName, Objects::nonNull, "name cannot be null").get(); IllegalStateException.class,
}); () -> Validator.of(tom)
.validate(User::getName, Objects::nonNull, "name cannot be null")
.get()
);
} }
@Test @Test
public void testForInvalidAge() { public void testForInvalidAge() {
User john = new User("John", 17, Sex.MALE, "john@qwe.bar"); var john = new User("John", 17, Sex.MALE, "john@qwe.bar");
assertThrows(IllegalStateException.class, () -> { assertThrows(
Validator.of(john).validate(User::getName, Objects::nonNull, "name cannot be null") IllegalStateException.class,
.validate(User::getAge, age -> age > 21, "user is underaged") () -> Validator.of(john)
.get(); .validate(User::getName, Objects::nonNull, "name cannot be null")
}); .validate(User::getAge, age -> age > 21, "user is underage")
.get()
);
} }
@Test @Test
public void testForValid() { public void testForValid() {
User sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org"); var sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org");
User validated = Validator.of(sarah).validate(User::getName, Objects::nonNull, "name cannot be null") var validated = Validator.of(sarah)
.validate(User::getAge, age -> age > 21, "user is underaged") .validate(User::getName, Objects::nonNull, "name cannot be null")
.validate(User::getAge, age -> age > 21, "user is underage")
.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();