Class CastUtils

java.lang.Object
org.saidone.utils.CastUtils

public class CastUtils extends Object
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 Details

    • CastUtils

      public CastUtils()
  • Method Details

    • castToListOfStrings

      public List<String> castToListOfStrings(List<?> list)
      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 of String. If the input list is null, 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

      public Map<String,Serializable> castToMapOfStringSerializable(Object object)
      Casts an input object to a map with String keys and Serializable values.
      If the provided object is null, an empty map is returned. If the provided object is not an instance of Map<?, ?>, an IllegalArgumentException is thrown. The method performs a runtime cast of each key to String; if any key cannot be cast, a ClassCastException 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 map
      ClassCastException - if a map key cannot be cast to String or a value cannot be cast to Serializable