Class powersoft.jcm.util.SortableVector
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class powersoft.jcm.util.SortableVector

java.lang.Object
   |
   +----powersoft.jcm.util.SortableVector

public class SortableVector
extends Object
implements Cloneable
A general-purpose vector class. The size of the vector is dynamic and increased as necessary. The elements of the vector may or may not be sorted. Each element of the vector is a (null or non-null) reference to an Object instance. There may be duplicates in the vector.

Notes: The following methods invoke the equals method on the elements to determine equality:

Note that if null values are stored in the vector, the equals method of any object should return false when a null value is supplied.

The following methods require a user-defined comparison function (compare routine) to determine the order among objects. The compare routine must have been specifie before these methods can be invoked. Otherwise, a runtime exception will be thrown.

The compare routine must be an object derived from the interface CompareRoutine.

See Also:
CompareRoutine

Variable Index

 o _count
The number of objects stored in the vector.
 o _growBy
The default amount to grow the array used in #grow.
 o _objs
The internal array storing the objects.
 o _quickSortThreshold
If the size of an array is less than the threshold, insertion sort will be used instead of quick sort.
 o _routine
The compare routine.

Constructor Index

 o SortableVector()
Constructs an empty vector.
 o SortableVector(CompareRoutine)
Constructs an empty vector and sets the compare routine as specified.
 o SortableVector(int)
Constructs a vector initially containing the specified number of null values.
 o SortableVector(Object[])
Constructs a vector from the elements of the specified array.
 o SortableVector(Vector)
Constructs a vector from the elements of the specified vector.

Method Index

 o append(Object)
Append an object to the end of the vector.
 o binarySearch(Object)
Performs a binary search for the specified object.
 o clear()
Clears the vector by setting all elements to null.
 o clone()
Creates a clone of the vector.
 o equals(Object)
Determines whether this vector is equal to the specified object.
 o equals(Object, Object)
 o equals(SortableVector)
Determines whether this vector is equal to the specified sortable vector.
 o getArray()
Gets a shallow copy of the internal array
 o getCapacity()
Determines the capacity of the vector.
 o getCompareRoutine()
Gets the comparison routine.
 o getCount()
Determines the number of objects currently stored in the vector.
 o getElement(int)
Retrieves the object stored at the specified index.
 o getElements()
Gets an enumeration of the elements of the vector.
 o getLowerBound(Object)
 o getMedian(Object, Object, Object)
 o grow()
Increases the capacity of the vector by the default amount (10 elements).
 o grow(int)
Increases the capacity by the specified amount.
 o index(Object)
Finds the index of the specified object.
 o insert(int, Object)
Inserts an object at the specified position.
 o insert(int, Object, boolean)
Inserts an object at the specified position.
 o insert(Object)
Inserts the specified object in sorted order.
 o insert(Object, boolean)
Inserts the specified object in sorted order.
 o partition(int, int, Object)
 o quickSort(int, int)
 o remove(Object)
Removes the specifed object from the vector.
 o removeAt(int)
Removes the object at the specified index.
 o removeAtIndex(int)
 o setArray(Object[])
Makes a shallow copy of the given array and stores it in the vector.
 o setCompareRoutine(CompareRoutine)
Specifies the comparison routine.
 o setElement(int, Object)
Stores an object in the specified element.
 o sort()
Sorts the vector using the comparison routine specified by the CompareRoutine property.
 o sortTwo(int)
 o toString()
Generates a string representation of the contents of the vector.

Variables

 o _growBy
  protected final static int _growBy
The default amount to grow the array used in #grow.
 o _quickSortThreshold
  protected final static int _quickSortThreshold
If the size of an array is less than the threshold, insertion sort will be used instead of quick sort.
 o _count
  protected int _count
The number of objects stored in the vector.
 o _objs
  protected Object _objs[]
The internal array storing the objects.
 o _routine
  protected CompareRoutine _routine
The compare routine.

Constructors

 o SortableVector
  public SortableVector()
Constructs an empty vector.
 o SortableVector
  public SortableVector(int initialSize)
Constructs a vector initially containing the specified number of null values.
Parameters:
initialSize - Specifies the initial size of the vector.
 o SortableVector
  public SortableVector(Object array[])
Constructs a vector from the elements of the specified array.
Parameters:
array - Specifies an array of Objects.
 o SortableVector
  public SortableVector(Vector vec)
Constructs a vector from the elements of the specified vector.
Parameters:
vec - Specifies the vector whose elements are copies to the new vector.
 o SortableVector
  public SortableVector(CompareRoutine routine)
Constructs an empty vector and sets the compare routine as specified.
Parameters:
routine - Specifies an initial compare routine.

Methods

 o append
  public synchronized int append(Object object)
Append an object to the end of the vector.
Parameters:
object - Specifies the object to be appended to the vector.
Returns:
The index of the new item. If object is null, -1 is returned.
 o binarySearch
  public synchronized int binarySearch(Object object)
Performs a binary search for the specified object.

Notes: The vector must be sorted.

Parameters:
object - The object to search for.
Returns:
The index of the object or -1 if the object is not found.
Throws: InvalidStateException
if the CompareRoutine property has not be set for the vector.
See Also:
setCompareRoutine
 o clear
  public synchronized void clear()
Clears the vector by setting all elements to null.

Notes: The Count property to set to 0. The Capacity property is not changed.

See Also:
getCount, getCapacity
 o grow
  public void grow()
Increases the capacity of the vector by the default amount (10 elements).
 o grow
  public synchronized void grow(int growBy)
Increases the capacity by the specified amount.
Parameters:
growBy - Specifies the number of elements by which to increase the capacity.
Throws: IllegalArgumentException
if (code>growBy < 0.
 o index
  public synchronized int index(Object object)
Finds the index of the specified object.
Parameters:
object - Specifies the object whose index is be determined.
Returns:
The index of the object, or -1 if the object cannot be found.
See Also:
binarySearch
 o insert
  public synchronized int insert(Object object)
Inserts the specified object in sorted order.

Notes: The vector must be sorted. Duplicate objects will be inserted.

Parameters:
object - The object to be inserted.
Returns:
The index of the inserted object.
See Also:
append
 o insert
  public synchronized int insert(Object object,
                                 boolean allowsDuplicates)
Inserts the specified object in sorted order.

Notes: The vector must be sorted. Duplicate objects will be inserted if allowsDuplicates is true.

Parameters:
object - The object to be inserted.
allowsDuplicates - If true, duplicate objects are permitted. If a copy of object already exists, another copy will be inserted. If false, duplicate objects are not permitted. If the object already exists, another copy is not inserted.
Returns:
The index of the inserted object, or -1 if the object could not be inserted.
Throws: InvalidStateException
is thrown if the CompareRoutine property has not been set.
See Also:
setCompareRoutine
 o insert
  public synchronized int insert(int index,
                                 Object object)
Inserts an object at the specified position.

Notes: If the vector already contains the object, a duplicate copy will be inserted.

Parameters:
index - Specifies the zero-base index of the object.
object - The object to insert.
Returns:
The index of the inserted object.
 o insert
  public synchronized int insert(int index,
                                 Object object,
                                 boolean allowsDuplicates)
Inserts an object at the specified position.

Notes: If the vector already contains the specified object, a duplicate copy will be added if allowsDuplicates is true.

Parameters:
index - Specifies the zero-based index of the object.
object - The object to insert.
allowsDuplicates - If true, duplicate objects are permitted. If a copy of object already exists, another copy will be inserted. If false, duplicate objects are not permitted. If the object already exists, another copy is not inserted.
Returns:
The index is the inserted object.
Throws: ArrayIndexOutOfBoundsException
if index is not between 0 and getCount() inclusive.
 o remove
  public synchronized Object remove(Object object)
Removes the specifed object from the vector.
Parameters:
object - The object to be removed.
Returns:
The object that was removed from the vector, or null if the object cannot be found.
 o removeAt
  public synchronized Object removeAt(int index)
Removes the object at the specified index.
Parameters:
index - The index of the object to be removed.
Returns:
The object that was removed from the vector, or null if the object cannot be found.
Throws: ArrayIndexOutOfBoundsException
if index is not valid.
 o sort
  public synchronized void sort()
Sorts the vector using the comparison routine specified by the CompareRoutine property.
Throws: InvalidStateException
if the CompareRoutine property has not been set.
See Also:
setCompareRoutine
 o clone
  public synchronized Object clone()
Creates a clone of the vector. The elements of the original vector are copied to the clone.
Returns:
The cloned vector.
Overrides:
clone in class Object
 o equals
  public synchronized boolean equals(Object obj)
Determines whether this vector is equal to the specified object. The object must be an instance of a sortable vector.

Notes: The vectors are equal if their sizes are equal and all pairs of corresponding elements are equal.

Parameters:
obj - Specifies the object to be compared with the current vector.
Returns:
true if the vectors are equal; false otherwise.
Overrides:
equals in class Object
 o equals
  public synchronized boolean equals(SortableVector vec)
Determines whether this vector is equal to the specified sortable vector.

Notes: The vectors are equal if their sizes are equal and all pairs of corresponding elements are equal.

Parameters:
vec - Specified the sortable vector to be compared with the current vector.
Returns:
true if the vectors are equal; false otherwise.
 o toString
  public synchronized String toString()
Generates a string representation of the contents of the vector.

Notes: The string representation is generated by appending the string representation of each element. Each element is separated by a comma and the entire string is enclose in square brackets '[]'.

Returns:
A string representation of the vector (see Notes).
Overrides:
toString in class Object
 o getArray
  public synchronized Object[] getArray()
Gets a shallow copy of the internal array
Returns:
The copied array if the vector is non-empty, or null if the vector is empty.
 o setArray
  public synchronized void setArray(Object array[])
Makes a shallow copy of the given array and stores it in the vector. The elements originally in the vector are cleared first.
Parameters:
array - The array to be stored; null to set the vector empty.
 o getCapacity
  public int getCapacity()
Determines the capacity of the vector.
Returns:
The number of objects that can be stored in the vector.
 o getCompareRoutine
  public CompareRoutine getCompareRoutine()
Gets the comparison routine.
Returns:
The comparison routine.
 o setCompareRoutine
  public synchronized void setCompareRoutine(CompareRoutine routine)
Specifies the comparison routine.
Parameters:
routine - The comparison routine.
 o getCount
  public int getCount()
Determines the number of objects currently stored in the vector.
Returns:
The number objects in the vector.
See Also:
getCapacity
 o getElement
  public synchronized Object getElement(int index)
Retrieves the object stored at the specified index.
Parameters:
index - The index of the element to be retrieved.
Returns:
The object contained in the specified element.
Throws: ArrayIndexOutOfBoundsException
if index is out of range.
 o setElement
  public synchronized void setElement(int index,
                                      Object obj)
Stores an object in the specified element.
Parameters:
index - The index of the element in which to store the object.
obj - The object to store in the specified element.
Throws: ArrayIndexOutOfBoundsException
if index is out of range.
 o getElements
  public Enumeration getElements()
Gets an enumeration of the elements of the vector.
 o equals
  protected boolean equals(Object obj1,
                           Object obj2)
 o removeAtIndex
  protected Object removeAtIndex(int index)
 o getLowerBound
  protected int getLowerBound(Object object)
 o partition
  protected int partition(int first,
                          int last,
                          Object pivot)
 o getMedian
  protected Object getMedian(Object a,
                             Object b,
                             Object c)
 o sortTwo
  protected void sortTwo(int first)
 o quickSort
  protected void quickSort(int first,
                           int last)

All Packages  Class Hierarchy  This Package  Previous  Next  Index