public final class ObjectUtils extends Object
If the value isn't an instance of the specified type, then converts the value if possible.
The following conversions are valid:
Convert to type | Valid input value types | Notes |
---|---|---|
A class that is the normal type of a registered PojoSwap .
|
A value whose class matches the transformed type of that registered PojoSwap .
|
|
A class that is the transformed type of a registered PojoSwap .
|
A value whose class matches the normal type of that registered PojoSwap .
|
|
Number (e.g. Integer , Short , Float ,...)
Number. (e.g. Integer. ,
Short. , Float. ,...)
|
Number , String , |
For primitive TYPES , |
Map (e.g. Map , HashMap , TreeMap , ObjectMap )
|
Map
|
If Map is not constructible, a ObjectMap is created.
|
|
|
If |
|
|
|
|
|
|
|
|
|
Bean |
|
|
|
Anything | Arrays are converted to JSON arrays |
Anything with one of the following methods:
|
|
|
Constructor and Description |
---|
ObjectUtils() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
castOrNull(Object o,
Class<T> c)
If the specified object is an instance of the specified class, casts it to that type.
|
static int |
compare(int i1,
int i2)
Compare two integers numerically.
|
static int |
compare(Object o1,
Object o2)
Compares two objects for equality.
|
static boolean |
equals(Object o1,
Object o2)
Returns
|
static <T> T |
firstNonEmpty(T... o)
Returns the first non-empty value in the list of objects.
|
static <T> T |
firstNonNull(T... t)
Returns the first non-null value in the specified array
|
static int |
firstNonZero(int... ints)
Returns the first non-zero value in the list of ints.
|
static Enum<?>[] |
getEnumConstants(Class<?> c)
Returns the enum names for the specified enum class.
|
static boolean |
isEmpty(Object o)
Returns
|
static Boolean |
toBoolean(Object o)
Converts an object to a Boolean.
|
static Integer |
toInteger(Object o)
Converts an object to an Integer.
|
static Number |
toNumber(Object o)
Converts an object to a Number.
|
static <T> T |
toType(Object value,
Class<T> type)
Converts the specified object to the specified type.
|
static <T> T |
toType(Object value,
Class<T> type,
Type... args)
Converts the specified object to the specified type.
|
public ObjectUtils()
public static <T> T toType(Object value, Class<T> type)
T
- The class type to convert the value to.value
- The value to convert.type
- The class type to convert the value to.InvalidDataConversionException
- If the specified value cannot be converted to the specified type.public static <T> T toType(Object value, Class<T> type, Type... args)
T
- The class type to convert the value to.value
- The value to convert.type
- The class type to convert the value to.args
- The type arguments.InvalidDataConversionException
- If the specified value cannot be converted to the specified type.public static boolean equals(Object o1, Object o2)
Gracefully handles
o1
- Object #1o2
- Object #2public static boolean isEmpty(Object o)
Return
Object.toString()
.
o
- The object to test.@SafeVarargs public static <T> T firstNonNull(T... t)
t
- The values to check.public static Boolean toBoolean(Object o)
o
- The object to convert.public static Integer toInteger(Object o)
o
- The object to convert.public static Number toNumber(Object o)
o
- The object to convert.public static Enum<?>[] getEnumConstants(Class<?> c)
c
- The enum class.public static <T> T castOrNull(Object o, Class<T> c)
o
- The object to cast.c
- The class to cast to.public static int firstNonZero(int... ints)
ints
- The ints to check.@SafeVarargs public static <T> T firstNonEmpty(T... o)
o
- The objects to check.isEmpty(Object)
returns public static int compare(Object o1, Object o2)
Nulls are always considered less-than unless both are null.
o1
- Object 1.o2
- Object 2.Comparable
interface.public static final int compare(int i1, int i2)
i1
- Integer #1i2
- Integer #2Copyright © 2016–2020 The Apache Software Foundation. All rights reserved.