public class Validator extends Object
BulletConfig
. Use Validator.Entry
to define
fields and Validator.Relationship
to define relationships between them.
It also provides a bunch of useful Predicate
and BiPredicate
for use in the checks for the entries
and relationships and type converting Function
for converting types of entries.Modifier and Type | Class and Description |
---|---|
static class |
Validator.Entry
This represents a field in the Validator.
|
static class |
Validator.Relationship
This represents a binary relationship between two fields in a
BulletConfig . |
static class |
Validator.State
This represents a n-ary validation for the config.
|
Constructor and Description |
---|
Validator()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
static Predicate<Object> |
and(Predicate<Object>... predicates)
Creates a
Predicate that is true if and only if all the provided predicates are true and false otherwise. |
static Object |
asDouble(Object value)
|
static Object |
asFloat(Object value)
|
static Object |
asInt(Object value)
|
static Object |
asLong(Object value)
|
static Object |
asString(Object value)
This casts an Object to an
String . |
Validator |
copy()
Returns a copy of this validator.
|
Validator.Entry |
define(String key)
Creates an instance of the Entry using the name of the field.
|
Validator.State |
evaluate(String description,
String... keys)
Create a state with a description for the given fields.
|
static Predicate<Object> |
hasMaximumListSize(int n)
|
static Predicate<Object> |
hasMinimumListSize(int n)
|
static BiPredicate<Object,Object> |
ifTrueThenCheck(Predicate<Object> predicate)
Returns a
BiPredicate that checks to see if the first boolean argument implies the second
Predicate . |
static BiPredicate<Object,Object> |
isAtleastNTimes(double n)
Returns a
BiPredicate that checks to see if the first argument is at least the given times
more than the second. |
static boolean |
isBoolean(Object value)
Checks to see if the value is a
Boolean . |
static boolean |
isClassName(Object value)
Checks to see if the given object refers to a class name that can be loaded.
|
static boolean |
isFalse(Object value)
Checks to see if the value is false or not.
|
static boolean |
isFloat(Object value)
Checks to see if the value is an floating-point.
|
static boolean |
isGreaterOrEqual(Object first,
Object second)
Checks to see if the first numeric object is greater than or equal to the second numeric object.
|
static boolean |
isImplied(Object first,
Object second)
Checks to see if the first boolean object implies the second boolean object.
|
static BiPredicate<Object,Object> |
isImpliedBy(Predicate<Object> firstTest,
Predicate<Object> secondTest)
Returns a
BiPredicate that checks to see if the first Predicate implies the second. |
static <T> Predicate<Object> |
isIn(T... values)
Creates a
Predicate that checks to see if the given object is in the list of values. |
static <T extends Number> |
isInRange(T min,
T max)
|
static boolean |
isInt(Object value)
Checks to see if the value is an integer type.
|
static boolean |
isList(Object value)
Checks to see if the value is a
List . |
static <T> Predicate<Object> |
isListOfType(Class<T> type)
|
static boolean |
isMap(Object value)
Checks to see if the value is a
Map . |
static <K,V> Predicate<Object> |
isMapOfType(Class<K> keyType,
Class<V> valueType)
Checks to see if the value is a
Map of the given key and value type. |
static boolean |
isNonEmptyList(Object value)
Checks to see if the value is a non-empty
List . |
static boolean |
isNonEmptyMap(Object value)
Checks to see if the value is a non-empty
Map . |
static boolean |
isNotNull(Object value)
Checks to see if the value is null or not.
|
static boolean |
isNull(Object value)
Checks to see if the value is null or not.
|
static boolean |
isNumber(Object value)
Checks to see if the value is a
Number . |
static boolean |
isPositive(Object value)
Checks to see if the value was positive.
|
static boolean |
isPositiveInt(Object value)
|
static boolean |
isPowerOfTwo(Object value)
|
static boolean |
isString(Object value)
Checks to see if the value is a
String . |
static boolean |
isTrue(Object value)
Checks to see if the value is true or not.
|
static boolean |
isType(Object value,
Class clazz)
Checks to see if the value is of the provided type or not.
|
static Predicate<Object> |
not(Predicate<Object> predicate)
Creates a
Predicate that is true if the given predicate is false and false if the given predicate is true. |
static Predicate<Object> |
or(Predicate<Object>... predicates)
Creates a
Predicate that is true if any of the provided predicates are true and false otherwise. |
Validator.Relationship |
relate(String description,
String keyA,
String keyB)
Create a relationship with a description for it for the given fields.
|
void |
validate(BulletConfig config)
Validate and normalize the provided
BulletConfig for the defined entries and relationships. |
public Validator.Entry define(String key)
Predicate
unless you add a check using Validator.Entry.checkIf(Predicate)
.key
- The name of the field.Validator.Entry
.public Validator.Relationship relate(String description, String keyA, String keyB)
Validator.Relationship.checkIf(BiPredicate)
. By default,
if the relationship fails to hold, the defaults defined by the Entries for these fields will be used unless
you provide new ones using Validator.Relationship.orElseUse(Object, Object)
.description
- A string description of this relationship.keyA
- The first field in the relationship.keyB
- The second field in the relationship.Validator.Relationship
.public Validator.State evaluate(String description, String... keys)
Validator.State.checkIf(Predicate)
,
which provides you the values for the fields you are defining this for. Unless you ask the the check to fail with
Validator.State.orFail()
, it will use the defaults for each of the entries.description
- A string description for this state validation.keys
- The non-null fields for this state validation. They must already be defined as entries.Validator.State
.public void validate(BulletConfig config)
BulletConfig
for the defined entries and relationships. Then entries
are used to validate the config first.config
- The config containing fields to validate.public Validator copy()
Validator.Entry
and Validator.Relationship
.public static Object asInt(Object value)
value
- The value to cast.public static Object asLong(Object value)
value
- The value to cast.public static Object asFloat(Object value)
value
- The value to cast.public static Object asDouble(Object value)
value
- The value to cast.public static Object asString(Object value)
String
.value
- The value to cast.public static boolean isNotNull(Object value)
value
- The object to check.public static boolean isNull(Object value)
value
- The object to check.public static boolean isTrue(Object value)
value
- The object to check.public static boolean isFalse(Object value)
value
- The object to check.public static boolean isType(Object value, Class clazz)
value
- The object to check type for.clazz
- The supposed class of the value.public static boolean isBoolean(Object value)
Boolean
.value
- The object to check.public static boolean isString(Object value)
String
.value
- The object to check.public static boolean isList(Object value)
List
.value
- The object to check.public static boolean isMap(Object value)
Map
.value
- The object to check.public static boolean isNumber(Object value)
Number
.value
- The object to check.public static boolean isInt(Object value)
value
- The object to check.public static boolean isFloat(Object value)
value
- The object to check.public static boolean isPositive(Object value)
value
- The object to check.public static boolean isPositiveInt(Object value)
value
- The object to check.public static boolean isPowerOfTwo(Object value)
value
- The object to check.public static boolean isNonEmptyList(Object value)
List
.value
- The object to check.public static boolean isNonEmptyMap(Object value)
Map
.value
- The object to check.public static boolean isClassName(Object value)
value
- The object to check if it is a class name.public static <T> Predicate<Object> isIn(T... values)
Predicate
that checks to see if the given object is in the list of values.T
- The type of the values and the object.values
- The values that the object could be equal to that is being tested.public static Predicate<Object> hasMinimumListSize(int n)
Predicate
that checks to see if the given object is a List
and has at least n items
in it. Note that the object must be a List even if n is 0.n
- The minimum number of items that can be in the List.List
has a size of at least the given parameter.public static Predicate<Object> hasMaximumListSize(int n)
Predicate
that checks to see if the given object is a List
and has at most n items
in it. Note that the object must be a List even if n is 0.n
- The maximum number of items that can be in the List.List
has a size of at most the given parameter.public static <T extends Number> Predicate<Object> isInRange(T min, T max)
T
- The type of the value, min, and max.min
- The smallest this number value could be.max
- The largest this number value could be.public static <T> Predicate<Object> isListOfType(Class<T> type)
T
- The type of the content in the list.type
- The class of the contents of the list to check for.public static <K,V> Predicate<Object> isMapOfType(Class<K> keyType, Class<V> valueType)
Map
of the given key and value type.K
- The type of the key in the map.V
- The type of the value in the map.keyType
- The class of the key of the map to check for.valueType
- The class of the value of the map to check for.@SafeVarargs public static Predicate<Object> and(Predicate<Object>... predicates)
Predicate
that is true if and only if all the provided predicates are true and false otherwise.predicates
- The predicates to be ANDed.@SafeVarargs public static Predicate<Object> or(Predicate<Object>... predicates)
Predicate
that is true if any of the provided predicates are true and false otherwise.predicates
- The predicates to be ORed.public static Predicate<Object> not(Predicate<Object> predicate)
Predicate
that is true if the given predicate is false and false if the given predicate is true.predicate
- The predicates to be negated.public static boolean isGreaterOrEqual(Object first, Object second)
first
- The first numeric object.second
- The second numeric object.public static boolean isImplied(Object first, Object second)
first
- The first boolean object.second
- The second boolean object.public static BiPredicate<Object,Object> isAtleastNTimes(double n)
BiPredicate
that checks to see if the first argument is at least the given times
more than the second.n
- The number of times the second argument must be smaller than the first.BiPredicate
.public static BiPredicate<Object,Object> ifTrueThenCheck(Predicate<Object> predicate)
BiPredicate
that checks to see if the first boolean argument implies the second
Predicate
. In other words, if the first argument is true and the second argument evaluates to false
on the given Predicate
, this check is false. Else, it is true.predicate
- The Predicate
to test the second argument to the returned BiPredicate
with.BiPredicate
.public static BiPredicate<Object,Object> isImpliedBy(Predicate<Object> firstTest, Predicate<Object> secondTest)
BiPredicate
that checks to see if the first Predicate
implies the second. In other
words, if the first one evaluates to true on the first object in the BiPredicate and the second evaluates to
false on the second object, this check is false. Else, it is true. Analogous to
isImplied(Object, Object)
.firstTest
- The first Predicate
.secondTest
- The second Predicate
.BiPredicate
.Copyright © 2021. All rights reserved.