Class SortedList<E>
- java.lang.Object
-
- ca.odell.glazedlists.AbstractEventList<E>
-
- ca.odell.glazedlists.TransformedList<E,E>
-
- ca.odell.glazedlists.SortedList<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 SortedList<E> extends TransformedList<E,E>
AnEventListthat shows its sourceEventListin sorted order.The sorting strategy is specified with a
Comparator. If noComparatoris specified, all of the elements of the sourceEventListmust implementComparable.This
EventListsupports all write operations.Warning: This class breaks the contract required by
List. SeeEventListfor an example.Warning: This class is thread ready but not thread safe. See
EventListfor 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
Fields Modifier and Type Field Description static intAVOID_MOVING_ELEMENTSSorting mode where elements aren't moved when their value is changed, even if this means they are no longer in perfect sorted order.static intSTRICT_SORT_ORDERSorting 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
Constructors Constructor Description SortedList(EventList<E> source)Creates aSortedListthat sorts the specifiedEventList.SortedList(EventList<E> source, java.util.Comparator<? super E> comparator)Creates aSortedListthat sorts the specifiedEventListusing the specifiedComparatorto determine sort order.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleancontains(java.lang.Object object)Returns true if this list contains the specified element.static <E extends java.lang.Comparable<? super E>>
SortedList<E>create(EventList<E> source)Creates aSortedListthat sorts the specifiedEventList.java.util.Comparator<? super E>getComparator()Gets theComparatorthat is being used to sort this list.intgetMode()Get the behaviour mode for thisSortedList.protected intgetSourceIndex(int mutationIndex)Gets the index in the sourceEventListthat corresponds to the specified index.intindexOf(java.lang.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.intindexOfSimulated(java.lang.Object object)Deprecated.Deprecated as of 12/11/2005.protected booleanisWritable()Gets whether the sourceEventListis writable via this API.java.util.Iterator<E>iterator()Returns an iterator over the elements in this list in proper sequence.intlastIndexOf(java.lang.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.intlastSortIndex(java.lang.Object object)Returns the last index of theobject's sort location or the last index at which theobjectcould be positioned if inserted.voidlistChanged(ListEvent<E> listChanges)When the underlying list changes, this notification allows the object to repaint itself or update itself as necessary.voidsetComparator(java.util.Comparator<? super E> comparator)Set theComparatorin use in thisEventList.voidsetMode(int mode)Modify the behaviour of thisSortedListto one of the predefined modes.intsortIndex(java.lang.Object object)Returns the first index of theobject's sort location or the first index at which theobjectcould 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
-
-
-
-
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 aSortedListthat sorts the specifiedEventList. Because this constructor takes noComparatorargument, all elements in the specifiedEventListmust implementComparableor aClassCastExceptionwill be thrown.Usage of factory method
create(EventList)is preferable.- Parameters:
source- theEventListto be sorted
-
SortedList
public SortedList(EventList<E> source, java.util.Comparator<? super E> comparator)
Creates aSortedListthat sorts the specifiedEventListusing the specifiedComparatorto determine sort order. If the specifiedComparatorisnull, then thisListwill be unsorted.
-
-
Method Detail
-
create
public static <E extends java.lang.Comparable<? super E>> SortedList<E> create(EventList<E> source)
Creates aSortedListthat sorts the specifiedEventList. All elements in the specifiedEventListmust implementComparable.- Parameters:
source- theEventListto be sorted
-
setMode
public void setMode(int mode)
Modify the behaviour of thisSortedListto one of the predefined modes.- Parameters:
mode- eitherSTRICT_SORT_ORDERorAVOID_MOVING_ELEMENTS.
-
getMode
public int getMode()
Get the behaviour mode for thisSortedList.- Returns:
- one of
STRICT_SORT_ORDER(default) orAVOID_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:
listChangedin interfaceListEventListener<E>- Specified by:
listChangedin classTransformedList<E,E>- Parameters:
listChanges- aListEventdescribing the changes to the list
-
getSourceIndex
protected int getSourceIndex(int mutationIndex)
Gets the index in the sourceEventListthat corresponds to the specified index. More formally, returns the index such thatthis.get(i) == source.get(getSourceIndex(i))for all legal values ofi.- Overrides:
getSourceIndexin classTransformedList<E,E>
-
isWritable
protected boolean isWritable()
Gets whether the sourceEventListis writable via this API.Extending classes must override this method in order to make themselves writable.
- Specified by:
isWritablein classTransformedList<E,E>
-
getComparator
public java.util.Comparator<? super E> getComparator()
Gets theComparatorthat is being used to sort this list.- Returns:
- the
Comparatorin use, or null if this list is currently unsorted. If this is anEventListofComparableelements in natural order, then a ComparableComparator} will be returned.
-
setComparator
public void setComparator(java.util.Comparator<? super E> comparator)
Set theComparatorin use in thisEventList. This will sort the sourceEventListinto a new order.Performance Note: sorting will take
O(N * Log N)time.Warning: This method is thread ready but not thread safe. See
EventListfor an example of thread safe code.- Parameters:
comparator- theComparatorto specify how to sort the list. If the sourceEventListelements implementComparable, you may use aGlazedLists.comparableComparator()to sort them in their natural order. You may also specifynullto put thisSortedListin unsorted order.
-
indexOf
public int indexOf(java.lang.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:
indexOfin interfacejava.util.List<E>- Overrides:
indexOfin classAbstractEventList<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(java.lang.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:
lastIndexOfin interfacejava.util.List<E>- Overrides:
lastIndexOfin classAbstractEventList<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(java.lang.Object object)
Returns the first index of theobject's sort location or the first index at which theobjectcould be positioned if inserted.Unlike
indexOf(java.lang.Object)this method does not guarantee the givenobjectequalsthe element at the returned index. Instead, they are indistinguishable according to the sortingComparator.- Returns:
- a value in [0, size()] inclusive
-
lastSortIndex
public int lastSortIndex(java.lang.Object object)
Returns the last index of theobject's sort location or the last index at which theobjectcould be positioned if inserted.Unlike
lastIndexOf(java.lang.Object)this method does not guarantee the givenobjectequalsthe element at the returned index. Instead, they are indistinguishable according to the sortingComparator.- Returns:
- a value in [0, size()] inclusive
-
indexOfSimulated
public int indexOfSimulated(java.lang.Object object)
Deprecated.Deprecated as of 12/11/2005. Replaced withsortIndex(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(java.lang.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:
containsin interfacejava.util.Collection<E>- Specified by:
containsin interfacejava.util.List<E>- Overrides:
containsin classAbstractEventList<E>- Parameters:
object- element whose presence in this list is to be tested.- Returns:
- true if this list contains the specified element.
-
iterator
public java.util.Iterator<E> iterator()
Returns an iterator over the elements in this list in proper sequence.The returned
Iteratorwill become inconsistent if theEventListthat it views is modified. To overcome this problem, useAbstractEventList.listIterator(). When used concurrently, the returnedIteratorrequires locking via this list'sReadWriteLock.
-
-