|
| Components |
|
| |||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||||||
com.sas.collection.OrderedCollection
public class OrderedCollection
An ordered collection is a collection which accesses items via consecutive integer indices. These indices range from 0 to n, where n=collection.count()-1.
An ordered collection allows insertion or deletion from anywhere in the collection.
The OrderedCollection uses an efficient implementation that allows for
contiguous insertion and deletion in constant time (whereas java.util.Vector will have O(n)
behavior when inserting or deleting at the beginning of the Vector.) For example,
inserting many items at the same location (the beginning or the end, or repeatdedly at some other fixed index)
in an OrderedCollection is very efficient.
The OrderedListCollection uses a double linked list implementation. You should choose
this class if you wish to do frequent inserts and deletes in a non-contiguous manner
and otherwise access the items sequentially instead of randomly.
As with all collections, the OrderedCollection fires a
ContentsChangedEvent when the contents changes. OrderedCollection
implements the ContentsChangedSource interface, which defines
methods to add and remove ContentsChangedEvent listeners. To allow
more efficient processing of the event, OrderedCollection
may send an instance of
com.sas.collection.CollectionChangedInfo
which provides more detailed information about the change, including
which items changed (the range of indices) and how the collection
was changed (i.e. items were added, removed, replaced, or reordered).
If you have a view of the collection, you can use this information
to more efficiently handle the change, rather than refreshing the
view based on a complete refresh.
OrderedListCollection,
CollectionChangedInfo,
Serialized Form| Field Summary | |
|---|---|
static java.lang.String |
RB_KEY
|
| Fields inherited from class com.sas.collection.BaseCollection |
|---|
allContentsChangedListeners, mappingObject |
| Constructor Summary | |
|---|---|
OrderedCollection()
Default Constructor. |
|
OrderedCollection(int initialCapacity)
Construct an OrderedCollection with the specified initial capacity. |
|
OrderedCollection(int initialSize,
int capacityIncrement)
Construct an OrderedCollection with the specified initial capacity. |
|
OrderedCollection(java.lang.Object[] array)
Construct an OrderedCollection which always maps to an existing array. |
|
OrderedCollection(java.lang.Object[] array,
int capacityIncrement)
Construct an OrderedCollection which initially maps to an existing array. |
|
OrderedCollection(java.lang.String data)
Construct an OrderedCollection from a string of comma delimited values |
|
| Method Summary | |
|---|---|
void |
add(java.lang.Object item)
Add an item to a collection. |
void |
add(java.lang.Object item,
int index)
Insert an item into a specific location in this collection. |
void |
addItems(java.util.Enumeration items)
Add all elements of an enumeration to this collection. |
void |
addItems(java.util.Enumeration items,
int index)
Add all elements of an Enumeration to this collection. |
void |
append(int index,
java.lang.Object item)
If this collection contains fewer than index+1 items, perform a setSize(index+1) first. |
void |
apply(com.sas.util.ApplyInterface action)
Applies an action to every item in the current collection. |
java.lang.Object |
clone()
Clone the object. |
java.lang.Object |
clone(com.sas.DeepCloneSupport support,
boolean deeply)
Clone this object using a deep copy. |
protected int |
computeNewCapacity()
Compute the amount by which to grow this collection. |
boolean |
contains(java.lang.Object item)
Test the existence of an object in a collection. |
int |
count()
Returns the number of items in this collection |
void |
dumpComponent()
Prints attribute information about the class. |
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)
Fetch the item at the specified index. |
static com.sas.beans.ExtendedBeanInfo |
getExtendedBeanInfo()
|
int |
getIndex(java.lang.Object item,
int startIndex)
Returns the index of the first occurrence of the item. |
java.util.Enumeration |
getItems()
Return an enumeration of all the elements; the enumeration contains duplicates if this collection contains duplicates. |
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 item,
int startIndex)
Returns the index of the last occurrence of the item. |
protected void |
moveHole(int newHolePosition)
Move the hole to a new position. |
protected void |
moveHoleEnsureCapacity(int newHolePosition,
int capacity)
Move the hole to a new location, while ensuring the resulting data vector has the specified data capacity. |
boolean |
remove(java.lang.Object item)
Remove an item from this collection. |
void |
removeAll()
Remove all items from this collection. |
int |
removeAll(java.lang.Object item)
Remove all occurrences of an item from this collection. |
java.lang.Object |
removeAt(int index)
Remove from this collection the item at the specified index. |
void |
set(int index,
java.lang.Object item)
Replace the item at the specified index with a new item. |
void |
setSize(int newSize)
Set the number of items that are being held in this collection. |
void |
sort(com.sas.util.Comparator comparator)
Sorts the current collection in place using the provided comparison function. |
void |
sort(com.sas.util.Comparator comparator,
int start,
int end)
Sorts a range of the current collection in place using the provided comparison function. |
static void |
sort(OrderedCollectionInterface collection,
com.sas.util.Comparator comparator,
int start,
int end)
Sorts a range of items in a collection in place using the provided comparison function. |
static int[] |
sort(OrderedCollectionInterface collection,
int algorithm,
com.sas.util.Comparator comparator,
int start,
int end,
int[] permutation)
Sorts a range of items in a collection in place using the provided comparison function. |
java.lang.Object[] |
toArray()
Return a new array of objects containing the contents of this collection. |
static java.lang.Object |
toArray(IndexedGetInterface collection)
Return a new array of objects containing the contents of the 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 |
|---|
public static final java.lang.String RB_KEY
| Constructor Detail |
|---|
public OrderedCollection()
OrderedCollection(8,0)
public OrderedCollection(int initialCapacity)
OrderedCollection with the specified initial capacity.
The collection is still empty, and it may grow beyond this initial
capacity. Use this constructor form if you have a good estimate of
the number of items you will put in the collection. The collection
will use default capacity growth, which is automatic
doubling of the capacity.
initialCapacity - the initial capacity. Room for this many
elements is initially allocated.
public OrderedCollection(int initialSize,
int capacityIncrement)
OrderedCollection with the specified initial capacity.
The collection is still empty, and it may grow beyond this initial
capacity. Use this constructor form if you have a good estimate of
the number of items you will put in the collection.
initialCapacity - the initial capacity. Room for this many
elements is initially allocated.capacityIncrement - how much the collection should grow each
time an add operation occurs when the collection is full.
If capacityIncrement is 0, the collection
will use default capacity growth, which is automatic
doubling of the capacity.
If capacityIncrement is negative, the collection will never
exceed the initialCapacity; possibly throwing an exception
if an operation such as an add is attempted.public OrderedCollection(java.lang.Object[] array)
OrderedCollection which always maps to an existing array.
The collection cannot grow beyond the current array size
and will always map onto the array. (Attempts to grow the
array through add operations, etc., will throw exceptions.)
Items in the array will be replaced with null if items are deleted
from the collection, but there is no guarantee that items will
be "compressed" towards the front; i.e. a gap may be left in the array
after deleting items.
array - a non-null array of objects.
public OrderedCollection(java.lang.Object[] array,
int capacityIncrement)
OrderedCollection which initially maps to an existing array.
Items in the array will be replaced with null if items are deleted
from the collection, but there is no guarantee that items will
be "compressed" towards the front; i.e. a gap may be left in the array
after deleting items.
array - a non-null array of objects.capacityIncrement - how to handle growth. If you add to the collection,
the collection will grow by this much, effectively severing the mapping
to the array, since a new array must be allocated.
Use capacityIncrement == -1 to ensure the collection always
maps to the array.public OrderedCollection(java.lang.String data)
OrderedCollection 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.
java.lang.IllegalArgumentException - if the data string
is invalid| Method Detail |
|---|
public static com.sas.beans.ExtendedBeanInfo getExtendedBeanInfo()
protected int computeNewCapacity()
java.lang.IndexOutOfBoundsExceptions - if
capacityIncrement is less than 0.protected final void moveHole(int newHolePosition)
An OrderedCollection uses the buffer/hole algorithm
for managing a sequence of objects which allows
relativelyy efficient insertion/deletion of characters
anywhere in the buffer, while maintaining O(1) access to
any element. Simple array based implementations
suffer in that additions/deletions anywhere but at the end
are expensive, since they require shifting 1/2 the array
on average, so add/remove operations are of O(n)
complexity, where n == the length of the data.
The hole algorithm takes advantage of the efficiency of adding/deleting items at the end of the array and makes it applicable anywhere. Basically, in a standard array implementation, N of M characters of an array of size M are "in use" and the remaining M-N items at the end of the array are unused and available. The hole algorithms simply moves this block of unused items, the "hole", such that it always appears after the point of the last insertion/deletion. Subsequent insertions/deletions at the same point are therefore O(1) operations.
For example, let '=' represent a hole in a character string.
In a buffer of 8 characters representing
the N=3 character string
"abc", the buffer actually contains "abc=====". The position
of the hole is at location 3. So far, that's no different
from the array implementation: we can delete the 'c'
or append a 'd' pretty easily.
However, to delete the 'b' at position 1, we first shift
the hole to immediately after the 'b',
to yield "ab=====c",
then we delete 'b'; the internal
representation becomes "a======c". The hole is no
longer after the data but right in its midst. Note, however,
that we can insert one or more items at position 1
without shifting the data: add(1, '1'); add(2, '2');
add(3, '3') will result in subsequent intermediate results:
"a1=====c", "a12====c"f and
"a123===c", with the hole shrinking
and moving to the right each time. Only when we insert/delete
an item at some location other than where the hole is do
we incur the penalty of shifting data.
Internally, the method is the workhorse, with moveHoleEnsureCapacity(int newHolePosition,
int capacity)moveHole(int newHolePosition)
doing a lot of work as well. moveHole will move the hole
such that it appears after new position. This is done
before deleting an item. For example, moveHole(0) will
reuslt in "a===123c" and is a necessary precursor to deleting
item 0, which is accomplished by simply decrementing the item
count, replacing the item with the default value, and
decrementing the holePosition. This is O(1).
moveHoleEnsureCapacity(int, int) will perform the moveHole operation
and ensure there is enough room for an insert.
It does both operations at the same time to avoid unnecessary
copying of data that would otherwise be necessary if the hole
were moved first (moving up to N items), then reallocating
the array (moving N items again), then shifting the
items (moving N items again).
newHolePosition - where the hole should appear.
The hole appears before this item; i.e.
after moveHole(0), the hole is before item 0.
After moveHole(count), the hole is after the last
item (which is at position (count - 1).
This must be a value in the range [0,count]
or you'll get an ArrayIndexOutOfBoundsExceptionmoveHoleEnsureCapacity(int, int)
protected final void moveHoleEnsureCapacity(int newHolePosition,
int capacity)
moveHoleEnsureCapacity will perform the moveHole operation
and ensure there is enough room for an insert.
It does both operations at the same time to avoid unnecessary
copying of data that would otherwise be necessary if the hole
were moved first (moving up to N items), then reallocating
the array (moving N items again), then shifting the
items (moving N items again).
newHolePosition - where the hole should appear.
This must be a value in the range [0,count]
or you'll get an ArrayIndexOutOfBoundsException
The hole appears before this item; i.e.
after moveHole(0), the hole is before item 0.
After moveHole(count), the hole is after the last
item (which is at position (count - 1).capacity - how large you need the resulting data buffer
to be.moveHole(int)public void add(java.lang.Object item)
add in interface CollectionInterfaceitem - an object to add to this collection
public void add(java.lang.Object item,
int index)
add in interface OrderedCollectionInterfaceitem - the item to add to this collection.
The item may exist multiple times in this collection.index - insert the object before the indexed item. The
newly inserted item can be accessed by get(index) afterwards.
java.lang.IndexOutOfBoundsException - if index
is not in the range [0, count()].
public void addItems(java.util.Enumeration items,
int index)
Enumeration to this collection.
Note: to add the contents of another collection, use
targetCollection.addItems(otherCollection.getItems(), index);
addItems in interface OrderedCollectionInterfaceitems - an Enumeration of items to add to this collectionindex - insert the objects starting before the indexed item. The
first of the newly inserted item can be accessed by get(index)
afterwards.
java.lang.ClassCastException - In some implementations,
the objects that are stored may be restricted to a particular
type (such as String).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).public void apply(com.sas.util.ApplyInterface action)
apply in interface StaticCollectionInterfaceapply in class BaseCollectionaction - An object that performs some action on another object.
action.apply(item) is excecuted on successive items
in this collection until all items have been operated on, or until the method
returns ApplyInterface.ABORTpublic void removeAll()
setSize(0)
removeAll in interface CollectionInterfacepublic boolean contains(java.lang.Object item)
contains in interface StaticCollectionInterfacecontains in class BaseCollectionitem - the item to search for
StaticCollectionInterface.contains(java.lang.Object)public int count()
count in interface com.sas.util.Countablecount in class BaseCollectionpublic void dumpComponent()
dumpComponent in interface ComponentInterfacedumpComponent in class ComponentComponentInterface.dumpComponent()public java.lang.Object[] toArray()
null items. The array may have 0 length
if this collection is empty.public static java.lang.Object toArray(IndexedGetInterface collection)
collection - an object implementing the IndexedGetInterface
null items. The array may have 0 length
if this collection is empty.public boolean equals(StaticOrderedCollectionInterface orderedCollection)
Util.equal(java.util.Enumeration, java.util.Enumeration)
method is used to compare collections.
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 falsepublic java.lang.Object get(int index)
get in interface IndexedGetInterfaceindex - The position of the item, zero-based indexing.
java.lang.IndexOutOfBoundsException - if index
is not in the range [0, count()-1].
public int getIndex(java.lang.Object item,
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.
Note that the range of elements returned in the Enumeration does not
include the enditem; this is consistent with
substring(start, end)
for example.
For example, OrderedCollectionEnumeration(0, 5)
will return items 0 through 4.
Be sure to use -1 and not collection.count() if you want all remaining items, because if you use collection.count() and then add items before exhausting the enumeration, the new items will not be returned by the enumeration -- it will stop at the value of collection.count() when the enumeration was created.
java.lang.IndexOutOfBoundsException - if start
is not in the range [0, count()-1], or start>end
or end is not in the range [0, count()]public java.util.Enumeration getItems()
getItems in interface com.sas.util.EnumerablegetItems in class BaseCollectionEnumerable.getItems()
public int getLastIndex(java.lang.Object item,
int startIndex)
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 boolean remove(java.lang.Object item)
getIndex(Object item, int 0)
then invokes removeAt(int index).
remove in interface CollectionInterfaceitem - the item to remove
public int removeAll(java.lang.Object item)
removeAll in interface CollectionInterfaceitem - the item to remove
public java.lang.Object removeAt(int index)
removeAt in interface OrderedCollectionInterfaceindex - the position of the item, zero-based indexing.
java.lang.IndexOutOfBoundsException - if index
is not in the range [0, count()-1].
public void set(int index,
java.lang.Object item)
set in interface OrderedCollectionInterfaceset in interface IndexedSetInterfaceindex - The position where the Object will be placed.item - The Object to be added to this ordered collection.
java.lang.IndexOutOfBoundsException - if index
is not in the range [0, count()-1].
public void append(int index,
java.lang.Object item)
index - The position where the object will be placed.
If necessary, this collection is extended to
accommodate the item.item - the object to be added to this ordered collection.public void setSize(int newSize)
setSize in interface OrderedCollectionInterfacenewSize - The new size of this collection
java.lang.IndexOutOfBoundsException - if index is less than zero.
public static void sort(OrderedCollectionInterface collection,
com.sas.util.Comparator comparator,
int start,
int end)
comparator - An instance of a subclass of Comparator which provides a
compare(Object,Object) function.start - the index of the first item in the range to sortend - sort the items between start and (end-1)Comparator
public static int[] sort(OrderedCollectionInterface collection,
int algorithm,
com.sas.util.Comparator comparator,
int start,
int end,
int[] permutation)
collection - a collection to sortalgorithm - which algorithm to use? See com.sas.util.Sortcomparator - An instance of a subclass of Comparator which provides a
compare(Object,Object) function.start - the index of the first item in the range to sortend - sort the items between start and (end-1)permutation - an array of indexes which lists the reordering
(permutation) performed by the sort. The caller should initialize
the permutation array
int[] permutation = new int[c.count()];
for (int i = permutation.length-1; i >= 0; i--)
permutation[i] = i;
before calling sort(), then read the results when sort is done.
This sort method does not initialize but only permutes the integers.
For example, permutation[start]
will indicate the index in the sorted collection where the initial
collection.get(start) can be found after the sort.
This permutation can be used to permute (reorder) other collections.
permutation.length should be at least (end - 1).
The sort will only change elements permutation[start]
through permutation[end - 1]Permuter,
Comparator
public void sort(com.sas.util.Comparator comparator,
int start,
int end)
sort in interface Sortablecomparator - An instance of a subclass of Comparator which provides a
compare(Object,Object) function.start - the index of the first item in the range to sortend - sort the items between start and (end-1)Comparatorpublic void sort(com.sas.util.Comparator comparator)
sort in interface Sortablecomparator - An instance of a subclass of Comparator which provides a
compare(Object,Object) function.Comparator
public java.lang.Object clone()
throws java.lang.CloneNotSupportedException
clone in interface CollectionInterfaceclone in interface OrderedCollectionInterfaceclone in interface StaticCollectionInterfaceclone in interface StaticOrderedCollectionInterfaceclone in interface com.sas.PublicClonableclone in class BaseCollectionjava.lang.CloneNotSupportedException - if the clone fails.
public java.lang.Object clone(com.sas.DeepCloneSupport support,
boolean deeply)
throws java.lang.CloneNotSupportedException
DeepClonable
This object and all the objects it contains
are cloned, preserving the same structure.
clone in interface com.sas.DeepClonablesupport - a required object which keeps track
of cloned objects during the deep cloning.deeply - If true, clone recursively using the deep clone support.
if false, this is equivalent to a normal clone.
java.lang.CloneNotSupportedExceptionpublic java.lang.String toString()
toString in class java.lang.Object
|
| Components |
|
| |||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||||||