Class Coerce

java.lang.Object
org.spongepowered.api.util.Coerce

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

    • toString

      public static String toString(@Nullable 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 Optional<String> asString(@Nullable Object obj)
      Gets the given object as a String.
      Parameters:
      obj - The object to translate
      Returns:
      The string value, if available
    • toList

      public static List<?> toList(@Nullable 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 Optional<List<?>> asList(@Nullable Object obj)
      Gets the given object as a List.
      Parameters:
      obj - The object to translate
      Returns:
      The list, if available
    • toListOf

      public static <T> List<T> toListOf(@Nullable Object obj, 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 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 Optional<Boolean> asBoolean(@Nullable 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 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 Optional<Integer> asInteger(@Nullable 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 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 Optional<Double> asDouble(@Nullable 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 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 Optional<Float> asFloat(@Nullable 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 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 Optional<Short> asShort(@Nullable 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 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 Optional<Byte> asByte(@Nullable 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 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 Optional<Long> asLong(@Nullable 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 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 Optional<Character> asChar(@Nullable 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 Enum<E>> E toEnum(@Nullable Object obj, 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 Enum<E>> E toEnum(@Nullable Object obj, 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 Object obj, Class<T> pseudoEnumClass, 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 Object obj)
      Coerce the supplied object to a Vector2i.
      Parameters:
      obj - Object to coerce
      Returns:
      Vector2i, returns Vector2i.ZERO if coercion failed