Package org.spongepowered.api.util
Class Coerce
- java.lang.Object
-
- org.spongepowered.api.util.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 aBoolean
.static java.util.Optional<java.lang.Byte>
asByte(@Nullable java.lang.Object obj)
Gets the given object as aByte
.static java.util.Optional<java.lang.Character>
asChar(@Nullable java.lang.Object obj)
Gets the given object as aCharacter
.static java.util.Optional<java.lang.Double>
asDouble(@Nullable java.lang.Object obj)
Gets the given object as aDouble
.static java.util.Optional<java.lang.Float>
asFloat(@Nullable java.lang.Object obj)
Gets the given object as aFloat
.static java.util.Optional<java.lang.Integer>
asInteger(@Nullable java.lang.Object obj)
Gets the given object as aInteger
.static java.util.Optional<java.util.List<?>>
asList(@Nullable java.lang.Object obj)
Gets the given object as aList
.static java.util.Optional<java.lang.Long>
asLong(@Nullable java.lang.Object obj)
Gets the given object as aLong
.static java.util.Optional<java.lang.Short>
asShort(@Nullable java.lang.Object obj)
Gets the given object as aShort
.static java.util.Optional<java.lang.String>
asString(@Nullable java.lang.Object obj)
Gets the given object as aString
.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>>
EtoEnum(@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>>
EtoEnum(@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 byofClass
.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.
-
-
-
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 aString
.- 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 aList
.- 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 byofClass
. Also coerces list values where possible.- Type Parameters:
T
- type of list (notional)- Parameters:
obj
- Object to coerceofClass
- 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 aBoolean
.- 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 aInteger
.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 aDouble
.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 aFloat
.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 aShort
.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 aByte
.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 aLong
.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 aCharacter
.- 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 coerceenumClass
- 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 coerceenumClass
- Enum class to coerce todefaultValue
- 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 coercepseudoEnumClass
- The pseudo-enum classdictionaryClass
- Pseudo-enum dictionary class to look indefaultValue
- 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
-
-