ca.odell.glazedlists
Class UniqueList

java.lang.Object
  extended by ca.odell.glazedlists.AbstractEventList
      extended by ca.odell.glazedlists.TransformedList
          extended by ca.odell.glazedlists.UniqueList
All Implemented Interfaces:
ListEventListener, EventList, Iterable, Collection, EventListener, List

public final class UniqueList
extends TransformedList

An EventList that shows the unique elements from its source EventList.

This EventList supports all write operations.

Warning: This class breaks the contract required by List. See EventList for an example.

EventList Overview
Writable:yes
Concurrency:thread ready, not thread safe
Performance:reads: O(log N), writes O(log N)
Memory:72 bytes per element, plus 52 bytes per element ocurring 2 or more times
Unit Tests:N/A
Issues:N/A

Author:
Kevin Maltby

Field Summary
 
Fields inherited from class ca.odell.glazedlists.TransformedList
source
 
Fields inherited from class ca.odell.glazedlists.AbstractEventList
publisher, readWriteLock, updates
 
Constructor Summary
UniqueList(EventList source)
          Creates a UniqueList that determines uniqueness via the Comparable interface.
UniqueList(EventList source, Comparator comparator)
          Creates a UniqueList that determines uniqueness using the specified Comparator.
 
Method Summary
 boolean contains(Object object)
          Returns true if this list contains the specified element.
 void dispose()
          Releases the resources consumed by this TransformedList so that it may eventually be garbage collected.
 List getAll(int index)
          Gets a List of the duplicates of the value at the specified index.
 List getAll(Object value)
          Gets a List of the duplicates of the specified value.
 int getCount(int index)
          Gets the number of duplicates of the value found at the specified index.
 int getCount(Object value)
          Gets the number of duplicates of the specified value.
protected  int getSourceIndex(int index)
          Gets the index in the source EventList that corresponds to the specified index.
 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.
protected  boolean isWritable()
          Gets whether the source EventList is writable via this API.
 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.
 void listChanged(ListEvent listChanges)
          When the underlying list changes, this notification allows the object to repaint itself or update itself as necessary.

The receiving class must iterate through all of the changes in the list change event or else the change objects will remain in memory indefinitely. The easiest way to iterate through the changes is in a while loop like this:


 while(listChanges.next()) {
    
     // get the current change info
    int unsortedIndex = listChanges.getIndex();
    int changeType = listChanges.getType();

    // handle change with the specified index and type
 }
 

It is mandatory that the calling thread has obtained the write lock on the source list. This is because the calling thread will have written to the source list to cause this event. This condition guarantees that no writes can occur while the listener is handling this event. It is an error to write to the source list while processing an event.

 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.
 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).
 void replaceAll(SortedSet revision)
          Replaces the contents of this UniqueList with the contents of the specified SortedSet.
 Object set(int index, Object value)
          Replaces the element at the specified position in this list with the specified element (optional operation).
 int size()
          Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
 
Methods inherited from class ca.odell.glazedlists.TransformedList
add, add, addAll, addAll, clear, get, removeAll, retainAll
 
Methods inherited from class ca.odell.glazedlists.AbstractEventList
addListEventListener, containsAll, equals, getPublisher, getReadWriteLock, hashCode, isEmpty, iterator, listIterator, listIterator, removeListEventListener, subList, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

UniqueList

public UniqueList(EventList source,
                  Comparator comparator)
Creates a UniqueList that determines uniqueness using the specified Comparator.

Parameters:
source - The EventList containing duplicates to remove.
comparator - The Comparator used to determine equality.

UniqueList

public UniqueList(EventList source)
Creates a UniqueList that determines uniqueness via the Comparable interface. All elements of the source EventList must impelement Comparable.

Method Detail

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

getSourceIndex

protected int getSourceIndex(int index)
Gets the index in the source EventList that corresponds to the specified index. More formally, returns the index such that
this.get(i) == source.get(getSourceIndex(i)) for all legal values of i.

Overrides:
getSourceIndex in class TransformedList

isWritable

protected boolean isWritable()
Gets whether the source EventList is writable via this API.

Extending classes must override this method in order to make themselves writable.

Overrides:
isWritable in class TransformedList

listChanged

public void listChanged(ListEvent listChanges)
When the underlying list changes, this notification allows the object to repaint itself or update itself as necessary.

The receiving class must iterate through all of the changes in the list change event or else the change objects will remain in memory indefinitely. The easiest way to iterate through the changes is in a while loop like this:


 while(listChanges.next()) {
    
     // get the current change info
    int unsortedIndex = listChanges.getIndex();
    int changeType = listChanges.getType();

    // handle change with the specified index and type
 }
 

It is mandatory that the calling thread has obtained the write lock on the source list. This is because the calling thread will have written to the source list to cause this event. This condition guarantees that no writes can occur while the listener is handling this event. It is an error to write to the source list while processing an event.

Specified by:
listChanged in interface ListEventListener
Specified by:
listChanged in class TransformedList

getCount

public int getCount(int index)
Gets the number of duplicates of the value found at the specified index.


getCount

public int getCount(Object value)
Gets the number of duplicates of the specified value.


getAll

public List getAll(int index)
Gets a List of the duplicates of the value at the specified index.

Warning: the returned List is only valid until the next list change occurs. If this UniqueList is shared between multiple threads, it is necessary to have aquired the ReadWriteLock while accessing the returned List.


getAll

public List getAll(Object value)
Gets a List of the duplicates of the specified value.

Warning: the returned List is only valid until the next list change occurs. If this UniqueList is shared between multiple threads, it is necessary to have aquired the ReadWriteLock while accessing the returned List.


remove

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

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
Specified by:
remove in interface List
Overrides:
remove in class TransformedList
Parameters:
toRemove - element to be removed from this list, if present.
Returns:
true if this list contained the specified element.

set

public Object set(int index,
                  Object value)
Replaces the element at the specified position in this list with the specified element (optional operation).

Specified by:
set in interface List
Overrides:
set in class TransformedList
Parameters:
index - index of element to replace.
value - element to be stored at the specified position.
Returns:
the element previously at the specified position.

replaceAll

public void replaceAll(SortedSet revision)
Replaces the contents of this UniqueList with the contents of the specified SortedSet. If this UniqueList uses a Comparator to determine equality of elements, the specified SortedList must use an equal Comparator.

Warning: This method is thread ready but not thread safe. See EventList for an example of thread safe code.


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
Specified by:
contains in interface List
Overrides:
contains in class AbstractEventList
Parameters:
object - element whose presence in this list is to be tested.
Returns:
true if this list contains the specified element.

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
Overrides:
indexOf in class AbstractEventList
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
Overrides:
lastIndexOf in class AbstractEventList
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.

dispose

public void dispose()
Releases the resources consumed by this TransformedList so that it may eventually be garbage collected.

A TransformedList will be garbage collected without a call to TransformedList.dispose(), but not before its source EventList is garbage collected. By calling TransformedList.dispose(), you allow the TransformedList to be garbage collected before its source EventList. This is necessary for situations where a TransformedList is short-lived but its source EventList is long-lived.

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

Overrides:
dispose in class TransformedList


Glazed Lists, Copyright © 2003-2005 publicobject.com, O'Dell Engineering.
Generated 2005-09-24 12:46