Class Coerce


  • public final class Coerce
    extends java.lang.Object
    Utility class for coercing unknown values to specific target types.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Optional<java.lang.Boolean> asBoolean​(@Nullable java.lang.Object obj)
      Gets the given object as a Boolean.
      static java.util.Optional<java.lang.Byte> asByte​(@Nullable java.lang.Object obj)
      Gets the given object as a Byte.
      static java.util.Optional<java.lang.Character> asChar​(@Nullable java.lang.Object obj)
      Gets the given object as a Character.
      static java.util.Optional<java.lang.Double> asDouble​(@Nullable java.lang.Object obj)
      Gets the given object as a Double.
      static java.util.Optional<java.lang.Float> asFloat​(@Nullable java.lang.Object obj)
      Gets the given object as a Float.
      static java.util.Optional<java.lang.Integer> asInteger​(@Nullable java.lang.Object obj)
      Gets the given object as a Integer.
      static java.util.Optional<java.util.List<?>> asList​(@Nullable java.lang.Object obj)
      Gets the given object as a List.
      static java.util.Optional<java.lang.Long> asLong​(@Nullable java.lang.Object obj)
      Gets the given object as a Long.
      static java.util.Optional<java.lang.Short> asShort​(@Nullable java.lang.Object obj)
      Gets the given object as a Short.
      static java.util.Optional<java.lang.String> asString​(@Nullable java.lang.Object obj)
      Gets the given object as a String.
      static boolean toBoolean​(@Nullable java.lang.Object obj)
      Coerce the supplied object to a boolean, matches strings such as "yes" as well as literal boolean values.
      static byte toByte​(@Nullable java.lang.Object obj)
      Coerce the supplied object to a byte number, parse it if necessary.
      static char toChar​(@Nullable java.lang.Object obj)
      Coerce the supplied object to a character, parse it if necessary.
      static double toDouble​(@Nullable java.lang.Object obj)
      Coerce the supplied object to a double-precision floating-point number, parse it if necessary.
      static <E extends java.lang.Enum<E>>
      E
      toEnum​(@Nullable java.lang.Object obj, java.lang.Class<E> enumClass)
      Coerce the specified object to an enum of the supplied type, returns the first enum constant in the enum if parsing fails.
      static <E extends java.lang.Enum<E>>
      E
      toEnum​(@Nullable java.lang.Object obj, java.lang.Class<E> enumClass, E defaultValue)
      Coerce the specified object to an enum of the supplied type, returns the specified default value if parsing fails.
      static float toFloat​(@Nullable java.lang.Object obj)
      Coerce the supplied object to a single-precision floating-point number, parse it if necessary.
      static int toInteger​(@Nullable java.lang.Object obj)
      Coerce the supplied object to an integer, parse it if necessary.
      static java.util.List<?> toList​(@Nullable java.lang.Object obj)
      Coerce the supplied object to a list.
      static <T> java.util.List<T> toListOf​(@Nullable java.lang.Object obj, java.lang.Class<T> ofClass)
      Coerce the specified object to a list containing only objects of type specified by ofClass.
      static long toLong​(@Nullable java.lang.Object obj)
      Coerce the supplied object to a long number, parse it if necessary.
      static <T> T toPseudoEnum​(@Nullable java.lang.Object obj, java.lang.Class<T> pseudoEnumClass, java.lang.Class<?> dictionaryClass, T defaultValue)
      Coerce the specified object to the specified pseudo-enum type using the supplied pseudo-enum dictionary class.
      static short toShort​(@Nullable java.lang.Object obj)
      Coerce the supplied object to a short number, parse it if necessary.
      static java.lang.String toString​(@Nullable java.lang.Object obj)
      Coerce the supplied object to a string.
      static Vector2i toVector2i​(@Nullable java.lang.Object obj)
      Coerce the supplied object to a Vector2i.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • toString

        public static java.lang.String toString​(@Nullable java.lang.Object obj)
        Coerce the supplied object to a string.
        Parameters:
        obj - Object to coerce
        Returns:
        Object as a string, empty string if the object is null
      • asString

        public static java.util.Optional<java.lang.String> asString​(@Nullable java.lang.Object obj)
        Gets the given object as a String.
        Parameters:
        obj - The object to translate
        Returns:
        The string value, if available
      • toList

        public static java.util.List<?> toList​(@Nullable java.lang.Object obj)
        Coerce the supplied object to a list. Accepts lists and all types of 1D arrays. Also (naively) supports lists in Strings in a format like {1,2,3,I,am,a,list}
        Parameters:
        obj - Object to coerce
        Returns:
        Some kind of List filled with unimaginable horrors
      • asList

        public static java.util.Optional<java.util.List<?>> asList​(@Nullable java.lang.Object obj)
        Gets the given object as a List.
        Parameters:
        obj - The object to translate
        Returns:
        The list, if available
      • toListOf

        public static <T> java.util.List<T> toListOf​(@Nullable java.lang.Object obj,
                                                     java.lang.Class<T> ofClass)
        Coerce the specified object to a list containing only objects of type specified by ofClass. Also coerces list values where possible.
        Type Parameters:
        T - type of list (notional)
        Parameters:
        obj - Object to coerce
        ofClass - Class to coerce to
        Returns:
        List of coerced values
      • toBoolean

        public static boolean toBoolean​(@Nullable java.lang.Object obj)
        Coerce the supplied object to a boolean, matches strings such as "yes" as well as literal boolean values.
        Parameters:
        obj - Object to coerce
        Returns:
        Object as a boolean, false if the object is null
      • asBoolean

        public static java.util.Optional<java.lang.Boolean> asBoolean​(@Nullable java.lang.Object obj)
        Gets the given object as a Boolean.
        Parameters:
        obj - The object to translate
        Returns:
        The boolean, if available
      • toInteger

        public static int toInteger​(@Nullable java.lang.Object obj)
        Coerce the supplied object to an integer, parse it if necessary.
        Parameters:
        obj - Object to coerce
        Returns:
        Object as an integer, 0 if the object is null or cannot be parsed
      • asInteger

        public static java.util.Optional<java.lang.Integer> asInteger​(@Nullable java.lang.Object obj)
        Gets the given object as a Integer.

        Note that this does not translate numbers spelled out as strings.

        Parameters:
        obj - The object to translate
        Returns:
        The integer value, if available
      • toDouble

        public static double toDouble​(@Nullable java.lang.Object obj)
        Coerce the supplied object to a double-precision floating-point number, parse it if necessary.
        Parameters:
        obj - Object to coerce
        Returns:
        Object as a double, 0.0 if the object is null or cannot be parsed
      • asDouble

        public static java.util.Optional<java.lang.Double> asDouble​(@Nullable java.lang.Object obj)
        Gets the given object as a Double.

        Note that this does not translate numbers spelled out as strings.

        Parameters:
        obj - The object to translate
        Returns:
        The double value, if available
      • toFloat

        public static float toFloat​(@Nullable java.lang.Object obj)
        Coerce the supplied object to a single-precision floating-point number, parse it if necessary.
        Parameters:
        obj - Object to coerce
        Returns:
        Object as a float, 0.0 if the object is null or cannot be parsed
      • asFloat

        public static java.util.Optional<java.lang.Float> asFloat​(@Nullable java.lang.Object obj)
        Gets the given object as a Float.

        Note that this does not translate numbers spelled out as strings.

        Parameters:
        obj - The object to translate
        Returns:
        The float value, if available
      • toShort

        public static short toShort​(@Nullable java.lang.Object obj)
        Coerce the supplied object to a short number, parse it if necessary.
        Parameters:
        obj - Object to coerce
        Returns:
        Object as a short, 0 if the object is null or cannot be parsed
      • asShort

        public static java.util.Optional<java.lang.Short> asShort​(@Nullable java.lang.Object obj)
        Gets the given object as a Short.

        Note that this does not translate numbers spelled out as strings.

        Parameters:
        obj - The object to translate
        Returns:
        The short value, if available
      • toByte

        public static byte toByte​(@Nullable java.lang.Object obj)
        Coerce the supplied object to a byte number, parse it if necessary.
        Parameters:
        obj - Object to coerce
        Returns:
        Object as a byte, 0 if the object is null or cannot be parsed
      • asByte

        public static java.util.Optional<java.lang.Byte> asByte​(@Nullable java.lang.Object obj)
        Gets the given object as a Byte.

        Note that this does not translate numbers spelled out as strings.

        Parameters:
        obj - The object to translate
        Returns:
        The byte value, if available
      • toLong

        public static long toLong​(@Nullable java.lang.Object obj)
        Coerce the supplied object to a long number, parse it if necessary.
        Parameters:
        obj - Object to coerce
        Returns:
        Object as a long, 0 if the object is null or cannot be parsed
      • asLong

        public static java.util.Optional<java.lang.Long> asLong​(@Nullable java.lang.Object obj)
        Gets the given object as a Long.

        Note that this does not translate numbers spelled out as strings.

        Parameters:
        obj - The object to translate
        Returns:
        The long value, if available
      • toChar

        public static char toChar​(@Nullable java.lang.Object obj)
        Coerce the supplied object to a character, parse it if necessary.
        Parameters:
        obj - Object to coerce
        Returns:
        Object as a character, '' if the object is null or cannot be parsed
      • asChar

        public static java.util.Optional<java.lang.Character> asChar​(@Nullable java.lang.Object obj)
        Gets the given object as a Character.
        Parameters:
        obj - The object to translate
        Returns:
        The character, if available
      • toEnum

        public static <E extends java.lang.Enum<E>> E toEnum​(@Nullable java.lang.Object obj,
                                                             java.lang.Class<E> enumClass)
        Coerce the specified object to an enum of the supplied type, returns the first enum constant in the enum if parsing fails.
        Type Parameters:
        E - enum type
        Parameters:
        obj - Object to coerce
        enumClass - Enum class to coerce to
        Returns:
        Coerced enum value
      • toEnum

        public static <E extends java.lang.Enum<E>> E toEnum​(@Nullable java.lang.Object obj,
                                                             java.lang.Class<E> enumClass,
                                                             E defaultValue)
        Coerce the specified object to an enum of the supplied type, returns the specified default value if parsing fails.
        Type Parameters:
        E - enum type
        Parameters:
        obj - Object to coerce
        enumClass - Enum class to coerce to
        defaultValue - default value to return if coercion fails
        Returns:
        Coerced enum value
      • toPseudoEnum

        public static <T> T toPseudoEnum​(@Nullable java.lang.Object obj,
                                         java.lang.Class<T> pseudoEnumClass,
                                         java.lang.Class<?> dictionaryClass,
                                         T defaultValue)
        Coerce the specified object to the specified pseudo-enum type using the supplied pseudo-enum dictionary class.
        Type Parameters:
        T - pseudo-enum type
        Parameters:
        obj - Object to coerce
        pseudoEnumClass - The pseudo-enum class
        dictionaryClass - Pseudo-enum dictionary class to look in
        defaultValue - Value to return if lookup fails
        Returns:
        Coerced value or default if coercion fails
      • toVector2i

        public static Vector2i toVector2i​(@Nullable java.lang.Object obj)
        Coerce the supplied object to a Vector2i.
        Parameters:
        obj - Object to coerce
        Returns:
        Vector2i, returns Vector2i.ZERO if coercion failed