Class FieldRejection<T>
- java.lang.Object
-
- org.hawaiiframework.validation.field.FieldRejection<T>
-
- Type Parameters:
T
- The type of the value to evaluate.
public class FieldRejection<T> extends Object
Reject a field based on a few conditions.For instance:
new FieldRejection(validationResult, "houseNumber", "13-a") .whenNull() .orWhen(h -> h.contains("a")) .orWhen(h -> h.length() > 10);
If used with the ValidationResult class this will look like:
validationResult.rejectField("houseNumber", "13-a") .whenNull() .orWhen(h -> h.contains("a')) .orWhen(h -> h.length() > 10);
The rejections without
code
parameters have the valueinvalid
, except thewhenNull()
, this has therequired
code value.The chain will stop evaluating the rejection clauses after the first matching clause. In the examples above the chain will not evaluate the length of the house number.
- Author:
- Rutger Lubbers
-
-
Constructor Summary
Constructors Constructor Description FieldRejection(ValidationResult validationResult, String field, T actual)
Construct a new field rejection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FieldRejection<T>
or()
Syntactic sugar, allows the use ofor().when(...).or().when(...)
syntaxis.<R> FieldRejection<T>
or(Function<T,R> function, Matcher<R> matcher)
Rejects the field when thematcher
matches the result of thefunction
.<R> FieldRejection<T>
or(Function<T,R> function, Matcher<R> matcher, String code)
Rejects the field when thematcher
matches the result of thefunction
.FieldRejection<T>
or(Predicate<T> predicate)
Rejects the field when thepredicate
evaluates totrue
.FieldRejection<T>
or(Predicate<T> predicate, String code)
Rejects the field when thepredicate
evaluates totrue
.FieldRejection<T>
or(Matcher<T> matcher)
Rejects the field when thematcher
matches theactual
value.FieldRejection<T>
or(Matcher<T> matcher, String code)
Rejects the field when thematcher
matches theactual
value.<R> FieldRejection<T>
orWhen(Function<T,R> function, Matcher<R> matcher)
Rejects the field when thematcher
matches the result of thefunction
.<R> FieldRejection<T>
orWhen(Function<T,R> function, Matcher<R> matcher, String code)
Rejects the field when thematcher
matches the result of thefunction
.FieldRejection<T>
orWhen(Predicate<T> predicate)
Rejects the field when thepredicate
evaluates totrue
.FieldRejection<T>
orWhen(Predicate<T> predicate, String code)
Rejects the field when thepredicate
evaluates totrue
.FieldRejection<T>
orWhen(Matcher<T> matcher)
Rejects the field when thematcher
matches theactual
value.FieldRejection<T>
orWhen(Matcher<T> matcher, String code)
Rejects the field when thematcher
matches theactual
value.<R> FieldRejection<T>
when(Function<T,R> function, Matcher<R> matcher)
Rejects the field when thematcher
matches the result of thefunction
.<R> FieldRejection<T>
when(Function<T,R> function, Matcher<R> matcher, String code)
Rejects the field when thematcher
matches the result of thefunction
.FieldRejection<T>
when(Predicate<T> predicate)
Rejects the field when thepredicate
evaluates totrue
.FieldRejection<T>
when(Predicate<T> predicate, String code)
Rejects the field when thepredicate
evaluates totrue
.FieldRejection<T>
when(Matcher<T> matcher)
Rejects the field when thematcher
matches theactual
value.FieldRejection<T>
when(Matcher<T> matcher, String code)
Rejects the field when thematcher
matches theactual
value.FieldRejection<T>
whenNull()
Rejects the field when theactual
isnull
.FieldRejection<T>
whenNull(String code)
Rejects the field when theactual
isnull
.
-
-
-
Field Detail
-
REQUIRED
public static final String REQUIRED
- See Also:
- Constant Field Values
-
INVALID
public static final String INVALID
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
FieldRejection
public FieldRejection(ValidationResult validationResult, String field, T actual)
Construct a new field rejection.- Parameters:
validationResult
- The validation result.field
- The field name (property name).actual
- The field's value.
-
-
Method Detail
-
or
public FieldRejection<T> or()
Syntactic sugar, allows the use ofor().when(...).or().when(...)
syntaxis.- Returns:
- The current field rejection.
-
or
public FieldRejection<T> or(Matcher<T> matcher)
Rejects the field when thematcher
matches theactual
value. This will reject the field with the error codeinvalid
.- Parameters:
matcher
- The matcher to use.- Returns:
- The current field rejection.
-
or
public FieldRejection<T> or(Matcher<T> matcher, String code)
Rejects the field when thematcher
matches theactual
value. This will reject the field with supplied the error codecode
.- Parameters:
matcher
- The matcher to use.- Returns:
- The current field rejection.
-
or
public FieldRejection<T> or(Predicate<T> predicate)
Rejects the field when thepredicate
evaluates totrue
. This will reject the field with the error codeinvalid
.- Parameters:
predicate
- The predicate to use.- Returns:
- The current field rejection.
-
or
public FieldRejection<T> or(Predicate<T> predicate, String code)
Rejects the field when thepredicate
evaluates totrue
. This will reject the field with supplied the error codecode
.- Parameters:
predicate
- The predicate to use.- Returns:
- The current field rejection.
-
or
public <R> FieldRejection<T> or(Function<T,R> function, Matcher<R> matcher)
Rejects the field when thematcher
matches the result of thefunction
. This will reject the field with the error codeinvalid
.- Parameters:
function
- The function to apply to theactual
value.matcher
- The matcher to use against the return value of thefunction
.- Returns:
- The current field rejection.
-
or
public <R> FieldRejection<T> or(Function<T,R> function, Matcher<R> matcher, String code)
Rejects the field when thematcher
matches the result of thefunction
. This will reject the field with supplied the error codecode
.- Parameters:
function
- The function to apply to theactual
value.matcher
- The matcher to use against the return value of thefunction
.- Returns:
- The current field rejection.
-
whenNull
public FieldRejection<T> whenNull()
Rejects the field when theactual
isnull
. This will reject the field with the error coderequired
.- Returns:
- The current field rejection.
-
whenNull
public FieldRejection<T> whenNull(String code)
Rejects the field when theactual
isnull
. This will reject the field with supplied the error codecode
.- Parameters:
code
- The error code to set if the actual value isnull
.- Returns:
- The current field rejection.
-
when
public FieldRejection<T> when(Matcher<T> matcher)
Rejects the field when thematcher
matches theactual
value. This will reject the field with the error codeinvalid
.- Parameters:
matcher
- The matcher to use.- Returns:
- The current field rejection.
-
when
public FieldRejection<T> when(Matcher<T> matcher, String code)
Rejects the field when thematcher
matches theactual
value. This will reject the field with supplied the error codecode
.- Parameters:
matcher
- The matcher to use.- Returns:
- The current field rejection.
-
when
public FieldRejection<T> when(Predicate<T> predicate)
Rejects the field when thepredicate
evaluates totrue
. This will reject the field with the error codeinvalid
.- Parameters:
predicate
- The predicate to use.- Returns:
- The current field rejection.
-
when
public <R> FieldRejection<T> when(Function<T,R> function, Matcher<R> matcher)
Rejects the field when thematcher
matches the result of thefunction
. This will reject the field with the error codeinvalid
.- Parameters:
function
- The function to apply to theactual
value.matcher
- The matcher to use against the return value of thefunction
.- Returns:
- The current field rejection.
-
when
public <R> FieldRejection<T> when(Function<T,R> function, Matcher<R> matcher, String code)
Rejects the field when thematcher
matches the result of thefunction
. This will reject the field with supplied the error codecode
.- Parameters:
function
- The function to apply to theactual
value.matcher
- The matcher to use against the return value of thefunction
.- Returns:
- The current field rejection.
-
when
public FieldRejection<T> when(Predicate<T> predicate, String code)
Rejects the field when thepredicate
evaluates totrue
. This will reject the field with supplied the error codecode
.- Parameters:
predicate
- The predicate to use.- Returns:
- The current field rejection.
-
orWhen
public FieldRejection<T> orWhen(Matcher<T> matcher)
Rejects the field when thematcher
matches theactual
value. This will reject the field with the error codeinvalid
.- Parameters:
matcher
- The matcher to use.- Returns:
- The current field rejection.
-
orWhen
public FieldRejection<T> orWhen(Matcher<T> matcher, String code)
Rejects the field when thematcher
matches theactual
value. This will reject the field with supplied the error codecode
.- Parameters:
matcher
- The matcher to use.- Returns:
- The current field rejection.
-
orWhen
public FieldRejection<T> orWhen(Predicate<T> predicate)
Rejects the field when thepredicate
evaluates totrue
. This will reject the field with the error codeinvalid
.- Parameters:
predicate
- The predicate to use.- Returns:
- The current field rejection.
-
orWhen
public FieldRejection<T> orWhen(Predicate<T> predicate, String code)
Rejects the field when thepredicate
evaluates totrue
. This will reject the field with supplied the error codecode
.- Parameters:
predicate
- The predicate to use.- Returns:
- The current field rejection.
-
orWhen
public <R> FieldRejection<T> orWhen(Function<T,R> function, Matcher<R> matcher)
Rejects the field when thematcher
matches the result of thefunction
. This will reject the field with the error codeinvalid
.- Parameters:
function
- The function to apply to theactual
value.matcher
- The matcher to use against the return value of thefunction
.- Returns:
- The current field rejection.
-
orWhen
public <R> FieldRejection<T> orWhen(Function<T,R> function, Matcher<R> matcher, String code)
Rejects the field when thematcher
matches the result of thefunction
. This will reject the field with supplied the error codecode
.- Parameters:
function
- The function to apply to theactual
value.matcher
- The matcher to use against the return value of thefunction
.- Returns:
- The current field rejection.
-
-