ca.odell.glazedlists

Class SortedList<E>

Implemented Interfaces:
EventListener, EventList<E>, List, ListEventListener<E>

public final class SortedList<E>
extends TransformedList<S,E>

An EventList that shows its source EventList in sorted order.

The sorting strategy is specified with a Comparator. If no Comparator is specified, all of the elements of the source EventList must implement Comparable.

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), change comparator O(N log N)
Memory:72 bytes per element
Unit Tests:N/A
Issues: 39 40 58 60 62 66 161 170 206 239 255 261
Author:
Jesse Wilson

Field Summary

static int
AVOID_MOVING_ELEMENTS
Sorting mode where elements aren't moved when their value is changed, even if this means they are no longer in perfect sorted order.
static int
STRICT_SORT_ORDER
Sorting mode where elements are always in sorted order, even if this requires that elements be moved from one index to another when their value is changed.

Fields inherited from class ca.odell.glazedlists.TransformedList<S,E>

source

Fields inherited from class ca.odell.glazedlists.AbstractEventList<E>

publisher, readWriteLock, updates

Constructor Summary

SortedList(EventList source)
Creates a SortedList that sorts the specified EventList.
SortedList(EventList source, E> comparator)
Creates a SortedList that sorts the specified EventList using the specified Comparator to determine sort order.

Method Summary

boolean
contains(Object object)
int
getMode()
Get the behaviour mode for this SortedList.
protected int
getSourceIndex(int mutationIndex)
int
indexOf(Object object)
int
indexOfSimulated(Object object)
Deprecated. Deprecated as of 12/11/2005.
protected boolean
isWritable()
Iterator
iterator()
int
lastIndexOf(Object object)
int
lastSortIndex(Object object)
Returns the last index of the object's sort location or the last index at which the object could be positioned if inserted.
void
listChanged(ListEvent listChanges)
void
setComparator(E> comparator)
Set the Comparator in use in this EventList.
void
setMode(int mode)
Modify the behaviour of this SortedList to one of the predefined modes.
int
sortIndex(Object object)
Returns the first index of the object's sort location or the first index at which the object could be positioned if inserted.
Comparator
super E> getComparator()
Gets the Comparator that is being used to sort this list.

Methods inherited from class ca.odell.glazedlists.TransformedList<S,E>

add, addAll, clear, dispose, get, getSourceIndex, isWritable, listChanged, remove, removeAll, retainAll, set, size

Methods inherited from class ca.odell.glazedlists.AbstractEventList<E>

T[] toArray, add, add, addAll, addAll, addListEventListener, clear, contains, containsAll, equals, get, getPublisher, getReadWriteLock, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeListEventListener, retainAll, set, size, subList, toArray, toString

Field Details

AVOID_MOVING_ELEMENTS

public static final int AVOID_MOVING_ELEMENTS
Sorting mode where elements aren't moved when their value is changed, even if this means they are no longer in perfect sorted order. This mode is useful in editable lists and tables because it is annoying for the current element to move if its value changes.
Field Value:
1

STRICT_SORT_ORDER

public static final int STRICT_SORT_ORDER
Sorting mode where elements are always in sorted order, even if this requires that elements be moved from one index to another when their value is changed.
Field Value:
0

Constructor Details

SortedList

public SortedList(EventList source)
Creates a SortedList that sorts the specified EventList. Because this constructor takes no Comparator argument, all elements in the specified EventList must implement Comparable or a ClassCastException will be thrown.

SortedList

public SortedList(EventList source,
                  E> comparator)
Creates a SortedList that sorts the specified EventList using the specified Comparator to determine sort order. If the specified Comparator is null, then this List will be unsorted.

Method Details

contains

public boolean contains(Object object)
Overrides:
contains in interface AbstractEventList<E>

getMode

public int getMode()
Returns:
one of STRICT_SORT_ORDER (default) or AVOID_MOVING_ELEMENTS.

getSourceIndex

protected int getSourceIndex(int mutationIndex)
Overrides:
getSourceIndex in interface TransformedList<S,E>

indexOf

public int indexOf(Object object)
Overrides:
indexOf in interface AbstractEventList<E>

indexOfSimulated

public int indexOfSimulated(Object object)

Deprecated. Deprecated as of 12/11/2005. Replaced with sortIndex(Object) which has cleaner semantics.

Returns the index in this list of the first occurrence of the specified element, or the index where that element would be in the list if it were inserted.
Returns:
the index in this list of the first occurrence of the specified element, or the index where that element would be in the list if it were inserted. This will return a value in [0, size()], inclusive.

isWritable

protected boolean isWritable()
Overrides:
isWritable in interface TransformedList<S,E>

iterator

public Iterator iterator()
Overrides:
iterator in interface AbstractEventList<E>

lastIndexOf

public int lastIndexOf(Object object)
Overrides:
lastIndexOf in interface AbstractEventList<E>

lastSortIndex

public int lastSortIndex(Object object)
Returns the last index of the object's sort location or the last index at which the object could be positioned if inserted.

Unlike lastIndexOf(Object) this method does not guarantee the given object equals the element at the returned index. Instead, they are indistinguishable according to the sorting Comparator.

Returns:
a value in [0, size()] inclusive

listChanged

public void listChanged(ListEvent listChanges)
Specified by:
listChanged in interface ListEventListener<E>

setComparator

public void setComparator(E> comparator)
Set the Comparator in use in this EventList. This will sort the EventList into a new order.

Performance Note: sorting will take O(N * Log N) time.

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

Parameters:
comparator - the Comparator to specify how to sort the list. If the source EventList elements implement Comparable, you may use a GlazedLists.comparableComparator() to sort them in their natural order. You may also specify null to put this SortedList in unsorted order.

setMode

public void setMode(int mode)
Modify the behaviour of this SortedList to one of the predefined modes.
Parameters:
mode - either STRICT_SORT_ORDER or AVOID_MOVING_ELEMENTS.

sortIndex

public int sortIndex(Object object)
Returns the first index of the object's sort location or the first index at which the object could be positioned if inserted.

Unlike indexOf(Object) this method does not guarantee the given object equals the element at the returned index. Instead, they are indistinguishable according to the sorting Comparator.

Returns:
a value in [0, size()] inclusive

super E> getComparator

public Comparator getComparator()
Gets the Comparator that is being used to sort this list.
Returns:
the Comparator in use, or null if this list is currently unsorted. If this is an EventList of Comparable elements in natural order, then a ComparableComparator} will be returned.

Glazed Lists, Copyright © 2003-2006 publicobject.com, O'Dell Engineering.
Documentation build by pbuilder at 2007-12-21 23:49