Java 11 migraiton: monad
This commit is contained in:
parent
f1b27ef5c7
commit
a142c06048
@ -41,9 +41,8 @@ import org.slf4j.LoggerFactory;
|
||||
* 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.
|
||||
* <p>As a validation result {@link Validator#get()} either returns valid object
|
||||
* or throws {@link IllegalStateException} with list of exceptions collected during validation.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
@ -55,10 +54,10 @@ public class App {
|
||||
* @param args command line 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")
|
||||
.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()
|
||||
.toString());
|
||||
}
|
||||
|
@ -28,10 +28,10 @@ package com.iluwatar.monad;
|
||||
*/
|
||||
public class User {
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
private Sex sex;
|
||||
private String email;
|
||||
private final String name;
|
||||
private final int age;
|
||||
private final Sex sex;
|
||||
private final String email;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
|
@ -85,18 +85,21 @@ public class Validator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension for the {@link Validator#validate(Function, Predicate, String)} method, dedicated for
|
||||
* objects, that need to be projected before requested validation.
|
||||
* Extension for the {@link Validator#validate(Predicate, String)} method, dedicated for objects,
|
||||
* that need to be projected before requested validation.
|
||||
*
|
||||
* @param projection function that gets an objects, and returns projection representing element to
|
||||
* be validated.
|
||||
* @param validation see {@link Validator#validate(Function, Predicate, String)}
|
||||
* @param message see {@link Validator#validate(Function, Predicate, String)}
|
||||
* @param <U> see {@link Validator#validate(Function, Predicate, String)}
|
||||
* @param validation see {@link Validator#validate(Predicate, String)}
|
||||
* @param message see {@link Validator#validate(Predicate, String)}
|
||||
* @param <U> see {@link Validator#validate(Predicate, String)}
|
||||
* @return this
|
||||
*/
|
||||
public <U> Validator<T> validate(Function<T, U> projection, Predicate<U> validation,
|
||||
String message) {
|
||||
public <U> Validator<T> validate(
|
||||
Function<T, U> projection,
|
||||
Predicate<U> validation,
|
||||
String message
|
||||
) {
|
||||
return validate(projection.andThen(validation::test)::apply, message);
|
||||
}
|
||||
|
||||
@ -110,7 +113,7 @@ public class Validator<T> {
|
||||
if (exceptions.isEmpty()) {
|
||||
return obj;
|
||||
}
|
||||
IllegalStateException e = new IllegalStateException();
|
||||
var e = new IllegalStateException();
|
||||
exceptions.forEach(e::addSuppressed);
|
||||
throw e;
|
||||
}
|
||||
|
@ -32,8 +32,7 @@ public class AppTest {
|
||||
|
||||
@Test
|
||||
public void testMain() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,13 +23,12 @@
|
||||
|
||||
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.assertThrows;
|
||||
|
||||
import java.util.Objects;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test for Monad Pattern
|
||||
*/
|
||||
@ -37,27 +36,33 @@ public class MonadTest {
|
||||
|
||||
@Test
|
||||
public void testForInvalidName() {
|
||||
User tom = new User(null, 21, Sex.MALE, "tom@foo.bar");
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
Validator.of(tom).validate(User::getName, Objects::nonNull, "name cannot be null").get();
|
||||
});
|
||||
var tom = new User(null, 21, Sex.MALE, "tom@foo.bar");
|
||||
assertThrows(
|
||||
IllegalStateException.class,
|
||||
() -> Validator.of(tom)
|
||||
.validate(User::getName, Objects::nonNull, "name cannot be null")
|
||||
.get()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForInvalidAge() {
|
||||
User john = new User("John", 17, Sex.MALE, "john@qwe.bar");
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
Validator.of(john).validate(User::getName, Objects::nonNull, "name cannot be null")
|
||||
.validate(User::getAge, age -> age > 21, "user is underaged")
|
||||
.get();
|
||||
});
|
||||
var john = new User("John", 17, Sex.MALE, "john@qwe.bar");
|
||||
assertThrows(
|
||||
IllegalStateException.class,
|
||||
() -> Validator.of(john)
|
||||
.validate(User::getName, Objects::nonNull, "name cannot be null")
|
||||
.validate(User::getAge, age -> age > 21, "user is underage")
|
||||
.get()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForValid() {
|
||||
User sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org");
|
||||
User validated = Validator.of(sarah).validate(User::getName, Objects::nonNull, "name cannot be null")
|
||||
.validate(User::getAge, age -> age > 21, "user is underaged")
|
||||
var sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org");
|
||||
var validated = Validator.of(sarah)
|
||||
.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::getEmail, email -> email.contains("@"), "email does not contain @ sign")
|
||||
.get();
|
||||
|
Loading…
x
Reference in New Issue
Block a user