Class CachedList

  • All Implemented Interfaces:
    java.lang.Iterable, java.util.Collection, java.util.List

    public class CachedList
    extends java.util.AbstractList
    A wrapper around a List which translates added objects into SoftObject references, allowing the VM to garbage collect objects in the collection when memory is low.
    Version:
    $Revision$
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.List list
      Wrapped list.
      protected java.lang.ref.ReferenceQueue queue
      Reference queue.
      • Fields inherited from class java.util.AbstractList

        modCount
    • Constructor Summary

      Constructors 
      Constructor Description
      CachedList()
      Construct a CachedList using a LinkedList for storage.
      CachedList​(java.util.List list)
      Construct a CachedList.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, java.lang.Object obj)
      Inserts the specified element at the specified position in this list (optional operation).
      java.lang.Object get​(int index)
      Returns the element at the specified position in this list.
      private java.lang.Object getObject​(int index)
      Dereference the object at the given index.
      private void maintain()
      Maintains the collection by removing garbage collected objects.
      java.lang.Object remove​(int index)
      Removes the element at the specified position in this list (optional operation).
      java.lang.Object set​(int index, java.lang.Object obj)
      Replaces the element at the specified position in this list with the specified element.
      int size()
      Return the size of the list.
      • 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

        addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
    • Field Detail

      • queue

        protected final java.lang.ref.ReferenceQueue queue
        Reference queue.
      • list

        protected final java.util.List list
        Wrapped list.
    • Constructor Detail

      • CachedList

        public CachedList​(java.util.List list)
        Construct a CachedList.
        Parameters:
        list - List to wrap.
      • CachedList

        public CachedList()
        Construct a CachedList using a LinkedList for storage.
    • Method Detail

      • getObject

        private java.lang.Object getObject​(int index)
        Dereference the object at the given index.
      • get

        public java.lang.Object get​(int index)
        Returns the element at the specified position in this list.
        Specified by:
        get in interface java.util.List
        Specified by:
        get in class java.util.AbstractList
        Parameters:
        index - Index of element to return.
        Returns:
        The element at the specified position.
      • size

        public int size()
        Return the size of the list.
        Specified by:
        size in interface java.util.Collection
        Specified by:
        size in interface java.util.List
        Specified by:
        size in class java.util.AbstractCollection
        Returns:
        The number of elements in the list.
      • set

        public java.lang.Object set​(int index,
                                    java.lang.Object obj)
        Replaces the element at the specified position in this list with the specified element.
        Specified by:
        set in interface java.util.List
        Overrides:
        set in class java.util.AbstractList
        Parameters:
        index - Index of element to replace.
        obj - Element to be stored at the specified postion.
        Returns:
        The previous element at the given index.
      • add

        public void add​(int index,
                        java.lang.Object obj)
        Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
        Specified by:
        add in interface java.util.List
        Overrides:
        add in class java.util.AbstractList
        Parameters:
        index - Index at which the specified element is to be inserted.
        obj - Element to be inserted.
      • remove

        public java.lang.Object remove​(int index)
        Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
        Specified by:
        remove in interface java.util.List
        Overrides:
        remove in class java.util.AbstractList
        Parameters:
        index - The index of the element to remove.
        Returns:
        The element previously at the specified position.
      • maintain

        private void maintain()
        Maintains the collection by removing garbage collected objects.