Class OptionalType
Object container with automatic conversion to required
type when value is called.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final OptionalTypeStatic object representation of empty OptionalType.static final com.google.gson.GsonGsonpublic instance with default configuration. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> TGet actual value as required class type using Gson deserializer.<T> TGet actual value as specific type using Gson deserializer.<T> T[]asArray(T[] array, Function<OptionalType, T> function) Get actual value as array using function.Get the actual value as Boolean or null if the conversion fails.Get the actual value as Boolean or default value instead if the conversion fails.asByte()Get the actual value as Byte or null if the conversion fails.Get the actual value as Byte or default value instead if the conversion fails.asChar()Get the actual value as Character or null if the conversion fails.Get the actual value as Character or default value instead if the conversion fails.<T, C extends Collection<T>>
CasCollection(C collection, Function<OptionalType, T> function) Apply actual value to collection using function.asDouble()Get the actual value as Double or null if the conversion fails.Get the actual value as Double or default value instead if the conversion fails.<E> Set<E> asElementSet(Function<Integer, E> element, int size) Get the actual value as element type Set.
This method only works if the actual value is a bitField.<E extends Enum<E>>
EGet the actual value as Enum type.
This method only works if the actual value is a String or Number.<E extends Enum<E>>
EGet the actual value as Enum type.
This method only works if the actual value is a String or Number.Get the actual value as Enum type Set.
This method only works if the actual value is a bitField.asFloat()Get the actual value as Float or null if the conversion fails.Get the actual value as Float or default value instead if the conversion fails.asInt()Get the actual value as Integer or null if the conversion fails.Get the actual value as Integer or default value instead if the conversion fails.<T> List<T> asList(Function<OptionalType, T> function) Get actual value as list using function.asLong()Get the actual value as Long or null if the conversion fails.Get the actual value as Long or default value instead if the conversion fails.asObject()Get the actual value as Object or null.Get the actual value as Object or default value instead.asOrdinalSet(int size) Get the actual value as ordinal-value Set.
This method only works if the actual value is a bitField.asShort()Get the actual value as Short or null if the conversion fails.Get the actual value as Short or default value instead if the conversion fails.asString()Get the actual value as String or null.
This method only convert the actual value to String if it isn't null.Get the actual value as String if it isn't null or default value instead.asUuid()Get the actual value as UUID or null if the conversion fails.Get the actual value as UUID or default value instead if the conversion fails.<T> Tby(ThrowableFunction<Object, T> function) Get actual value by function.<T> Tby(ThrowableFunction<Object, T> function, T def) Get actual value by function with defined default value.<T> Tby(Class<T> type, ThrowableFunction<Object, T> function, T def) Get actual value as type or by function with defined default value.static <T> TCast any object to required type.
Take in count this method may produceClassCastExceptionif you want to cast inconvertible types.voidclear()Clear the current object, only affectCollectionandMaptypes.voidclear(boolean deep) Clear the current object, only affectCollectionandMaptypes.booleanfirst()Get the first value.
This method extract the first value if the current object is a collection or array.protected ObjectGet the object that can be iterated.inthashCode()booleanisEmpty()Check if the existence of current value.booleanisInstance(Class<?> clazz) Check if the current value is instance of defined class.booleanSame hasisEmpty()but with inverted result.booleanisNotInstance(Class<?> clazz) Same hasisInstance(Class)but with inverted result.static OptionalTypeGet current OptionalType from object value.<T> TGet actual value converted to required type class or default defined value.<T> Tor(T def) Get actual value converted to required type or default defined value.protected voidsetIterable(Object value) Set the object that can be iterated.single()Get actual value as single object
This method make a recursively extract from the first value if the current object is a collection or array.<T> Tvalue()Get actual value converted to required type.<T> TGet actual value converted to required type class.Methods inherited from class IterableType
isIterable, isNotIterable, iteratorMethods inherited from class Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
EMPTY
Static object representation of empty OptionalType. -
GSON
public static final com.google.gson.Gson GSONGsonpublic instance with default configuration.
-
-
Constructor Details
-
OptionalType
Constructs an OptionalType with specified value.- Parameters:
value- Saved value, can be null.
-
-
Method Details
-
of
Get current OptionalType from object value.- Parameters:
value- Saved value, can be null.- Returns:
- An OptionalType with object value.
-
cast
Cast any object to required type.
Take in count this method may produceClassCastExceptionif you want to cast inconvertible types.- Type Parameters:
T- Required type to cast the object.- Parameters:
object- Object to cast.- Returns:
- The object as required type or null;
-
getIterable
Description copied from class:IterableTypeGet the object that can be iterated.- Specified by:
getIterablein classIterableType<Object>- Returns:
- an iterable object.
-
setIterable
Description copied from class:IterableTypeSet the object that can be iterated.- Specified by:
setIterablein classIterableType<Object>- Parameters:
value- an iterable object.
-
isEmpty
public boolean isEmpty()Check if the existence of current value.- Returns:
- true if current value is null.
-
isNotEmpty
public boolean isNotEmpty()Same hasisEmpty()but with inverted result.- Returns:
- true if current value is not null.
-
isInstance
Check if the current value is instance of defined class.- Parameters:
clazz- Class type.- Returns:
- true if value is instance of class.
-
isNotInstance
Same hasisInstance(Class)but with inverted result.- Parameters:
clazz- Class type.- Returns:
- true if value is not instance of class.
-
first
Get the first value.
This method extract the first value if the current object is a collection or array.- Returns:
- An OptionalType with object value.
-
single
Get actual value as single object
This method make a recursively extract from the first value if the current object is a collection or array.- Returns:
- An OptionalType with object value.
-
value
public <T> T value()Get actual value converted to required type.- Type Parameters:
T- Type to cast.- Returns:
- Actual value or null if cast fails.
-
value
Get actual value converted to required type class.- Type Parameters:
T- Type to cast.- Parameters:
clazz- Class to check instance of.- Returns:
- Actual value or null if cast fails.
-
clear
public void clear()Clear the current object, only affectCollectionandMaptypes. -
clear
public void clear(boolean deep) Clear the current object, only affectCollectionandMaptypes.- Parameters:
deep- true to clear the inner objects.
-
equals
-
hashCode
-
or
public <T> T or(T def) Get actual value converted to required type or default defined value.- Type Parameters:
T- Type to cast.- Parameters:
def- Default value if cast fails.- Returns:
- Actual value or default if cast fails.
-
or
Get actual value converted to required type class or default defined value.- Type Parameters:
T- Type to cast.- Parameters:
clazz- Class to check instance of.def- Default value if cast fails.- Returns:
- Actual value or default if cast fails.
-
by
Get actual value by function.- Type Parameters:
T- The required function result type.- Parameters:
function- Function to process the value.- Returns:
- The function result.
-
by
Get actual value by function with defined default value.- Type Parameters:
T- The required result type.- Parameters:
function- Function to process the actual non-null value.def- Defined default value if the actual or final value is null.- Returns:
- The function result or default value if the function return null object or throws exception.
-
by
Get actual value as type or by function with defined default value.- Type Parameters:
T- The required result type.- Parameters:
type- The class type to match the value.function- Function to process the actual non-null value.def- Defined default value if the actual or final value is null.- Returns:
- A value as required type or default value if the function return null object or throws exception.
-
as
Get actual value as specific type using Gson deserializer.- Type Parameters:
T- The required type.- Parameters:
type- The specific type.- Returns:
- The value as required type or null if deserialization fails.
-
as
Get actual value as required class type using Gson deserializer.- Type Parameters:
T- The required type.- Parameters:
type- The class of type.- Returns:
- The value as required type or null if deserialization fails.
-
asArray
Get actual value as array using function.- Type Parameters:
T- The required type.- Parameters:
array- The array to fill with objects.function- Function to convert the value or any element inside value to required type.- Returns:
- The value as required array type.
-
asList
Get actual value as list using function.- Type Parameters:
T- The required type.- Parameters:
function- Function to convert the value or any element inside value to required type.- Returns:
- The value as required list type.
-
asCollection
public <T, C extends Collection<T>> C asCollection(C collection, Function<OptionalType, T> function) Apply actual value to collection using function.- Type Parameters:
T- The required type inside collection.C- The required collection type.- Parameters:
collection- The collection to add values.function- Function to convert the value or any element inside value to required type.- Returns:
- The provided collection.
-
asObject
-
asObject
-
asString
Get the actual value as String or null.
This method only convert the actual value to String if it isn't null.- Returns:
- A String value or null.
-
asString
-
asChar
Get the actual value as Character or null if the conversion fails.- Returns:
- A Character value or null.
-
asChar
-
asBoolean
Get the actual value as Boolean or null if the conversion fails.- Returns:
- A Boolean value or null.
-
asBoolean
-
asByte
Get the actual value as Byte or null if the conversion fails.- Returns:
- A Byte value or null.
-
asByte
-
asShort
Get the actual value as Short or null if the conversion fails.- Returns:
- A Short value or null.
-
asShort
-
asInt
Get the actual value as Integer or null if the conversion fails.- Returns:
- A Integer value or null.
-
asInt
-
asFloat
Get the actual value as Float or null if the conversion fails.- Returns:
- A Float value or null.
-
asFloat
-
asLong
Get the actual value as Long or null if the conversion fails.- Returns:
- A Long value or null.
-
asLong
-
asDouble
Get the actual value as Double or null if the conversion fails.- Returns:
- A Double value or null.
-
asDouble
-
asUuid
Get the actual value as UUID or null if the conversion fails.- Returns:
- An UUID value or null.
-
asUuid
-
asEnum
-
asEnum
Get the actual value as Enum type.
This method only works if the actual value is a String or Number.- Type Parameters:
E- Enum required type.- Parameters:
enumType- Enum type class.def- Default UUID value.- Returns:
- A Enum value or default value.
-
asEnumSet
Get the actual value as Enum type Set.
This method only works if the actual value is a bitField.- Type Parameters:
E- Enum required type.- Parameters:
enumType- Enum type class.- Returns:
- A Set with Enum elements or null if the class isn't an enum type
-
asElementSet
Get the actual value as element type Set.
This method only works if the actual value is a bitField.- Type Parameters:
E- Element required type.- Parameters:
element- Function to convert ordinal to element type.size- The maximum length of the enum values.- Returns:
- A Set with Enum elements.
-
asOrdinalSet
-