Class CastingList<E>

  • Type Parameters:
    E - Element type
    All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>

    public class CastingList<E>
    extends java.util.AbstractList<E>
    implements java.util.List<E>
    Converts a list whose members are automatically down-cast to a given type.

    If a member of the backing list is not an instanceof E, the accessing method (such as List.get(int)) will throw a ClassCastException.

    All modifications are automatically written to the backing list. Not synchronized.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Class<E> clazz  
      private java.util.List<? super E> list  
      • Fields inherited from class java.util.AbstractList

        modCount
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected CastingList​(java.util.List<? super E> list, java.lang.Class<E> clazz)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int pos, E o)  
      E get​(int index)  
      E remove​(int index)  
      E set​(int index, E element)  
      int size()  
      • Methods inherited from class java.util.AbstractList

        add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, removeRange, subList
      • Methods inherited from class java.util.AbstractCollection

        addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.List

        add, addAll, addAll, clear, contains, containsAll, equals, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, removeAll, replaceAll, retainAll, sort, spliterator, subList, toArray, toArray
    • Field Detail

      • list

        private final java.util.List<? super E> list
      • clazz

        private final java.lang.Class<E> clazz
    • Constructor Detail

      • CastingList

        protected CastingList​(java.util.List<? super E> list,
                              java.lang.Class<E> clazz)
    • Method Detail

      • get

        public E get​(int index)
        Specified by:
        get in interface java.util.List<E>
        Specified by:
        get in class java.util.AbstractList<E>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in interface java.util.List<E>
        Specified by:
        size in class java.util.AbstractCollection<E>
      • set

        public E set​(int index,
                     E element)
        Specified by:
        set in interface java.util.List<E>
        Overrides:
        set in class java.util.AbstractList<E>
      • remove

        public E remove​(int index)
        Specified by:
        remove in interface java.util.List<E>
        Overrides:
        remove in class java.util.AbstractList<E>
      • add

        public void add​(int pos,
                        E o)
        Specified by:
        add in interface java.util.List<E>
        Overrides:
        add in class java.util.AbstractList<E>