Genuts API

com.genuts.gameui.util
Class TickableVector

java.lang.Object
  extended bycom.genuts.gameui.util.TickableVector

public class TickableVector
extends java.lang.Object

The TickableVector class implements a growable array of Tickable objects. Like an array or the java.util.Vector, it contains components that can be accessed using an integer index. However, the size of a TickableVector can grow or shrink as needed to accommodate adding and removing tickables after the TickableVector has been created.

The TickableVector works as java.util.Vector whitout any synchronization or iterator facilities. The number of method is reduced.

This object is made to reduce garbage collector actions, and to optimize the storage of Tickable objects.

See Also:
Vector

Field Summary
protected  int capacityIncrement
          The amount by which the capacity of the vector is automatically incremented when its size becomes greater than its capacity.
protected  int elementCount
          The number of valid tickables in the vector.
protected  Tickable[] elementData
          The array buffer into which the tickables of the vector are stored.
 
Constructor Summary
TickableVector()
          Constructs an empty vector.
TickableVector(int initialCapacity)
          Constructs an empty vector with the specified initial capacity.
TickableVector(int initialCapacity, int capacityIncrement)
          Constructs an empty vector with the specified initial capacity and capacity increment.
 
Method Summary
 void addElement(Tickable tickable)
          Adds the specified tickable to the end of this vector, increasing its size by one.
 int capacity()
          Returns the current capacity of this vector.
 boolean contains(Tickable elem)
          Tests if the specified tickable is a component in this vector.
 void copyInto(Tickable[] anArray)
          Copies the tickables of this vector into the specified array.
 Tickable elementAt(int index)
          Returns the tickable at the specified index.
 void ensureCapacity(int minCapacity)
          Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of tickables specified by the minimum capacity argument.
 Tickable firstElement()
          Returns the first tickable of this vector.
 int indexOf(Tickable elem)
          Searches for the first occurence of the given argument, testing for equality using the == operator.
 int indexOf(Tickable elem, int index)
          Searches for the first occurence of the given argument, beginning the search at index, and testing for equality using the == operator.
 void insertElementAt(Tickable tickable, int index)
          Inserts the specified tickable as a component in this vector at the specified index.
 boolean isEmpty()
          Tests if this vector has no tickables.
 Tickable lastElement()
          Returns the last tickable of the vector.
 int lastIndexOf(Tickable elem)
          Returns the index of the last occurrence of the specified tickable in this vector.
 int lastIndexOf(Tickable elem, int index)
          Searches backwards for the specified tickable, starting from the specified index, and returns an index to it.
 void removeAllElements()
          Removes all components from this vector and sets its size to zero.
 boolean removeElement(Tickable tickable)
          Removes the first occurrence of the argument from this vector.
 void removeElementAt(int index)
          Deletes the tickable at the specified index.
 void setElementAt(Tickable tickable, int index)
          Sets the component at the specified index of this vector to be the specified tickable.
 void setSize(int newSize)
          Sets the size of this vector.
 int size()
          Returns the number of tickables in this vector.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

elementData

protected Tickable[] elementData
The array buffer into which the tickables of the vector are stored. The capacity of the vector is the length of this array buffer.


elementCount

protected int elementCount
The number of valid tickables in the vector.


capacityIncrement

protected int capacityIncrement
The amount by which the capacity of the vector is automatically incremented when its size becomes greater than its capacity. If the capacity increment is 0, the capacity of the vector is doubled each time it needs to grow.

Constructor Detail

TickableVector

public TickableVector(int initialCapacity,
                      int capacityIncrement)
Constructs an empty vector with the specified initial capacity and capacity increment.

Parameters:
initialCapacity - the initial capacity of the vector.
capacityIncrement - the amount by which the capacity is increased when the vector overflows.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is less than or equal to zero or the increment capacity is less than zero.

TickableVector

public TickableVector(int initialCapacity)
Constructs an empty vector with the specified initial capacity.

Parameters:
initialCapacity - the initial capacity of the vector.

TickableVector

public TickableVector()
Constructs an empty vector.

Method Detail

copyInto

public final void copyInto(Tickable[] anArray)
Copies the tickables of this vector into the specified array. The size of the array can be lower of the number of tickables, in this case, tickables with an index higher than the size of the array are ignored.

Parameters:
anArray - the array into which the tickables get copied.

ensureCapacity

public final void ensureCapacity(int minCapacity)
Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of tickables specified by the minimum capacity argument.

Parameters:
minCapacity - the desired minimum capacity.

setSize

public final void setSize(int newSize)
Sets the size of this vector. If the new size is greater than the current size, new null items are added to the end of the vector. If the new size is less than the current size, all components at index newSize and greater are discarded.

Parameters:
newSize - the new size of this vector.

capacity

public final int capacity()
Returns the current capacity of this vector.

Returns:
the current capacity of this vector.

size

public final int size()
Returns the number of tickables in this vector.

Returns:
the number of tickables in this vector.

isEmpty

public final boolean isEmpty()
Tests if this vector has no tickables.

Returns:
true if this vector has no tickables; false otherwise.

contains

public final boolean contains(Tickable elem)
Tests if the specified tickable is a component in this vector.

Parameters:
elem - a tickable.
Returns:
true if the specified tickable is a component in this vector; false otherwise.

indexOf

public final int indexOf(Tickable elem)
Searches for the first occurence of the given argument, testing for equality using the == operator.

Parameters:
elem - a tickable
Returns:
the index of the first occurrence of the argument in this vector; returns -1 if the tickable is not found.

indexOf

public final int indexOf(Tickable elem,
                         int index)
Searches for the first occurence of the given argument, beginning the search at index, and testing for equality using the == operator.

Parameters:
elem - a tickable.
index - the index to start searching from.
Returns:
the index of the first occurrence of the tickable argument in this vector at position index or later in the vector; returns -1 if the tickable is not found.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if an invalid index was given.

lastIndexOf

public final int lastIndexOf(Tickable elem)
Returns the index of the last occurrence of the specified tickable in this vector.

Parameters:
elem - the desired tickable.
Returns:
the index of the last occurrence of the specified tickable in this vector; returns -1 if the tickable is not found.

lastIndexOf

public final int lastIndexOf(Tickable elem,
                             int index)
Searches backwards for the specified tickable, starting from the specified index, and returns an index to it.

Parameters:
elem - the desired tickable.
index - the index to start searching from.
Returns:
the index of the last occurrence of the specified tickable in this vector at position less than index in the vector; -1 if the tickable is not found.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if an invalid index was given.

elementAt

public final Tickable elementAt(int index)
Returns the tickable at the specified index.

Parameters:
index - an index into this vector.
Returns:
the tickable at the specified index.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if an invalid index was given.

firstElement

public final Tickable firstElement()
Returns the first tickable of this vector.

Returns:
the first tickable of this vector, or null if there is no element in this vector.

lastElement

public final Tickable lastElement()
Returns the last tickable of the vector.

Returns:
the last tickable of the vector, i.e., the tickable at index size() - 1, or null if there is no element in this vector.

setElementAt

public final void setElementAt(Tickable tickable,
                               int index)
Sets the component at the specified index of this vector to be the specified tickable. The previous tickable at that position is discarded.
The index must be a value greater than or equal to 0 and less than the current size of the vector.

Parameters:
tickable - what the tickable is to be set to.
index - the specified index.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index was invalid.
See Also:
size()

removeElementAt

public final void removeElementAt(int index)
Deletes the tickable at the specified index. Each tickable in this vector with an index greater or equal to the specified index is shifted downward to have an index one smaller than the value it had previously.
The index must be a value greater than or equal to 0 and less than the current size of the vector.

Parameters:
index - the index of the tickable to remove.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index was invalid.
See Also:
size()

insertElementAt

public final void insertElementAt(Tickable tickable,
                                  int index)
Inserts the specified tickable as a component in this vector at the specified index. Each component in this vector with an index greater or equal to the specified index is shifted upward to have an index one greater than the value it had previously.
The index must be a value greater than or equal to 0 and less than or equal to the current size of the vector.

Parameters:
tickable - the component to insert.
index - where to insert the new component.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index was invalid.
See Also:
size()

addElement

public final void addElement(Tickable tickable)
Adds the specified tickable to the end of this vector, increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity.

Parameters:
tickable - the component to be added.

removeElement

public final boolean removeElement(Tickable tickable)
Removes the first occurrence of the argument from this vector. If the tickable is found in this vector, each component in the vector with an index greater or equal to the tickable's index is shifted downward to have an index one smaller than the value it had previously.

Parameters:
tickable - component to be removed.
Returns:
true if the argument was a component of this vector; false otherwise.

removeAllElements

public final void removeAllElements()
Removes all components from this vector and sets its size to zero.


Genuts API

Genuts API