com.levigo.util.base.glazedlists
Class DebugList<E>

java.lang.Object
  extended by com.levigo.util.base.glazedlists.AbstractEventList<E>
      extended by com.levigo.util.base.glazedlists.DebugList<E>
All Implemented Interfaces:
EventList<E>, Iterable<E>, Collection<E>, 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:

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

Field Summary
 
Fields inherited from class com.levigo.util.base.glazedlists.AbstractEventList
publisher, readWriteLock, updates
 
Constructor Summary
DebugList()
          Constructs a DebugList which, by default, performs no debugging.
 
Method Summary
 boolean add(E value)
          Appends the specified element to the end of this list (optional operation).
 void add(int index, E value)
          Inserts the specified element at the specified position in this list (optional operation).
 boolean addAll(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).
 boolean addAll(int index, Collection<? extends E> values)
          Inserts all of the elements in the specified collection into this list at the specified position (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(Object object)
          Returns true if this list contains the specified element.
 boolean containsAll(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(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.
 Set<Thread> getSanctionedReaderThreads()
          Returns the Set of Threads that are allowed to perform reads on this DebugList.
 Set<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(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(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(Object toRemove)
          Removes the first occurrence in this list of the specified element (optional operation).
 boolean removeAll(Collection<?> values)
          Removes from this list all the elements that are contained in the specified collection (optional operation).
 boolean retainAll(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.
 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.
 String toString()
          Returns a string representation of this collection.
 
Methods inherited from class com.levigo.util.base.glazedlists.AbstractEventList
addListEventListener, iterator, listIterator, listIterator, removeListEventListener, subList
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

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 Set<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 Set<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.


getReadWriteLock

public ReadWriteLock getReadWriteLock()
Gets the lock required to share this list between multiple threads.

Specified by:
getReadWriteLock in interface EventList<E>
Overrides:
getReadWriteLock in class AbstractEventList<E>
Returns:
a re-entrant ReadWriteLock that guarantees thread safe access to this list.

getPublisher

public ListEventPublisher getPublisher()
Get the publisher used to distribute ListEvents.

Specified by:
getPublisher in interface EventList<E>
Overrides:
getPublisher in class AbstractEventList<E>

get

public E get(int index)
Returns the element at the specified position in this list.

Specified by:
get in interface 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 Collection<E>
Specified by:
size in interface List<E>
Specified by:
size in class AbstractEventList<E>
Returns:
the number of elements in this list.

contains

public boolean contains(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 Collection<E>
Specified by:
contains in interface 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(Collection<?> collection)
Returns true if this list contains all of the elements of the specified collection.

Specified by:
containsAll in interface Collection<E>
Specified by:
containsAll in interface 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(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 Collection<E>
Specified by:
equals in interface 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 Collection<E>
Specified by:
hashCode in interface 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(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 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(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 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 Collection<E>
Specified by:
isEmpty in interface List<E>
Overrides:
isEmpty in class AbstractEventList<E>
Returns:
true if this list contains no elements.

toArray

public 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 Collection<E>
Specified by:
toArray in interface 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 Collection<E>
Specified by:
toArray in interface 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 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 Collection<E>
Specified by:
add in interface 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(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 Collection<E>
Specified by:
remove in interface 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(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 Collection<E>
Specified by:
addAll in interface 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,
                      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 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(Collection<?> values)
Removes from this list all the elements that are contained in the specified collection (optional operation).

Specified by:
removeAll in interface Collection<E>
Specified by:
removeAll in interface 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(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 Collection<E>
Specified by:
retainAll in interface 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 Collection<E>
Specified by:
clear in interface 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 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 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 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.



Copyright © 1995-2020 levigo holding gmbh. All Rights Reserved.