Package org.saidone.utils
Class CastUtils
java.lang.Object
org.saidone.utils.CastUtils
Utility class providing methods for safely casting collections to specific generic types.
This class offers methods to cast a list of unknown type to a list of strings, and to cast an object representing a map to a map with string keys and object values. All methods return empty collections if the provided input is null.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncastToListOfStrings
(List<?> list) Safely casts a list of objects to a list containing only non-null strings.castToMapOfStringSerializable
(Object object) Casts an input object to a map withString
keys andSerializable
values.
-
Constructor Details
-
CastUtils
public CastUtils()
-
-
Method Details
-
castToListOfStrings
Safely casts a list of objects to a list containing only non-null strings.
This method filters out null values and elements that are not instances ofString
. If the input list isnull
, an empty list is returned.- Parameters:
list
- the input list containing elements of any type- Returns:
- a list containing only non-null strings from the original list,
or an empty list if input is
null
-
castToMapOfStringSerializable
Casts an input object to a map withString
keys andSerializable
values.
If the provided object isnull
, an empty map is returned. If the provided object is not an instance ofMap<?, ?>
, anIllegalArgumentException
is thrown. The method performs a runtime cast of each key toString
; if any key cannot be cast, aClassCastException
will be thrown.- Parameters:
object
- the object to cast, expected to be a map with string keys- Returns:
- a map with string keys and serializable values, or an empty map if the input is null
- Throws:
IllegalArgumentException
- if the input object is not a mapClassCastException
- if a map key cannot be cast toString
or a value cannot be cast toSerializable
-