ca.odell.glazedlists
Class SortedList<E>

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

public final class SortedList<E>
extends TransformedList<E,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.

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), 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
source
 
Fields inherited from class ca.odell.glazedlists.AbstractEventList
publisher, readWriteLock, updates
 
Constructor Summary
SortedList(EventList<E> source)
          Creates a SortedList that sorts the specified EventList.
SortedList(EventList<E> source, Comparator<? super E> comparator)
          Creates a SortedList that sorts the specified EventList using the specified Comparator to determine sort order.
 
Method Summary
 boolean contains(Object object)
          Returns true if this list contains the specified element.
static
<E extends Comparable<? super E>>
SortedList<E>
create(EventList<E> source)
          Creates a SortedList that sorts the specified EventList.
 Comparator<? super E> getComparator()
          Gets the Comparator that is being used to sort this list.
 int getMode()
          Get the behaviour mode for this SortedList.
protected  int getSourceIndex(int mutationIndex)
          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.
 int indexOfSimulated(Object object)
          Deprecated. Deprecated as of 12/11/2005. Replaced with sortIndex(Object) which has cleaner semantics.
protected  boolean isWritable()
          Gets whether the source EventList is writable via this API.
 Iterator<E> iterator()
          Returns an iterator over the elements in this list in proper sequence.
 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.
 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<E> listChanges)
          When the underlying list changes, this notification allows the object to repaint itself or update itself as necessary.
 void setComparator(Comparator<? super 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.
 
Methods inherited from class ca.odell.glazedlists.TransformedList
add, addAll, clear, dispose, get, remove, removeAll, retainAll, set, size
 
Methods inherited from class ca.odell.glazedlists.AbstractEventList
add, addAll, addListEventListener, containsAll, equals, getPublisher, getReadWriteLock, hashCode, isEmpty, listIterator, listIterator, remove, removeListEventListener, subList, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

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.

See Also:
Constant Field Values

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.

See Also:
Constant Field Values
Constructor Detail

SortedList

public SortedList(EventList<E> 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.

Usage of factory method create(EventList) is preferable.

Parameters:
source - the EventList to be sorted

SortedList

public SortedList(EventList<E> source,
                  Comparator<? super 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 Detail

create

public static <E extends Comparable<? super E>> SortedList<E> create(EventList<E> source)
Creates a SortedList that sorts the specified EventList. All elements in the specified EventList must implement Comparable.

Parameters:
source - the EventList to be sorted

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.

getMode

public int getMode()
Get the behaviour mode for this SortedList.

Returns:
one of STRICT_SORT_ORDER (default) or AVOID_MOVING_ELEMENTS.

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 interface ListEventListener<E>
Specified by:
listChanged in class TransformedList<E,E>

getSourceIndex

protected int getSourceIndex(int mutationIndex)
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<E,E>

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.

Specified by:
isWritable in class TransformedList<E,E>

getComparator

public Comparator<? super E> 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.

setComparator

public void setComparator(Comparator<? super E> comparator)
Set the Comparator in use in this EventList. This will sort the source 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.

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.

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(java.lang.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

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(java.lang.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

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.

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.

iterator

public Iterator<E> iterator()
Returns an iterator over the elements in this list in proper sequence.

The returned Iterator will become inconsistent if the EventList that it views is modified. To overcome this problem, use AbstractEventList.listIterator(). When used concurrently, the returned Iterator requires locking via this list's ReadWriteLock.

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in interface List<E>
Overrides:
iterator in class AbstractEventList<E>
Returns:
an iterator over the elements in this list in proper sequence.


Glazed Lists, Copyright © 2003 publicobject.com, O'Dell Engineering.
Documentation build by jessewilson at 2009-01-25 19:21