Package org.spongepowered.api.util
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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Optional<Boolean>
asBoolean(@Nullable Object obj)
Gets the given object as aBoolean
.static Optional<Byte>
asByte(@Nullable Object obj)
Gets the given object as aByte
.static Optional<Character>
asChar(@Nullable Object obj)
Gets the given object as aCharacter
.static Optional<Double>
asDouble(@Nullable Object obj)
Gets the given object as aDouble
.static Optional<Float>
asFloat(@Nullable Object obj)
Gets the given object as aFloat
.static Optional<Integer>
asInteger(@Nullable Object obj)
Gets the given object as aInteger
.static Optional<List<?>>
asList(@Nullable Object obj)
Gets the given object as aList
.static Optional<Long>
asLong(@Nullable Object obj)
Gets the given object as aLong
.static Optional<Short>
asShort(@Nullable Object obj)
Gets the given object as aShort
.static Optional<String>
asString(@Nullable Object obj)
Gets the given object as aString
.static boolean
toBoolean(@Nullable Object obj)
Coerce the supplied object to a boolean, matches strings such as "yes" as well as literal boolean values.static byte
toByte(@Nullable Object obj)
Coerce the supplied object to a byte number, parse it if necessary.static char
toChar(@Nullable Object obj)
Coerce the supplied object to a character, parse it if necessary.static double
toDouble(@Nullable Object obj)
Coerce the supplied object to a double-precision floating-point number, parse it if necessary.static <E extends Enum<E>>
EtoEnum(@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.static <E extends Enum<E>>
EtoEnum(@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.static float
toFloat(@Nullable Object obj)
Coerce the supplied object to a single-precision floating-point number, parse it if necessary.static int
toInteger(@Nullable Object obj)
Coerce the supplied object to an integer, parse it if necessary.static List<?>
toList(@Nullable Object obj)
Coerce the supplied object to a list.static <T> List<T>
toListOf(@Nullable Object obj, Class<T> ofClass)
Coerce the specified object to a list containing only objects of type specified byofClass
.static long
toLong(@Nullable Object obj)
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
toShort(@Nullable Object obj)
Coerce the supplied object to a short number, parse it if necessary.static String
toString(@Nullable Object obj)
Coerce the supplied object to a string.static Vector2i
toVector2i(@Nullable Object obj)
Coerce the supplied object to a Vector2i.
-
-
-
Method Detail
-
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 aString
.- 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 aList
.- 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 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 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 aBoolean
.- 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 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 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 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 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 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 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 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 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 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 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 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 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 aCharacter
.- 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 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
-
-