Resolves checkstyle errors for remaining m (#1090)
* Reduces checkstyle errors in marker * Reduces checkstyle errors in master-worker-pattern * Reduces checkstyle errors in mediator * Reduces checkstyle errors in memento * Reduces checkstyle errors in model-view-controller * Reduces checkstyle errors in model-view-presenter * Reduces checkstyle errors in module * Reduces checkstyle errors in monad * Reduces checkstyle errors in monostate * Reduces checkstyle errors in multiton * Reduces checkstyle errors in mute-idiom * Reduces checkstyle errors in mutex
This commit is contained in:
committed by
Ilkka Seppälä
parent
3ccc9baa1a
commit
1fdc650545
@ -23,26 +23,27 @@
|
||||
|
||||
package com.iluwatar.monad;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 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:
|
||||
* 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.
|
||||
*
|
||||
* <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 {
|
||||
|
||||
@ -58,6 +59,7 @@ public class App {
|
||||
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::getAge, age -> age > 20 && age < 30, "age isn't between...").get().toString());
|
||||
.validate(User::getAge, age -> age > 20 && age < 30, "age isn't between...").get()
|
||||
.toString());
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@
|
||||
package com.iluwatar.monad;
|
||||
|
||||
/**
|
||||
* Enumeration of Types of Sex
|
||||
* Enumeration of Types of Sex.
|
||||
*/
|
||||
public enum Sex {
|
||||
MALE, FEMALE
|
||||
|
@ -24,7 +24,7 @@
|
||||
package com.iluwatar.monad;
|
||||
|
||||
/**
|
||||
* User Definition
|
||||
* User Definition.
|
||||
*/
|
||||
public class User {
|
||||
|
||||
@ -34,6 +34,8 @@ public class User {
|
||||
private String email;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param name - name
|
||||
* @param age - age
|
||||
* @param sex - sex
|
||||
|
@ -30,18 +30,18 @@ import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Class representing Monad design pattern. Monad is a way of chaining operations on the
|
||||
* given object together step by step. In Validator each step results in either success or
|
||||
* failure indicator, giving a way of receiving each of them easily and finally getting
|
||||
* validated object or list of exceptions.
|
||||
* Class representing Monad design pattern. Monad is a way of chaining operations on the given
|
||||
* object together step by step. In Validator each step results in either success or failure
|
||||
* indicator, giving a way of receiving each of them easily and finally getting validated object or
|
||||
* list of exceptions.
|
||||
*
|
||||
* @param <T> Placeholder for an object.
|
||||
*/
|
||||
public class Validator<T> {
|
||||
/**
|
||||
* Object that is validated
|
||||
* Object that is validated.
|
||||
*/
|
||||
private final T t;
|
||||
private final T obj;
|
||||
|
||||
/**
|
||||
* List of exception thrown during validation.
|
||||
@ -50,14 +50,15 @@ public class Validator<T> {
|
||||
|
||||
/**
|
||||
* Creates a monadic value of given object.
|
||||
* @param t object to be validated
|
||||
*
|
||||
* @param obj object to be validated
|
||||
*/
|
||||
private Validator(T t) {
|
||||
this.t = t;
|
||||
private Validator(T obj) {
|
||||
this.obj = obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates validator against given object
|
||||
* Creates validator against given object.
|
||||
*
|
||||
* @param t object to be validated
|
||||
* @param <T> object's type
|
||||
@ -68,25 +69,27 @@ public class Validator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param validation one argument boolean-valued function that
|
||||
* represents one step of validation. Adds exception to main validation exception
|
||||
* list when single step validation ends with failure.
|
||||
* Checks if the validation is successful.
|
||||
*
|
||||
* @param validation one argument boolean-valued function that represents one step of validation.
|
||||
* Adds exception to main validation exception list when single step validation
|
||||
* ends with failure.
|
||||
* @param message error message when object is invalid
|
||||
* @return this
|
||||
*/
|
||||
public Validator<T> validate(Predicate<T> validation, String message) {
|
||||
if (!validation.test(t)) {
|
||||
if (!validation.test(obj)) {
|
||||
exceptions.add(new IllegalStateException(message));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(Function, 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 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)}
|
||||
@ -105,7 +108,7 @@ public class Validator<T> {
|
||||
*/
|
||||
public T get() throws IllegalStateException {
|
||||
if (exceptions.isEmpty()) {
|
||||
return t;
|
||||
return obj;
|
||||
}
|
||||
IllegalStateException e = new IllegalStateException();
|
||||
exceptions.forEach(e::addSuppressed);
|
||||
|
Reference in New Issue
Block a user