Class CastUtils
The helpers in this class convert loose Object instances into typed
List and Map representations. Inputs are expected to already
contain compatible elements; otherwise a ClassCastException will be
raised by the casting operations. When null is provided, the methods
return empty collections instead of null.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> List<T>castToListOfObjects(Object object, Class<T> elementType) Casts an object to aListcontaining elements of the requested type.<KT,KV> Map<KT, KV> castToMapOfObjectObject(Object object, Class<KT> keyType, Class<KV> valueType)
-
Constructor Details
-
CastUtils
public CastUtils()
-
-
Method Details
-
castToListOfObjects
Casts an object to aListcontaining elements of the requested type.The input must already be a
List; otherwise anIllegalArgumentExceptionis thrown. Each element is cast usingClass.cast(Object), so incompatible entries trigger aClassCastException. Whennullis supplied, an empty list is returned.- Parameters:
object- the input value expected to be aListelementType- the desired element type- Returns:
- a list containing elements cast to
elementType, or an empty list when the input isnull - Throws:
IllegalArgumentException- ifobjectis not aListClassCastException- if any element cannot be cast toelementType
-
castToMapOfObjectObject
public <KT,KV> Map<KT,KV> castToMapOfObjectObject(Object object, Class<KT> keyType, Class<KV> valueType) Casts an object to aMapwithStringkeys and values of the requestedSerializabletype.The input must be a
Map; otherwise anIllegalArgumentExceptionis thrown. Keys are cast toStringand values tovalueType, so incompatible entries causeClassCastExceptions. Whennullis provided, an empty map is returned.- Parameters:
object- the input value expected to be aMapkeyType- the expected key typevalueType- the expected value type- Returns:
- a map containing entries cast to the requested types, or an empty
map when the input is
null - Throws:
IllegalArgumentException- ifobjectis not aMapClassCastException- if any key or value cannot be cast to the requested types
-