Package org.spongepowered.api.util
Class Coerce
java.lang.Object
org.spongepowered.api.util.Coerce
Utility class for coercing unknown values to specific target types.
-
Method Summary
Modifier and TypeMethodDescriptionGets the given object as aBoolean
.Gets the given object as aByte
.Gets the given object as aCharacter
.Gets the given object as aDouble
.Gets the given object as aFloat
.Gets the given object as aInteger
.Gets the given object as aList
.Gets the given object as aLong
.Gets the given object as aShort
.Gets the given object as aString
.static boolean
Coerce the supplied object to a boolean, matches strings such as "yes" as well as literal boolean values.static byte
Coerce the supplied object to a byte number, parse it if necessary.static char
Coerce the supplied object to a character, parse it if necessary.static double
Coerce the supplied object to a double-precision floating-point number, parse it if necessary.static <E extends Enum<E>>
ECoerce the specified object to an enum of the supplied type, returns the first enum constant in the enum if parsing fails.static <E extends Enum<E>>
ECoerce the specified object to an enum of the supplied type, returns the specified default value if parsing fails.static float
Coerce the supplied object to a single-precision floating-point number, parse it if necessary.static int
Coerce the supplied object to an integer, parse it if necessary.static List<?>
Coerce the supplied object to a list.static <T> List<T>
Coerce the specified object to a list containing only objects of type specified byofClass
.static long
Coerce the supplied object to a long number, parse it if necessary.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.static short
Coerce the supplied object to a short number, parse it if necessary.static String
Coerce the supplied object to a string.static Vector2i
toVector2i
(@Nullable Object obj) Coerce the supplied object to a Vector2i.
-
Method Details
-
toString
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
Gets the given object as aString
.- Parameters:
obj
- The object to translate- Returns:
- The string value, if available
-
toList
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
Gets the given object as aList
.- Parameters:
obj
- The object to translate- Returns:
- The list, if available
-
toListOf
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
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
Gets the given object as aBoolean
.- Parameters:
obj
- The object to translate- Returns:
- The boolean, if available
-
toInteger
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
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
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
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
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
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
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
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
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
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
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
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
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
Gets the given object as aCharacter
.- Parameters:
obj
- The object to translate- Returns:
- The character, if available
-
toEnum
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 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 coerceenumClass
- Enum class to coerce todefaultValue
- 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 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
Coerce the supplied object to a Vector2i.- Parameters:
obj
- Object to coerce- Returns:
- Vector2i, returns Vector2i.ZERO if coercion failed
-