Class UniqueList<E>
- java.lang.Object
-
- ca.odell.glazedlists.AbstractEventList<E>
-
- ca.odell.glazedlists.TransformedList<E,E>
-
- ca.odell.glazedlists.UniqueList<E>
-
- All Implemented Interfaces:
ListEventListener<E>
,EventList<E>
,java.lang.Iterable<E>
,java.util.Collection<E>
,java.util.EventListener
,java.util.List<E>
public final class UniqueList<E> extends TransformedList<E,E>
AnEventList
that shows the unique elements from its sourceEventList
. For example, the source list {A, A, B, C, C, C, D} would be simplified to {A, B, C, D} by this UniqueList.Warning: This class breaks the contract required by
List
. SeeEventList
for an example.Warning: This class is thread ready but not thread safe. See
EventList
for an example of thread safe code.EventList Overview Writable: yes Concurrency: thread ready, not thread safe Performance: Memory: N/A Unit Tests: N/A Issues: 27 34 35 45 46 55 58 114 - Author:
- Kevin Maltby, James Lemieux, Jesse Wilson
-
-
Field Summary
-
Fields inherited from class ca.odell.glazedlists.TransformedList
source
-
Fields inherited from class ca.odell.glazedlists.AbstractEventList
publisher, readWriteLock, updates
-
-
Constructor Summary
Constructors Constructor Description UniqueList(EventList<E> source)
Creates aUniqueList
that determines uniqueness via theComparable
interface.UniqueList(EventList<E> source, java.util.Comparator<? super E> comparator)
Creates aUniqueList
that determines uniqueness using the specifiedComparator
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <E extends java.lang.Comparable<? super E>>
UniqueList<E>create(EventList<E> source)
Creates aUniqueList
that determines uniqueness via theComparable
interface.void
dispose()
Releases the resources consumed by thisTransformedList
so that it may eventually be garbage collected.java.util.List<E>
getAll(int index)
Returns a List of all original elements represented by the value at the givenindex
within thisUniqueList
.java.util.List<E>
getAll(E value)
Returns a List of all original elements represented by the givenvalue
within thisUniqueList
.int
getCount(int index)
Returns the number of duplicates of the value found at the specified index.int
getCount(E value)
Returns the number of duplicates of the specified value.protected int
getSourceIndex(int index)
Gets the index in the sourceEventList
that corresponds to the specified index.int
indexOf(java.lang.Object element)
Returns the index in this list of the first occurrence of the specifiedelement
, or -1 if this list does not contain thiselement
.protected boolean
isWritable()
Gets whether the sourceEventList
is writable via this API.void
listChanged(ListEvent<E> listChanges)
When the underlying list changes, this notification allows the object to repaint itself or update itself as necessary.E
remove(int index)
Removes the element at the specified position in this list (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
setComparator(java.util.Comparator<? super E> comparator)
Change theComparator
which determines the unique elements of this List.int
size()
Returns the number of elements in this list.-
Methods inherited from class ca.odell.glazedlists.TransformedList
add, addAll, clear, get, removeAll, retainAll
-
Methods inherited from class ca.odell.glazedlists.AbstractEventList
add, addAll, addListEventListener, contains, containsAll, equals, getPublisher, getReadWriteLock, hashCode, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, removeListEventListener, subList, toArray, toArray, toString
-
-
-
-
Constructor Detail
-
UniqueList
public UniqueList(EventList<E> source)
Creates aUniqueList
that determines uniqueness via theComparable
interface. All elements of the sourceEventList
must implementComparable
.Usage of factory method
create(EventList)
is preferable.- Parameters:
source
- theEventList
containing duplicates to remove
-
UniqueList
public UniqueList(EventList<E> source, java.util.Comparator<? super E> comparator)
Creates aUniqueList
that determines uniqueness using the specifiedComparator
.- Parameters:
source
- theEventList
containing duplicates to removecomparator
- theComparator
used to determine equality
-
-
Method Detail
-
create
public static <E extends java.lang.Comparable<? super E>> UniqueList<E> create(EventList<E> source)
Creates aUniqueList
that determines uniqueness via theComparable
interface. All elements of the sourceEventList
must implementComparable
.- Parameters:
source
- theEventList
containing duplicates to remove
-
setComparator
public void setComparator(java.util.Comparator<? super E> comparator)
Change theComparator
which determines the unique elements of this List.- Parameters:
comparator
- theComparator
used to determine groupings; null will be treated asGlazedLists.comparableComparator()
-
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.
-
getSourceIndex
protected int getSourceIndex(int index)
Gets the index in the sourceEventList
that corresponds to the specified index. More formally, returns the index such thatthis.get(i) == source.get(getSourceIndex(i))
for all legal values ofi
.- Overrides:
getSourceIndex
in classTransformedList<E,E>
-
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 interfacejava.util.List<E>
- Overrides:
remove
in classTransformedList<E,E>
- Parameters:
index
- the index of the element to removed.- Returns:
- the element previously at the specified position.
-
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 interfacejava.util.List<E>
- Overrides:
set
in classTransformedList<E,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.
-
indexOf
public int indexOf(java.lang.Object element)
Returns the index in this list of the first occurrence of the specifiedelement
, or -1 if this list does not contain thiselement
. More formally, returns the lowest index i such that uniqueListComparator.compare(get(i), element) == 0, or -1 if there is no such index.Note: This is a departure from the contract for
List.indexOf(java.lang.Object)
since it does not guarantee that element.equals(get(i)) where i is a positive index returned from this method.- Specified by:
indexOf
in interfacejava.util.List<E>
- Overrides:
indexOf
in classAbstractEventList<E>
- Parameters:
element
- the 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
- Throws:
java.lang.ClassCastException
- if the type of the specified element is incompatible with this list
-
isWritable
protected boolean isWritable()
Gets whether the sourceEventList
is writable via this API.Extending classes must override this method in order to make themselves writable.
- Specified by:
isWritable
in classTransformedList<E,E>
-
listChanged
public void listChanged(ListEvent<E> listChanges)
When the underlying list changes, this notification allows the object to repaint itself or update itself as necessary.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 interfaceListEventListener<E>
- Specified by:
listChanged
in classTransformedList<E,E>
- Parameters:
listChanges
- aListEvent
describing the changes to the list
-
getCount
public int getCount(int index)
Returns the number of duplicates of the value found at the specified index.
-
getCount
public int getCount(E value)
Returns the number of duplicates of the specified value.
-
getAll
public java.util.List<E> getAll(int index)
Returns a List of all original elements represented by the value at the givenindex
within thisUniqueList
.
-
getAll
public java.util.List<E> getAll(E value)
Returns a List of all original elements represented by the givenvalue
within thisUniqueList
.
-
dispose
public void dispose()
Releases the resources consumed by thisTransformedList
so that it may eventually be garbage collected.A
TransformedList
will be garbage collected without a call toTransformedList.dispose()
, but not before its sourceEventList
is garbage collected. By callingTransformedList.dispose()
, you allow theTransformedList
to be garbage collected before its sourceEventList
. This is necessary for situations where aTransformedList
is short-lived but its sourceEventList
is long-lived.Warning: It is an error to call any method on a
TransformedList
after it has been disposed.
-
-