Class GroupingList<E>
- java.lang.Object
-
- ca.odell.glazedlists.AbstractEventList<E>
-
- ca.odell.glazedlists.TransformedList<E,java.util.List<E>>
-
- ca.odell.glazedlists.GroupingList<E>
-
- All Implemented Interfaces:
ListEventListener<E>
,EventList<java.util.List<E>>
,java.lang.Iterable<java.util.List<E>>
,java.util.Collection<java.util.List<E>>
,java.util.EventListener
,java.util.List<java.util.List<E>>
public final class GroupingList<E> extends TransformedList<E,java.util.List<E>>
A grouping list contains elements which are themselves Lists. Those Lists are infact elements of the source list which have been grouped together into a List. The logic of how to group the source elements is specified via a Comparator. Elements for which the Comparator returns 0 are guaranteed to be contained within the same group within this GroupingList. This implies that source elements may only participate in a single group within this GroupingList.Further transformations may be layered on top of this GroupingList to transform the group lists into any other desirable form.
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: reads: O(log N), writes O(log N) Memory: Unit Tests: GroupingListTest Issues: 281 491 - Author:
- James Lemieux
-
-
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 GroupingList(EventList<E> source)
Creates aGroupingList
that determines groupings via theComparable
interface which all elements of thesource
are assumed to implement.GroupingList(EventList<E> source, java.util.Comparator<? super E> comparator)
Creates aGroupingList
that determines groups using the specifiedComparator
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(int index, java.util.List<E> value)
This version of add will distribute all elements within the givenvalue
List into groups.static <E extends java.lang.Comparable<? super E>>
GroupingList<E>create(EventList<E> source)
Creates aGroupingList
that determines groupings via theComparable
interface which all elements of thesource
are assumed to implement.void
dispose()
Releases the resources consumed by thisTransformedList
so that it may eventually be garbage collected.java.util.List<E>
get(int index)
Returns the element at the specified position in this list.protected int
getSourceIndex(int index)
Gets the index in the sourceEventList
that corresponds to the specified index.int
indexOfGroup(E groupElement)
Return the index of the group to which thegroupElement
would belong if it were hypothetically added to the source list.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.java.util.List<E>
remove(int index)
Removes the element at the specified position in this list (optional operation).java.util.List<E>
set(int index, java.util.List<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 groupings presented by this Listint
size()
Returns the number of elements in this list.-
Methods inherited from class ca.odell.glazedlists.TransformedList
addAll, clear, removeAll, retainAll
-
Methods inherited from class ca.odell.glazedlists.AbstractEventList
add, addAll, addListEventListener, contains, containsAll, equals, getPublisher, getReadWriteLock, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, removeListEventListener, subList, toArray, toArray, toString
-
-
-
-
Constructor Detail
-
GroupingList
public GroupingList(EventList<E> source)
Creates aGroupingList
that determines groupings via theComparable
interface which all elements of thesource
are assumed to implement.Usage of factory method
create(EventList)
is preferable.
-
GroupingList
public GroupingList(EventList<E> source, java.util.Comparator<? super E> comparator)
Creates aGroupingList
that determines groups using the specifiedComparator
.- Parameters:
source
- theEventList
containing elements to be groupedcomparator
- theComparator
used to determine groupings
-
-
Method Detail
-
create
public static <E extends java.lang.Comparable<? super E>> GroupingList<E> create(EventList<E> source)
Creates aGroupingList
that determines groupings via theComparable
interface which all elements of thesource
are assumed to implement.
-
indexOfGroup
public int indexOfGroup(E groupElement)
Return the index of the group to which thegroupElement
would belong if it were hypothetically added to the source list. Note thatgroupElement
does NOT have to exist in a group. This method is essentially a convenient way to locate a group based on a prototypical element of that group.- Parameters:
groupElement
- a prototype element of the group to locate- Returns:
- the index of the group that would contain
groupElement
if it were added to the source list or-1
if no currently existing group would contain thegroupElement
-
setComparator
public void setComparator(java.util.Comparator<? super E> comparator)
Change theComparator
which determines the groupings presented by this List- Parameters:
comparator
- theComparator
used to determine groupings; null will be treated asGlazedLists.comparableComparator()
-
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,java.util.List<E>>
-
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,java.util.List<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,java.util.List<E>>
- Parameters:
listChanges
- aListEvent
describing the changes to the list
-
get
public java.util.List<E> get(int index)
Returns the element at the specified position in this list.- Specified by:
get
in interfacejava.util.List<E>
- Overrides:
get
in classTransformedList<E,java.util.List<E>>
- Parameters:
index
- index of element to return.- Returns:
- the element at the specified position in this list.
-
remove
public java.util.List<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,java.util.List<E>>
- Parameters:
index
- the index of the element to removed.- Returns:
- the element previously at the specified position.
-
set
public java.util.List<E> set(int index, java.util.List<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,java.util.List<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, java.util.List<E> value)
This version of add will distribute all elements within the givenvalue
List into groups. Existing groups will be reused and new groups will be created as needed. As such, theindex
argument is meaningless.Warning: This method breaks the contract required by
List.add(int, Object)
.- Specified by:
add
in interfacejava.util.List<E>
- Overrides:
add
in classTransformedList<E,java.util.List<E>>
- Parameters:
index
- index at which the specified element is to be inserted.value
- element to be inserted.
-
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.
-
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.
-
-