LBJ2.util
Class OVector

java.lang.Object
  extended by LBJ2.util.OVector
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class OVector
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

This class implements an expandable array of objects that should be faster than java's Vector.

See Also:
Serialized Form

Field Summary
protected static int defaultCapacity
          The default capacity of a vector upon first construction.
protected  int size
          The number of elements in the vector.
protected  java.lang.Object[] vector
          The elements of the vector.
 
Constructor Summary
OVector()
          Constructs a new vector with capacity equal to defaultCapacity.
OVector(int c)
          Constructs a new vector with the specified capacity.
OVector(java.lang.Object[] v)
          Constructs a new vector using the specified array as a starting point.
OVector(OVector v)
          Constructs a copy of a vector starting with capacity equal to that vector's size.
 
Method Summary
 void add(java.lang.Object v)
          Adds the specified value on to the end of the vector, expanding its capacity as necessary.
 void addAll(OVector v)
          Adds all the values in the given vector to the end of this vector, expanding its capacity as necessary.
protected  void boundsCheck(int i)
          Throws an exception when the specified index is negative.
 java.lang.Object clone()
          Returns a shallow clone of this vector; the vector itself is cloned, but the element objects aren't.
 boolean equals(java.lang.Object o)
          Two OVectors are considered equal if they contain equivalent elements and have the same size.
protected  void expandFor(int index, java.lang.Object d)
          Makes sure the capacity and size of the vector can accomodate the given index.
 java.lang.Object get(int i)
          Retrieves the value stored at the specified index of the vector, or null if the vector isn't long enough.
 java.lang.Object get(int i, java.lang.Object d)
          Retrieves the value stored at the specified index of the vector or d if the vector isn't long enough.
 int hashCode()
          A hash code based on the hash code of vector.
 java.lang.Object remove(int i)
          Removes the element at the specified index of the vector.
 java.lang.Object set(int i, java.lang.Object v)
          Sets the value at the specified index to the given value.
 java.lang.Object set(int i, java.lang.Object v, java.lang.Object d)
          Sets the value at the specified index to the given value.
 int size()
          Returns the value of size.
 void sort(java.util.Comparator c)
          Sorts this vector in increasing order according to the given comparator.
 java.lang.Object[] toArray()
          Returns a new array of objectss containing the same data as this vector.
 java.lang.String toString()
          Returns a text representation of this vector.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

defaultCapacity

protected static final int defaultCapacity
The default capacity of a vector upon first construction.

See Also:
Constant Field Values

vector

protected java.lang.Object[] vector
The elements of the vector.


size

protected int size
The number of elements in the vector.

Constructor Detail

OVector

public OVector()
Constructs a new vector with capacity equal to defaultCapacity.


OVector

public OVector(int c)
Constructs a new vector with the specified capacity.

Parameters:
c - The initial capacity for the new vector.

OVector

public OVector(java.lang.Object[] v)
Constructs a new vector using the specified array as a starting point.

Parameters:
v - The initial array.

OVector

public OVector(OVector v)
Constructs a copy of a vector starting with capacity equal to that vector's size.

Parameters:
v - The vector to copy.
Method Detail

boundsCheck

protected void boundsCheck(int i)
Throws an exception when the specified index is negative.

Parameters:
i - The index.
Throws:
java.lang.ArrayIndexOutOfBoundsException - When i < 0.

get

public java.lang.Object get(int i)
Retrieves the value stored at the specified index of the vector, or null if the vector isn't long enough.

Parameters:
i - The index of the value to retrieve.
Returns:
The retrieved value.
Throws:
java.lang.ArrayIndexOutOfBoundsException - When i < 0.

get

public java.lang.Object get(int i,
                            java.lang.Object d)
Retrieves the value stored at the specified index of the vector or d if the vector isn't long enough.

Parameters:
i - The index of the value to retrieve.
d - The default value.
Returns:
The retrieved value.
Throws:
java.lang.ArrayIndexOutOfBoundsException - When i < 0.

set

public java.lang.Object set(int i,
                            java.lang.Object v)
Sets the value at the specified index to the given value.

Parameters:
i - The index of the value to set.
v - The new value at that index.
Returns:
The value that used to be at index i.
Throws:
java.lang.ArrayIndexOutOfBoundsException - When i < 0.

set

public java.lang.Object set(int i,
                            java.lang.Object v,
                            java.lang.Object d)
Sets the value at the specified index to the given value. If the given index is greater than the vector's current size, the vector will expand to accomodate it.

Parameters:
i - The index of the value to set.
v - The new value at that index.
d - The default value for other new indexes that might get created.
Returns:
The value that used to be at index i.
Throws:
java.lang.ArrayIndexOutOfBoundsException - When i < 0.

add

public void add(java.lang.Object v)
Adds the specified value on to the end of the vector, expanding its capacity as necessary.

Parameters:
v - The new value to appear last in the vector.

addAll

public void addAll(OVector v)
Adds all the values in the given vector to the end of this vector, expanding its capacity as necessary.

Parameters:
v - The new vector of values to appear at the end of this vector.

remove

public java.lang.Object remove(int i)
Removes the element at the specified index of the vector.

Parameters:
i - The index of the element to remove.
Returns:
The removed element.

size

public int size()
Returns the value of size.


sort

public void sort(java.util.Comparator c)
Sorts this vector in increasing order according to the given comparator.

Parameters:
c - A comparator for the elements of this vector.

expandFor

protected void expandFor(int index,
                         java.lang.Object d)
Makes sure the capacity and size of the vector can accomodate the given index. The capacity of the vector is simply doubled until it can accomodate its size.

Parameters:
index - The index where a new value will be stored.
d - The default value for other new indexes that might get created.

toArray

public java.lang.Object[] toArray()
Returns a new array of objectss containing the same data as this vector.


equals

public boolean equals(java.lang.Object o)
Two OVectors are considered equal if they contain equivalent elements and have the same size.

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
A hash code based on the hash code of vector.

Overrides:
hashCode in class java.lang.Object

clone

public java.lang.Object clone()
Returns a shallow clone of this vector; the vector itself is cloned, but the element objects aren't.

Overrides:
clone in class java.lang.Object

toString

public java.lang.String toString()
Returns a text representation of this vector.

Overrides:
toString in class java.lang.Object