public class PrimitiveConversions extends Object
| Modifier and Type | Field and Description |
|---|---|
static Map<Class<?>,Class<?>> |
NON_NUMERIC_CONVERSIONS
All non-numeric primitives and their corresponding non-primitive representations; vice-versa.
|
static Set<Class<?>> |
NUMERIC_CLASSES
All numeric data type classes.
|
static Map<Class<?>,Class<?>> |
NUMERIC_PRIMITIVES
All numeric primitives and their corresponding non-primitive representations.
|
static Map<Class<?>,Class<?>> |
PRIMITIVES_TO_OBJECTS
All primitives mapped to their object representations.
|
| Constructor and Description |
|---|
PrimitiveConversions() |
| Modifier and Type | Method and Description |
|---|---|
static Object |
convertNumber(Object value,
Class<?> target)
Converts a number to the target type.
|
static boolean |
isNumber(Class<?> clazz)
Returns if the given class represents a numeric data type.
|
static Optional<BigInteger> |
toBigInt(Object value)
Converts the given value to big integer.
|
static Optional<Byte> |
toByte(Object value)
Converts the given value to byte.
|
static Optional<Double> |
toDouble(Object value)
Converts the given value to double.
|
static Optional<Float> |
toFloat(Object value)
Converts the given value to float.
|
static Optional<Integer> |
toInt(Object value)
Converts the given value to integer.
|
static Optional<Long> |
toLong(Object value)
Converts the given value to long.
|
static Optional<Short> |
toShort(Object value)
Converts the given value to short.
|
public static final Map<Class<?>,Class<?>> NUMERIC_PRIMITIVES
public static final Map<Class<?>,Class<?>> PRIMITIVES_TO_OBJECTS
public static final Map<Class<?>,Class<?>> NON_NUMERIC_CONVERSIONS
public static boolean isNumber(@NotNull
Class<?> clazz)
clazz - the class to checkpublic static Object convertNumber(@NotNull Object value, @NotNull Class<?> target)
Number and NUMERIC_CLASSES must contain the target class.
This method supports casting between two numeric primitives, two non-primitive numeric representations and one of each kind. Casting between any primitive type, and it's non-primitive representation is also supported.
value - the value to converttarget - the target typepublic static Optional<Integer> toInt(@Nullable Object value)
If the given value is null, not an instance of Number or could not be converted by parsing
from string via Integer.valueOf(String), returns an empty optional.
value - the numberpublic static Optional<Byte> toByte(@Nullable Object value)
If the given value is null, not an instance of Number or could not be converted by parsing
from string via Byte.valueOf(String), returns an empty optional.
value - the numberpublic static Optional<Long> toLong(@Nullable Object value)
If the given value is null, not an instance of Number or could not be converted by parsing
from string via Long.valueOf(String), returns an empty optional.
value - the numberpublic static Optional<Double> toDouble(@Nullable Object value)
If the given value is null, not an instance of Number or could not be converted by parsing
from string via Double.valueOf(String), returns an empty optional.
value - the numberpublic static Optional<Float> toFloat(@Nullable Object value)
If the given value is null, not an instance of Number or could not be converted by parsing
from string via Float.valueOf(String), returns an empty optional.
value - the numberpublic static Optional<Short> toShort(@Nullable Object value)
If the given value is null, not an instance of Number or could not be converted by parsing
from string via Short.valueOf(String), returns an empty optional.
value - the numberpublic static Optional<BigInteger> toBigInt(@Nullable Object value)
If the given value is null, not an instance of BigInteger, Number or could not be
converted by parsing from string via BigInteger(String), returns an empty optional.
value - the numberCopyright © 2022. All rights reserved.