Class OptionalType

java.lang.Object
com.saicone.rtag.util.IterableType<Object>
com.saicone.rtag.util.OptionalType
All Implemented Interfaces:
Iterable<Object>

public class OptionalType extends IterableType<Object>
Object container with automatic conversion to required type when value is called.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final OptionalType
    Static object representation of empty OptionalType.
    static final com.google.gson.Gson
    Gson public instance with default configuration.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an OptionalType with specified value.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    as(Class<T> type)
    Get actual value as required class type using Gson deserializer.
    <T> T
    as(Type type)
    Get 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.
    Get the actual value as Byte or null if the conversion fails.
    asByte(Byte def)
    Get the actual value as Byte or default value instead if the conversion fails.
    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>>
    C
    asCollection(C collection, Function<OptionalType, T> function)
    Apply actual value to collection using function.
    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>>
    E
    asEnum(Class<E> enumType)
    Get the actual value as Enum type.
    This method only works if the actual value is a String or Number.
    <E extends Enum<E>>
    E
    asEnum(Class<E> enumType, E def)
    Get the actual value as Enum type.
    This method only works if the actual value is a String or Number.
    <E extends Enum<E>>
    Set<E>
    asEnumSet(Class<E> enumType)
    Get the actual value as Enum type Set.
    This method only works if the actual value is a bitField.
    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.
    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>
    Get actual value as list using function.
    Get the actual value as Long or null if the conversion fails.
    asLong(Long def)
    Get the actual value as Long or default value instead if the conversion fails.
    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.
    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.
    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.
    Get the actual value as UUID or null if the conversion fails.
    asUuid(UUID def)
    Get the actual value as UUID or default value instead if the conversion fails.
    <T> T
    Get actual value by function.
    <T> T
    by(ThrowableFunction<Object,T> function, T def)
    Get actual value by function with defined default value.
    <T> T
    by(Class<T> type, ThrowableFunction<Object,T> function, T def)
    Get actual value as type or by function with defined default value.
    static <T> T
    cast(Object object)
    Cast any object to required type.
    Take in count this method may produce ClassCastException if you want to cast inconvertible types.
    void
    Clear the current object, only affect Collection and Map types.
    void
    clear(boolean deep)
    Clear the current object, only affect Collection and Map types.
    boolean
     
    Get the first value.
    This method extract the first value if the current object is a collection or array.
    protected Object
    Get the object that can be iterated.
    int
     
    boolean
    Check if the existence of current value.
    boolean
    isInstance(Class<?> clazz)
    Check if the current value is instance of defined class.
    boolean
    Same has isEmpty() but with inverted result.
    boolean
    isNotInstance(Class<?> clazz)
    Same has isInstance(Class) but with inverted result.
    of(Object value)
    Get current OptionalType from object value.
    <T> T
    or(Class<T> clazz, T def)
    Get actual value converted to required type class or default defined value.
    <T> T
    or(T def)
    Get actual value converted to required type or default defined value.
    protected void
    Set the object that can be iterated.
    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> T
    Get actual value converted to required type.
    <T> T
    value(Class<T> clazz)
    Get actual value converted to required type class.

    Methods inherited from class IterableType

    isIterable, isNotIterable, iterator

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface Iterable

    forEach, spliterator
  • Field Details

    • EMPTY

      public static final OptionalType EMPTY
      Static object representation of empty OptionalType.
    • GSON

      public static final com.google.gson.Gson GSON
      Gson public instance with default configuration.
  • Constructor Details

    • OptionalType

      public OptionalType(Object value)
      Constructs an OptionalType with specified value.
      Parameters:
      value - Saved value, can be null.
  • Method Details

    • of

      public static OptionalType of(Object value)
      Get current OptionalType from object value.
      Parameters:
      value - Saved value, can be null.
      Returns:
      An OptionalType with object value.
    • cast

      public static <T> T cast(Object object)
      Cast any object to required type.
      Take in count this method may produce ClassCastException if 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

      protected Object getIterable()
      Description copied from class: IterableType
      Get the object that can be iterated.
      Specified by:
      getIterable in class IterableType<Object>
      Returns:
      an iterable object.
    • setIterable

      protected void setIterable(Object value)
      Description copied from class: IterableType
      Set the object that can be iterated.
      Specified by:
      setIterable in class IterableType<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 has isEmpty() but with inverted result.
      Returns:
      true if current value is not null.
    • isInstance

      public boolean isInstance(Class<?> clazz)
      Check if the current value is instance of defined class.
      Parameters:
      clazz - Class type.
      Returns:
      true if value is instance of class.
    • isNotInstance

      public boolean isNotInstance(Class<?> clazz)
      Same has isInstance(Class) but with inverted result.
      Parameters:
      clazz - Class type.
      Returns:
      true if value is not instance of class.
    • first

      public OptionalType 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

      public OptionalType 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

      public <T> T value(Class<T> clazz)
      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 affect Collection and Map types.
    • clear

      public void clear(boolean deep)
      Clear the current object, only affect Collection and Map types.
      Parameters:
      deep - true to clear the inner objects.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • 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

      public <T> T or(Class<T> clazz, T def)
      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

      public <T> T by(ThrowableFunction<Object,T> function)
      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

      public <T> T by(ThrowableFunction<Object,T> function, T def)
      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

      public <T> T by(Class<T> type, ThrowableFunction<Object,T> function, T def)
      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

      public <T> T as(Type type)
      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

      public <T> T as(Class<T> type)
      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

      public <T> T[] asArray(T[] array, Function<OptionalType, T> function)
      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

      public <T> List<T> asList(Function<OptionalType, T> function)
      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

      public Object asObject()
      Get the actual value as Object or null.
      Returns:
      An Object value or null.
    • asObject

      public Object asObject(Object def)
      Get the actual value as Object or default value instead.
      Parameters:
      def - Default Object value.
      Returns:
      An Object value or default value.
    • asString

      public String 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

      public String asString(String def)
      Get the actual value as String if it isn't null or default value instead.
      Parameters:
      def - Default String value.
      Returns:
      A String value or default value.
    • asChar

      public Character asChar()
      Get the actual value as Character or null if the conversion fails.
      Returns:
      A Character value or null.
    • asChar

      public Character asChar(Character def)
      Get the actual value as Character or default value instead if the conversion fails.
      Parameters:
      def - Default Character value.
      Returns:
      A Character value or default value.
    • asBoolean

      public Boolean asBoolean()
      Get the actual value as Boolean or null if the conversion fails.
      Returns:
      A Boolean value or null.
    • asBoolean

      public Boolean asBoolean(Boolean def)
      Get the actual value as Boolean or default value instead if the conversion fails.
      Parameters:
      def - Default Boolean value.
      Returns:
      A Boolean value or default value.
    • asByte

      public Byte asByte()
      Get the actual value as Byte or null if the conversion fails.
      Returns:
      A Byte value or null.
    • asByte

      public Byte asByte(Byte def)
      Get the actual value as Byte or default value instead if the conversion fails.
      Parameters:
      def - Default Byte value.
      Returns:
      A Byte value or default value.
    • asShort

      public Short asShort()
      Get the actual value as Short or null if the conversion fails.
      Returns:
      A Short value or null.
    • asShort

      public Short asShort(Short def)
      Get the actual value as Short or default value instead if the conversion fails.
      Parameters:
      def - Default Short value.
      Returns:
      A Short value or default value.
    • asInt

      public Integer asInt()
      Get the actual value as Integer or null if the conversion fails.
      Returns:
      A Integer value or null.
    • asInt

      public Integer asInt(Integer def)
      Get the actual value as Integer or default value instead if the conversion fails.
      Parameters:
      def - Default Integer value.
      Returns:
      A Integer value or default value.
    • asFloat

      public Float asFloat()
      Get the actual value as Float or null if the conversion fails.
      Returns:
      A Float value or null.
    • asFloat

      public Float asFloat(Float def)
      Get the actual value as Float or default value instead if the conversion fails.
      Parameters:
      def - Default Float value.
      Returns:
      A Float value or default value.
    • asLong

      public Long asLong()
      Get the actual value as Long or null if the conversion fails.
      Returns:
      A Long value or null.
    • asLong

      public Long asLong(Long def)
      Get the actual value as Long or default value instead if the conversion fails.
      Parameters:
      def - Default Long value.
      Returns:
      A Long value or default value.
    • asDouble

      public Double asDouble()
      Get the actual value as Double or null if the conversion fails.
      Returns:
      A Double value or null.
    • asDouble

      public Double asDouble(Double def)
      Get the actual value as Double or default value instead if the conversion fails.
      Parameters:
      def - Default Double value.
      Returns:
      A Double value or default value.
    • asUuid

      public UUID asUuid()
      Get the actual value as UUID or null if the conversion fails.
      Returns:
      An UUID value or null.
    • asUuid

      public UUID asUuid(UUID def)
      Get the actual value as UUID or default value instead if the conversion fails.
      Parameters:
      def - Default UUID value.
      Returns:
      An UUID value or default value.
    • asEnum

      public <E extends Enum<E>> E asEnum(Class<E> enumType)
      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.
      Returns:
      A Enum value.
    • asEnum

      public <E extends Enum<E>> E asEnum(Class<E> enumType, E def)
      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

      public <E extends Enum<E>> Set<E> asEnumSet(Class<E> enumType)
      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

      public <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.
      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

      public Set<Integer> asOrdinalSet(int size)
      Get the actual value as ordinal-value Set.
      This method only works if the actual value is a bitField.
      Parameters:
      size - The maximum length of the ordinal values.
      Returns:
      A Set with ordinals.