Class TypedCollections


  • public class TypedCollections
    extends Object
    • Constructor Detail

      • TypedCollections

        public TypedCollections()
    • Method Detail

      • dynamicallyCastCollection

        public static <E,​TypedC extends Collection<E>> TypedC dynamicallyCastCollection​(Collection<?> c,
                                                                                              Class<E> type,
                                                                                              Class<TypedC> collectionType)
        Dynamically check that the members of the collection are all instances of the given type (or null), and that the collection itself is of the given collection type.
        Type Parameters:
        E - the collection's element type
        Parameters:
        c - the collection to cast
        type - the class of the collection's element type.
        Returns:
        the dynamically-type checked collection.
        Throws:
        ClassCastException
      • dynamicallyCastList

        public static <E> List<E> dynamicallyCastList​(List<?> list,
                                                      Class<E> type)
        Dynamically check that the members of the list are all instances of the given type (or null).
        Type Parameters:
        E - the list's element type
        Parameters:
        list - the list to cast
        type - the class of the list's element type.
        Returns:
        the dynamically-type checked list.
        Throws:
        ClassCastException
      • dynamicallyCastSet

        public static <E> Set<E> dynamicallyCastSet​(Set<?> set,
                                                    Class<E> type)
        Dynamically check that the members of the set are all instances of the given type (or null).
        Type Parameters:
        E - the set's element type
        Parameters:
        set - the set to cast
        type - the class of the set's element type.
        Returns:
        the dynamically-type checked set.
        Throws:
        ClassCastException
      • dynamicallyCastMap

        public static <K,​V> Map<K,​V> dynamicallyCastMap​(Map<?,​?> map,
                                                                    Class<K> keyType,
                                                                    Class<V> valueType)
        Dynamically check that the keys and values in the map are all instances of the correct types (or null).
        Type Parameters:
        K - the map's key type
        V - the map's value type
        Parameters:
        map - the map to cast
        keyType - the class of the map's key type.
        valueType - the class of the map's value type.
        Returns:
        the dynamically-type checked map.
        Throws:
        ClassCastException