Class DebugList<E>

  • All Implemented Interfaces:
    EventList<E>, java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>

    public class DebugList<E>
    extends AbstractEventList<E>
    DebugList is meant to be used as a drop-in replacement for BasicEventList at the root of pipelines of EventLists during development. It provides methods for turning on various types of assertions which throw RuntimeExceptions when they are violated. The goal is to detect and fail fast on error conditions in much the same way Iterators commonly throw ConcurrentModificationExceptions.

    Some of the assertions that are controlled by this DebugList include:

    • setLockCheckingEnabled(boolean) toggles whether this DebugList asserts that all read operations are guarded by read locks and all write operations are guarded by write locks.
    • getSanctionedReaderThreads() is the Set of Threads which are allowed to read from the DebugList. If the Set is empty then ALL Threads are assumed to be sanctioned readers.
    • getSanctionedWriterThreads() is the Set of Threads which are allowed to write to the DebugList. If the Set is empty then ALL Threads are assumed to be sanctioned writers.
    This class is left non-final to allow subclassing but since it is a debugging tool, we make no guarantees about backward compatibility between releases. It is meant to evolve as users discover new assertions to be added.
    Author:
    James Lemieux
    • Constructor Summary

      Constructors 
      Constructor Description
      DebugList()
      Constructs a DebugList which, by default, performs no debugging.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, E value)
      Inserts the specified element at the specified position in this list (optional operation).
      boolean add​(E value)
      Appends the specified element to the end of this list (optional operation).
      boolean addAll​(int index, java.util.Collection<? extends E> values)
      Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
      boolean addAll​(java.util.Collection<? extends E> values)
      Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).
      protected void afterReadOperation()
      This method is currently a no-op and exists for parity.
      protected void afterWriteOperation()
      This method is currently a no-op and exists for parity.
      protected void beforeReadOperation()
      This generic method is called immediately before any read operation is invoked.
      protected void beforeWriteOperation()
      This generic method is called immediately before any write operation is invoked.
      void clear()
      Removes all of the elements from this list (optional operation).
      boolean contains​(java.lang.Object object)
      Returns true if this list contains the specified element.
      boolean containsAll​(java.util.Collection<?> collection)
      Returns true if this list contains all of the elements of the specified collection.
      <E> DebugList<E> createNewDebugList()
      Returns a new empty DebugList which shares the same ListEventListener and ReadWriteLock with this DebugList.
      void dispose()
      Disposing an EventList will make it eligible for garbage collection.
      boolean equals​(java.lang.Object object)
      Compares the specified object with this list for equality.
      E get​(int index)
      Returns the element at the specified position in this list.
      ListEventPublisher getPublisher()
      Get the publisher used to distribute ListEvents.
      ReadWriteLock getReadWriteLock()
      Gets the lock required to share this list between multiple threads.
      java.util.Set<java.lang.Thread> getSanctionedReaderThreads()
      Returns the Set of Threads that are allowed to perform reads on this DebugList.
      java.util.Set<java.lang.Thread> getSanctionedWriterThreads()
      Returns the Set of Threads that are allowed to perform writes on this DebugList.
      int hashCode()
      Returns the hash code value for this list.
      int indexOf​(java.lang.Object object)
      Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
      boolean isEmpty()
      Returns true if this list contains no elements.
      boolean isLockCheckingEnabled()
      Returns true if DebugList is currently checking the calling Thread for lock ownership before each read and write operation.
      int lastIndexOf​(java.lang.Object object)
      Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
      E remove​(int index)
      Removes the element at the specified position in this list (optional operation).
      boolean remove​(java.lang.Object toRemove)
      Removes the first occurrence in this list of the specified element (optional operation).
      boolean removeAll​(java.util.Collection<?> values)
      Removes from this list all the elements that are contained in the specified collection (optional operation).
      boolean retainAll​(java.util.Collection<?> values)
      Retains only the elements in this list that are contained in the specified collection (optional operation).
      E set​(int index, E value)
      Replaces the element at the specified position in this list with the specified element (optional operation).
      void setLockCheckingEnabled​(boolean lockCheckingEnabled)
      If lockCheckingEnabled is true this DebugList will check the calling Thread for lock ownership before each read and write operation; false indicates this check shouldn't be performed.
      int size()
      Returns the number of elements in this list.
      java.lang.Object[] toArray()
      Returns an array containing all of the elements in this list in proper sequence.
      <T> T[] toArray​(T[] array)
      Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array.
      java.lang.String toString()
      Returns a string representation of this collection.
      • 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

        replaceAll, sort, spliterator
    • Constructor Detail

      • DebugList

        public DebugList()
        Constructs a DebugList which, by default, performs no debugging. It must be customized after construction to turn on the assertions which make sense for the list pipeline.
    • Method Detail

      • isLockCheckingEnabled

        public boolean isLockCheckingEnabled()
        Returns true if DebugList is currently checking the calling Thread for lock ownership before each read and write operation.
      • setLockCheckingEnabled

        public void setLockCheckingEnabled​(boolean lockCheckingEnabled)
        If lockCheckingEnabled is true this DebugList will check the calling Thread for lock ownership before each read and write operation; false indicates this check shouldn't be performed.
      • getSanctionedReaderThreads

        public java.util.Set<java.lang.Thread> getSanctionedReaderThreads()
        Returns the Set of Threads that are allowed to perform reads on this DebugList. If the Set is empty, all Threads are allowed to read from this DebugList. Users are expected to add and remove Threads directly on this Set.
      • getSanctionedWriterThreads

        public java.util.Set<java.lang.Thread> getSanctionedWriterThreads()
        Returns the Set of Threads that are allowed to perform writes on this DebugList. If the Set is empty, all Threads are allowed to write to this DebugList. Users are expected to add and remove Threads directly on this Set.
      • createNewDebugList

        public <E> DebugList<E> createNewDebugList()
        Returns a new empty DebugList which shares the same ListEventListener and ReadWriteLock with this DebugList. This method is particularly useful when debugging a CompositeList where some member lists are DebugLists and thus must share an identical publisher and locks in order to participate in the CompositeList.
      • beforeReadOperation

        protected void beforeReadOperation()
        This generic method is called immediately before any read operation is invoked. All generic read assertions should take place here.
      • afterReadOperation

        protected void afterReadOperation()
        This method is currently a no-op and exists for parity. Subclasses may choose to insert extract assertion logic here.
      • beforeWriteOperation

        protected void beforeWriteOperation()
        This generic method is called immediately before any write operation is invoked. All generic write assertions should take place here.
      • afterWriteOperation

        protected void afterWriteOperation()
        This method is currently a no-op and exists for parity. Subclasses may choose to insert extract assertion logic here.
      • get

        public E get​(int index)
        Returns the element at the specified position in this list.
        Specified by:
        get in interface java.util.List<E>
        Specified by:
        get in class AbstractEventList<E>
        Parameters:
        index - index of element to return.
        Returns:
        the element at the specified position in this list.
      • size

        public int size()
        Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in interface java.util.List<E>
        Specified by:
        size in class AbstractEventList<E>
        Returns:
        the number of elements in this list.
      • contains

        public boolean contains​(java.lang.Object object)
        Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
        Specified by:
        contains in interface java.util.Collection<E>
        Specified by:
        contains in interface java.util.List<E>
        Overrides:
        contains in class AbstractEventList<E>
        Parameters:
        object - element whose presence in this list is to be tested.
        Returns:
        true if this list contains the specified element.
      • containsAll

        public boolean containsAll​(java.util.Collection<?> collection)
        Returns true if this list contains all of the elements of the specified collection.
        Specified by:
        containsAll in interface java.util.Collection<E>
        Specified by:
        containsAll in interface java.util.List<E>
        Overrides:
        containsAll in class AbstractEventList<E>
        Parameters:
        collection - collection to be checked for containment in this list.
        Returns:
        true if this list contains all of the elements of the specified collection.
        See Also:
        AbstractEventList.contains(Object)
      • equals

        public boolean equals​(java.lang.Object object)
        Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.
        Specified by:
        equals in interface java.util.Collection<E>
        Specified by:
        equals in interface java.util.List<E>
        Overrides:
        equals in class AbstractEventList<E>
        Parameters:
        object - the object to be compared for equality with this list.
        Returns:
        true if the specified object is equal to this list.
      • hashCode

        public int hashCode()
        Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:
          hashCode = 1;
          Iterator i = list.iterator();
          while (i.hasNext()) {
              Object obj = i.next();
              hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
          }
         
        This ensures that list1.equals(list2) implies that list1.hashCode()==list2.hashCode() for any two lists, list1 and list2, as required by the general contract of Object.hashCode.
        Specified by:
        hashCode in interface java.util.Collection<E>
        Specified by:
        hashCode in interface java.util.List<E>
        Overrides:
        hashCode in class AbstractEventList<E>
        Returns:
        the hash code value for this list.
        See Also:
        Object.hashCode(), Object.equals(Object), AbstractEventList.equals(Object)
      • indexOf

        public int indexOf​(java.lang.Object object)
        Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
        Specified by:
        indexOf in interface java.util.List<E>
        Overrides:
        indexOf in class AbstractEventList<E>
        Parameters:
        object - element to search for.
        Returns:
        the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object object)
        Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
        Specified by:
        lastIndexOf in interface java.util.List<E>
        Overrides:
        lastIndexOf in class AbstractEventList<E>
        Parameters:
        object - element to search for.
        Returns:
        the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
      • isEmpty

        public boolean isEmpty()
        Returns true if this list contains no elements.
        Specified by:
        isEmpty in interface java.util.Collection<E>
        Specified by:
        isEmpty in interface java.util.List<E>
        Overrides:
        isEmpty in class AbstractEventList<E>
        Returns:
        true if this list contains no elements.
      • toArray

        public java.lang.Object[] toArray()
        Returns an array containing all of the elements in this list in proper sequence. Obeys the general contract of the Collection.toArray method.
        Specified by:
        toArray in interface java.util.Collection<E>
        Specified by:
        toArray in interface java.util.List<E>
        Overrides:
        toArray in class AbstractEventList<E>
        Returns:
        an array containing all of the elements in this list in proper sequence.
        See Also:
        Arrays.asList(T...)
      • toArray

        public <T> T[] toArray​(T[] array)
        Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array. Obeys the general contract of the Collection.toArray(Object[]) method.
        Specified by:
        toArray in interface java.util.Collection<E>
        Specified by:
        toArray in interface java.util.List<E>
        Overrides:
        toArray in class AbstractEventList<E>
        Parameters:
        array - the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
        Returns:
        an array containing the elements of this list.
      • toString

        public java.lang.String toString()
        Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

        This implementation creates an empty string buffer, appends a left square bracket, and iterates over the collection appending the string representation of each element in turn. After appending each element except the last, the string ", " is appended. Finally a right bracket is appended. A string is obtained from the string buffer, and returned.

        Overrides:
        toString in class AbstractEventList<E>
        Returns:
        a string representation of this collection.
      • add

        public boolean add​(E value)
        Appends the specified element to the end of this list (optional operation).

        Lists that support this operation may place limitations on what elements may be added to this list. In particular, some lists will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. List classes should clearly specify in their documentation any restrictions on what elements may be added.

        Specified by:
        add in interface java.util.Collection<E>
        Specified by:
        add in interface java.util.List<E>
        Overrides:
        add in class AbstractEventList<E>
        Parameters:
        value - element to be appended to this list.
        Returns:
        true (as per the general contract of the Collection.add method).
      • remove

        public boolean remove​(java.lang.Object toRemove)
        Removes the first occurrence in this list of the specified element (optional operation). If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).
        Specified by:
        remove in interface java.util.Collection<E>
        Specified by:
        remove in interface java.util.List<E>
        Overrides:
        remove in class AbstractEventList<E>
        Parameters:
        toRemove - element to be removed from this list, if present.
        Returns:
        true if this list contained the specified element.
      • addAll

        public boolean addAll​(java.util.Collection<? extends E> values)
        Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)
        Specified by:
        addAll in interface java.util.Collection<E>
        Specified by:
        addAll in interface java.util.List<E>
        Overrides:
        addAll in class AbstractEventList<E>
        Parameters:
        values - collection whose elements are to be added to this list.
        Returns:
        true if this list changed as a result of the call.
        See Also:
        AbstractEventList.add(Object)
      • addAll

        public boolean addAll​(int index,
                              java.util.Collection<? extends E> values)
        Inserts all of the elements in the specified collection into this list at the specified position (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are returned by the specified collection's iterator. The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)
        Specified by:
        addAll in interface java.util.List<E>
        Overrides:
        addAll in class AbstractEventList<E>
        Parameters:
        index - index at which to insert first element from the specified collection.
        values - elements to be inserted into this list.
        Returns:
        true if this list changed as a result of the call.
      • removeAll

        public boolean removeAll​(java.util.Collection<?> values)
        Removes from this list all the elements that are contained in the specified collection (optional operation).
        Specified by:
        removeAll in interface java.util.Collection<E>
        Specified by:
        removeAll in interface java.util.List<E>
        Overrides:
        removeAll in class AbstractEventList<E>
        Parameters:
        values - collection that defines which elements will be removed from this list.
        Returns:
        true if this list changed as a result of the call.
        See Also:
        AbstractEventList.remove(Object), AbstractEventList.contains(Object)
      • retainAll

        public boolean retainAll​(java.util.Collection<?> values)
        Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all the elements that are not contained in the specified collection.
        Specified by:
        retainAll in interface java.util.Collection<E>
        Specified by:
        retainAll in interface java.util.List<E>
        Overrides:
        retainAll in class AbstractEventList<E>
        Parameters:
        values - collection that defines which elements this set will retain.
        Returns:
        true if this list changed as a result of the call.
        See Also:
        AbstractEventList.remove(Object), AbstractEventList.contains(Object)
      • clear

        public void clear()
        Removes all of the elements from this list (optional operation). This list will be empty after this call returns (unless it throws an exception).
        Specified by:
        clear in interface java.util.Collection<E>
        Specified by:
        clear in interface java.util.List<E>
        Overrides:
        clear in class AbstractEventList<E>
      • set

        public E set​(int index,
                     E value)
        Replaces the element at the specified position in this list with the specified element (optional operation).
        Specified by:
        set in interface java.util.List<E>
        Overrides:
        set in class AbstractEventList<E>
        Parameters:
        index - index of element to replace.
        value - element to be stored at the specified position.
        Returns:
        the element previously at the specified position.
      • add

        public void add​(int index,
                        E value)
        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<E>
        Overrides:
        add in class AbstractEventList<E>
        Parameters:
        index - index at which the specified element is to be inserted.
        value - element to be inserted.
      • remove

        public E 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<E>
        Overrides:
        remove in class AbstractEventList<E>
        Parameters:
        index - the index of the element to removed.
        Returns:
        the element previously at the specified position.
      • dispose

        public void dispose()
        Disposing an EventList will make it eligible for garbage collection. Some EventLists install themselves as listeners to related objects so disposing them is necessary.

        Warning: It is an error to call any method on an EventList after it has been disposed.