public final class Coerce extends Object
| Modifier and Type | Method and Description |
|---|---|
static Optional<Boolean> |
asBoolean(Object obj)
Gets the given object as a
Boolean. |
static Optional<Byte> |
asByte(Object obj)
Gets the given object as a
Byte. |
static Optional<Character> |
asChar(Object obj)
Gets the given object as a
Character. |
static Optional<Double> |
asDouble(Object obj)
Gets the given object as a
Double. |
static Optional<Float> |
asFloat(Object obj)
Gets the given object as a
Float. |
static Optional<Integer> |
asInteger(Object obj)
Gets the given object as a
Integer. |
static Optional<List<?>> |
asList(Object obj)
Gets the given object as a
List. |
static Optional<Long> |
asLong(Object obj)
Gets the given object as a
Long. |
static Optional<Short> |
asShort(Object obj)
Gets the given object as a
Short. |
static Optional<String> |
asString(Object obj)
Gets the given object as a
String. |
static boolean |
toBoolean(Object obj)
Coerce the supplied object to a boolean, matches strings such as "yes" as
well as literal boolean values.
|
static byte |
toByte(Object obj)
Coerce the supplied object to a byte number, parse it if necessary.
|
static char |
toChar(Object obj)
Coerce the supplied object to a character, parse it if necessary.
|
static double |
toDouble(Object obj)
Coerce the supplied object to a double-precision floating-point number,
parse it if necessary.
|
static <E extends Enum<E>> |
toEnum(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>> |
toEnum(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(Object obj)
Coerce the supplied object to a single-precision floating-point number,
parse it if necessary.
|
static int |
toInteger(Object obj)
Coerce the supplied object to an integer, parse it if necessary.
|
static List<?> |
toList(Object obj)
Coerce the supplied object to a list.
|
static <T> List<T> |
toListOf(Object obj,
Class<T> ofClass)
Coerce the specified object to a list containing only objects of type
specified by
ofClass. |
static long |
toLong(Object obj)
Coerce the supplied object to a long number, parse it if necessary.
|
static <T> T |
toPseudoEnum(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(Object obj)
Coerce the supplied object to a short number, parse it if necessary.
|
static String |
toString(Object obj)
Coerce the supplied object to a string.
|
static com.flowpowered.math.vector.Vector2i |
toVector2i(Object obj)
Coerce the supplied object to a Vector2i.
|
public static String toString(@Nullable Object obj)
obj - Object to coercepublic static Optional<String> asString(@Nullable Object obj)
String.obj - The object to translatepublic static List<?> toList(@Nullable Object obj)
{1,2,3,I,am,a,list}obj - Object to coercepublic static Optional<List<?>> asList(@Nullable Object obj)
List.obj - The object to translatepublic static <T> List<T> toListOf(@Nullable Object obj, Class<T> ofClass)
ofClass. Also coerces list values where
possible.T - type of list (notional)obj - Object to coerceofClass - Class to coerce topublic static boolean toBoolean(@Nullable Object obj)
obj - Object to coercefalse if the object is nullpublic static Optional<Boolean> asBoolean(@Nullable Object obj)
Boolean.obj - The object to translatepublic static int toInteger(@Nullable Object obj)
obj - Object to coerce0 if the object is null or
cannot be parsedpublic static Optional<Integer> asInteger(@Nullable Object obj)
Integer.
Note that this does not translate numbers spelled out as strings.
obj - The object to translatepublic static double toDouble(@Nullable Object obj)
obj - Object to coerce0.0 if the object is null or
cannot be parsedpublic static Optional<Double> asDouble(@Nullable Object obj)
Double.
Note that this does not translate numbers spelled out as strings.
obj - The object to translatepublic static float toFloat(@Nullable Object obj)
obj - Object to coerce0.0 if the object is null or
cannot be parsedpublic static Optional<Float> asFloat(@Nullable Object obj)
Float.
Note that this does not translate numbers spelled out as strings.
obj - The object to translatepublic static short toShort(@Nullable Object obj)
obj - Object to coerce0 if the object is null or cannot
be parsedpublic static Optional<Short> asShort(@Nullable Object obj)
Short.
Note that this does not translate numbers spelled out as strings.
obj - The object to translatepublic static byte toByte(@Nullable Object obj)
obj - Object to coerce0 if the object is null or cannot
be parsedpublic static Optional<Byte> asByte(@Nullable Object obj)
Byte.
Note that this does not translate numbers spelled out as strings.
obj - The object to translatepublic static long toLong(@Nullable Object obj)
obj - Object to coerce0 if the object is null or cannot
be parsedpublic static Optional<Long> asLong(@Nullable Object obj)
Long.
Note that this does not translate numbers spelled out as strings.
obj - The object to translatepublic static char toChar(@Nullable Object obj)
obj - Object to coerce' ' if the object is
null or cannot be parsedpublic static Optional<Character> asChar(@Nullable Object obj)
Character.obj - The object to translatepublic static <E extends Enum<E>> E toEnum(@Nullable Object obj, Class<E> enumClass)
E - enum typeobj - Object to coerceenumClass - Enum class to coerce topublic static <E extends Enum<E>> E toEnum(@Nullable Object obj, Class<E> enumClass, E defaultValue)
E - enum typeobj - Object to coerceenumClass - Enum class to coerce todefaultValue - default value to return if coercion failspublic static <T> T toPseudoEnum(@Nullable Object obj, Class<T> pseudoEnumClass, Class<?> dictionaryClass, T defaultValue)
T - pseudo-enum typeobj - Object to coercepseudoEnumClass - The pseudo-enum classdictionaryClass - Pseudo-enum dictionary class to look indefaultValue - Value to return if lookup fails