|
| Components |
|
| |||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||||||
com.sas.collection.SortedCollection
public class SortedCollection
SortedCollection is a collection which inserts items
in a sort order defined by a Comparator.
The sorted collection is constructed with a Comparator which
is used to determine where new items added to the collection
should be placed.
The behavior of a SortedCollection is undefined if
the Comparator is null or if the comparator cannot compare two items
(i.e. if the comparator ever returns Comparator.INCOMPARABLE)
See also TreeMap and TreeSet which provide
alternate data structures for storing objects in sorted order.
Sample
The example below shows constructing a SortedCollection
which uses a StringComparator
to keep the collection of strings
sorted. A BufferedReader reader
is used to read lines of text from a stream.
The lines are added to the collection, which keeps them sorted.
The enumeration will return the items in sorted order,
as does the get(int) method.
import com.sas.collection.SortedCollection; import com.sas.util.StringComparator; ... SortedCollection lines = newMerging Sorted Collections If you have twoSortedCollection(StringComparator.defaultInstance); try { String line; while ( (line = reader.readLine()) != null)lines.add(line); } catch (IOException ioe) { } for (int i = 0, len =lines.count(); i < len; i++) System.out.println(lines.get(i)); // Or, use an enumeration: for (Enumeration sortedLines =lines.getItems(); sortedLines.hasMoreElements(); ) System.out.println(sortedLines.nextElement());
SortedCollections which you wish to merge,
simply call addItems on the first, passing an Enumeration of the
items from the second:
collection1.addItems(collection2.getItems());
Converting a Sorted Collection into an Ordered Collection
If you have two SortedCollections which you wish to convert into an Ordered Collection
simply call addItems on a new OrderedCollection,
passing an Enumeration of the items from the second:
OrderedCollection oc = new OrderedCollection(); oc.addItems(collection2.getItems());
SortedCollection,
Serialized Form| Field Summary | |
|---|---|
protected com.sas.collection.AVLTree |
avlTree
|
static java.lang.String |
RB_KEY
|
| Fields inherited from class com.sas.collection.BaseCollection |
|---|
allContentsChangedListeners, mappingObject |
| Constructor Summary | |
|---|---|
SortedCollection()
Create a default SortedCollection. |
|
SortedCollection(com.sas.util.Comparator comparator)
Create a SortedCollection which uses the specified
comparator to maintain the sort order. |
|
SortedCollection(com.sas.util.Comparator comparator,
boolean allowDuplicates)
Create a SortedCollection which uses the specified
comparator to maintain the sort order. |
|
SortedCollection(java.util.Enumeration e,
com.sas.util.Comparator comparator,
boolean allowDuplicates)
Create a SortedCollection from the contents of an array. |
|
SortedCollection(java.lang.Object[] array,
com.sas.util.Comparator comparator,
boolean allowDuplicates)
Create a SortedCollection from the contents of an array. |
|
SortedCollection(StaticCollectionInterface items,
com.sas.util.Comparator comparator,
boolean allowDuplicates)
Create a SortedCollection from the contents of an array. |
|
SortedCollection(java.lang.String data,
com.sas.util.Comparator comparator,
boolean allowDuplicates)
Construct an SortedCollection from a string of comma delimited values |
|
| Method Summary | |
|---|---|
void |
add(java.lang.Object item)
Add an item to this collection. |
void |
addItems(java.util.Enumeration items)
Add all elements of an enumeration to this collection. |
void |
apply(com.sas.util.ApplyInterface action)
Applies the action to every item in the collection. |
java.lang.Object |
clone()
Clone the object. |
boolean |
contains(java.lang.Object element)
Test for the presence of an object in this collection |
int |
count()
Returns the number of items in the collection |
boolean |
equals(java.lang.Object object)
Compare to another object. |
boolean |
equals(StaticOrderedCollectionInterface orderedCollection)
Compare the current items with those in another collection and tell whether the collections are identical. |
java.lang.Object |
get(int index)
Get a value via an integer index. |
com.sas.util.Comparator |
getComparator()
Return the comparator used to maintain the sort order. |
static com.sas.beans.ExtendedBeanInfo |
getExtendedBeanInfo()
|
int |
getIndex(java.lang.Object element,
int startIndex)
Returns the index of the first occurrence of the item. |
java.util.Enumeration |
getItems()
Return an enumeration of all the elements in this collection |
java.util.Enumeration |
getItems(int start,
int end)
Returns an enumeration of a subset of the items from a collection. |
int |
getLastIndex(java.lang.Object element,
int startIndex)
Returns the index of the last occurrence of the element. |
protected void |
invalidate()
Marks the SortedCollection as modified -- done after adding or removing items. |
boolean |
isDuplicatesAllowed()
Return the duplicatesAllowed setting |
protected com.sas.util.Comparator |
newComparator()
A Factory Method for constructing the comparator. |
boolean |
remove(java.lang.Object element)
Remove an item from this collection. |
void |
removeAll()
Remove all items from this collection |
int |
removeAll(java.lang.Object element)
Remove an all occurrences of an item from this collection. |
void |
removeAt(int index)
Remove the item at the specificed position. |
void |
setComparator(com.sas.util.Comparator newComparator)
Set the comparator used to maintain the sort order. |
void |
setDuplicatesAllowed(boolean duplicatesAllowed)
Specify whether the sorted collection allows duplicates or not |
void |
sort()
Re-sort the collection. |
java.lang.Object[] |
toArray()
Return a new array of objects containing the contents of this collection. |
java.lang.String |
toString()
Convert this ordered collection to a single string. |
| Methods inherited from class com.sas.collection.BaseCollection |
|---|
addContentsChangedListener, anyContentsChangedListeners, disableEvents, enableEvents, enableEvents, fireContentsChanged, map, removeContentsChangedListener, unmap |
| Methods inherited from interface com.sas.collection.ContentsChangedSource |
|---|
addContentsChangedListener, removeContentsChangedListener |
| Methods inherited from interface com.sas.beans.PropertyChangeSource |
|---|
addPropertyChangeListener, removePropertyChangeListener |
| Field Detail |
|---|
protected com.sas.collection.AVLTree avlTree
public static final java.lang.String RB_KEY
| Constructor Detail |
|---|
public SortedCollection()
newComparator(),
and allow duplicates.
public SortedCollection(java.lang.Object[] array,
com.sas.util.Comparator comparator,
boolean allowDuplicates)
array - a non-null array of elements.comparator - the comparator used to maintain the sort order.
It is used whenever a new item is added to the collection, or
when resorting the collection.allowDuplicates - if true, the collection allows insertion
of duplicate values (i.e. values that are EQUALS according to
the comparator. If false, this collection behaves
like a sorted set.
public SortedCollection(java.util.Enumeration e,
com.sas.util.Comparator comparator,
boolean allowDuplicates)
e - a non-null enumeration of elements.comparator - the comparator used to maintain the sort order.
It is used whenever a new item is added to the collection, or
when resorting the collection.allowDuplicates - if true, the collection allows insertion
of duplicate values (i.e. values that are EQUALS according to
the comparator. If false, this collection behaves
like a sorted set.
public SortedCollection(StaticCollectionInterface items,
com.sas.util.Comparator comparator,
boolean allowDuplicates)
items - a non-null collection of elements.comparator - the comparator used to maintain the sort order.
It is used whenever a new item is added to the collection, or
when resorting the collection.allowDuplicates - if true, the collection allows insertion
of duplicate values (i.e. values that are EQUALS according to
the comparator. If false, this collection behaves
like a sorted set.
public SortedCollection(java.lang.String data,
com.sas.util.Comparator comparator,
boolean allowDuplicates)
SortedCollection from a string of comma delimited values
data - a string of values, separated by commas.
Booleans, character literals and numbers are recognized and parsed into
appropriate java.lang.Boolean, java.lang.Character, java.lang.Integer, java.lang.Long,
or java.lang.Double objects (for either the or the value);
everything else is parsed into a String.comparator - the comparator used to maintain the sort order.
It is used whenever a new item is added to the collection, or
when resorting the collection.allowDuplicates - if true, the collection allows insertion
of duplicate values (i.e. values that are EQUALS according to
the comparator. If false, this collection behaves
like a sorted set.
java.lang.IllegalArgumentException - if the data string
is invalidpublic SortedCollection(com.sas.util.Comparator comparator)
SortedCollection which uses the specified
comparator to maintain the sort order. The default allows
duplicates.
comparator - the comparator used to maintain the sort order.
It is used whenever a new item is added to the collection, or
when resorting the collection.
public SortedCollection(com.sas.util.Comparator comparator,
boolean allowDuplicates)
SortedCollection which uses the specified
comparator to maintain the sort order.
comparator - the comparator used to maintain the sort order.
It is used whenever a new item is added to the collection, or
when resorting the collection.allowDuplicates - if true, the collection allows insertion
of duplicate values (i.e. values that are EQUALS according to
the comparator. If false, this collection behaves
like a sorted set.| Method Detail |
|---|
protected com.sas.util.Comparator newComparator()
GenericComparator. Subclasses
may override this in order to define a different
default comparator.
public static com.sas.beans.ExtendedBeanInfo getExtendedBeanInfo()
public void setDuplicatesAllowed(boolean duplicatesAllowed)
duplicatesAllowed - if true, duplicate values may be added.
If false, adding an item that already exists in the collection
will fail silently. Duplicates are two items such that the
comparator returns EQUALS.
Note: if this changes from true to false, the duplicates
are removed from the collection.sort(),
Comparator.EQUALSpublic boolean isDuplicatesAllowed()
public void setComparator(com.sas.util.Comparator newComparator)
newComparator - the comparator used to maintain the sort order.sort()public com.sas.util.Comparator getComparator()
public void sort()
sort in interface SortedCollectionInterfacepublic void removeAt(int index)
removeAt in interface SortedCollectionInterfaceindex - the index of the item to remove from the sorted collection.public void add(java.lang.Object item)
add in interface CollectionInterfaceitem - an object to add to this collection.
java.lang.ClassCastException - In some implementations,
the objects that are stored may be restricted to a particular
type (such as String). (a RuntimeException)public void addItems(java.util.Enumeration items)
addItems in interface CollectionInterfaceitems - an Enumeration of items to add to this collection
java.lang.ClassCastException - In some implementations,
the objects that are stored may be restricted to a particular
type (such as String). (a RuntimeException)public boolean remove(java.lang.Object element)
remove in interface CollectionInterfaceelement - the item to remove
java.lang.ClassCastException - In some implementations,
the objects that are stored may be restricted to a particular
type (such as String). (a RuntimeException)public void removeAll()
removeAll in interface CollectionInterfacepublic int removeAll(java.lang.Object element)
removeAll in interface CollectionInterfaceelement - the item to remove
public boolean equals(StaticOrderedCollectionInterface orderedCollection)
equals() method is used to compare
collection items.
equals in interface StaticOrderedCollectionInterfaceorderedCollection - Another ordered collection to be compared to.
public boolean equals(java.lang.Object object)
equals in class java.lang.Objectobject - another object
StaticOrderedCollectionInterface,
return equals((StaticOrderedCollectionInterface) other), else return false
public int getIndex(java.lang.Object element,
int startIndex)
getIndex in interface StaticOrderedCollectionInterfaceelement - item to search forstartIndex - the position where the search should start
java.lang.IndexOutOfBoundsException - if the index is
not in the range [0..count()-1].
java.lang.ClassCastException - In some implementations,
the objects that are stored may be restricted to a particular
type (such as String).
public java.util.Enumeration getItems(int start,
int end)
getItems in interface StaticOrderedCollectionInterfacestart - the index of the first item, zero based.end - one past the index of the last item to include in the enumeration, zero based,
or -1 to indicate the enumeration should include all remaining elements.
Note that the range of elements returned in the Enumeration does not
include the end item; this is consistent with String.substring(start, end)
for example.
java.lang.IndexOutOfBoundsException - if start
is not in the range [0, count()-1], or start > last
or [0, count()]
public int getLastIndex(java.lang.Object element,
int startIndex)
index == count()-1)
to the start of the list until an item is found.
getLastIndex in interface StaticOrderedCollectionInterfaceelement - the item to search forstartIndex - the position where the search should start
java.lang.IndexOutOfBoundsException - if the index is
not in the range [0..count()-1]
java.lang.ClassCastException - In some implementations,
the objects that are stored may be restricted to a particular
type (such as String). (a RuntimeException)public void apply(com.sas.util.ApplyInterface action)
sort() this collection.
apply in interface StaticCollectionInterfaceapply in class BaseCollectionaction - An object that provides a function which takes an
object as an argument and then does something to it.
action.apply(pair) is excecuted on successive values
in this collection until all values have been operated on, or until the method
returns ApplyInterface.ABORTpublic boolean contains(java.lang.Object element)
contains in interface StaticCollectionInterfacecontains in class BaseCollectionelement - the item to search for
java.lang.ClassCastException - In some implementations,
the objects that are stored may be restricted to a particular
type (such as String). (a RuntimeException)StaticCollectionInterface.contains(java.lang.Object)
public java.lang.Object clone()
throws java.lang.CloneNotSupportedException
clone in interface CollectionInterfaceclone in interface StaticCollectionInterfaceclone in interface StaticOrderedCollectionInterfaceclone in interface com.sas.PublicClonableclone in class BaseCollectionjava.lang.CloneNotSupportedException - if the clone fails.protected void invalidate()
public java.lang.Object get(int index)
get in interface IndexedGetInterfaceindex - the index of the item to fetch.
java.lang.IndexOutOfBoundsException - The implementor of this interface may throw
this RuntimeException if the index is not valid.public int count()
count in interface com.sas.util.Countablecount in class BaseCollectionpublic java.util.Enumeration getItems()
BaseCollection
getItems in interface com.sas.util.EnumerablegetItems in class BaseCollectionhasMoreElements()
returns false.Enumerable.getItems()public java.lang.String toString()
toString in class java.lang.Objectpublic java.lang.Object[] toArray()
null items. The array may have 0 length
if this collection is empty.
|
| Components |
|
| |||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||||||