Genuts API

com.genuts.gameui.util
Class SpriteVector

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

public class SpriteVector
extends java.lang.Object

The SpriteVector class implements a growable array of Sprite 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 SpriteVector can grow or shrink as needed to accommodate adding and removing sprites after the SpriteVector has been created.

The SpriteVector 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 Sprite objects.

It is really useful to store unused sprites like bullet instead of new instanciations.

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 sprites in the vector.
protected  Sprite[] elementData
          The array buffer into which the sprites of the vector are stored.
 
Constructor Summary
SpriteVector()
          Constructs an empty vector.
SpriteVector(int initialCapacity)
          Constructs an empty vector with the specified initial capacity.
SpriteVector(int initialCapacity, int capacityIncrement)
          Constructs an empty vector with the specified initial capacity and capacity increment.
 
Method Summary
 void addElement(Sprite sprite)
          Adds the specified sprite to the end of this vector, increasing its size by one.
 int capacity()
          Returns the current capacity of this vector.
 boolean contains(Sprite elem)
          Tests if the specified sprite is a component in this vector.
 void copyInto(Sprite[] anArray)
          Copies the sprites of this vector into the specified array.
 Sprite elementAt(int index)
          Returns the sprite 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 sprites specified by the minimum capacity argument.
 Sprite firstElement()
          Returns the first sprite of this vector.
 int indexOf(Sprite elem)
          Searches for the first occurence of the given argument, testing for equality using the == operator.
 int indexOf(Sprite 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(Sprite sprite, int index)
          Inserts the specified sprite as a component in this vector at the specified index.
 boolean isEmpty()
          Tests if this vector has no sprites.
 Sprite lastElement()
          Returns the last sprite of the vector.
 int lastIndexOf(Sprite elem)
          Returns the index of the last occurrence of the specified sprite in this vector.
 int lastIndexOf(Sprite elem, int index)
          Searches backwards for the specified sprite, 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(Sprite sprite)
          Removes the first occurrence of the argument from this vector.
 void removeElementAt(int index)
          Deletes the sprite at the specified index.
 void setElementAt(Sprite sprite, int index)
          Sets the component at the specified index of this vector to be the specified sprite.
 void setSize(int newSize)
          Sets the size of this vector.
 int size()
          Returns the number of sprites 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 Sprite[] elementData
The array buffer into which the sprites 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 sprites 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

SpriteVector

public SpriteVector(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.

SpriteVector

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

Parameters:
initialCapacity - the initial capacity of the vector.

SpriteVector

public SpriteVector()
Constructs an empty vector.

Method Detail

copyInto

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

Parameters:
anArray - the array into which the sprites 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 sprites 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 sprites in this vector.

Returns:
the number of sprites in this vector.

isEmpty

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

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

contains

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

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

indexOf

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

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

indexOf

public final int indexOf(Sprite 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 sprite.
index - the index to start searching from.
Returns:
the index of the first occurrence of the sprite argument in this vector at position index or later in the vector; returns -1 if the sprite is not found.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if an invalid index was given.

lastIndexOf

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

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

lastIndexOf

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

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

elementAt

public final Sprite elementAt(int index)
Returns the sprite at the specified index.

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

firstElement

public final Sprite firstElement()
Returns the first sprite of this vector.

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

lastElement

public final Sprite lastElement()
Returns the last sprite of the vector.

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

setElementAt

public final void setElementAt(Sprite sprite,
                               int index)
Sets the component at the specified index of this vector to be the specified sprite. The previous sprite 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:
sprite - what the sprite 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 sprite at the specified index. Each sprite 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 sprite to remove.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index was invalid.
See Also:
size()

insertElementAt

public final void insertElementAt(Sprite sprite,
                                  int index)
Inserts the specified sprite 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:
sprite - 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(Sprite sprite)
Adds the specified sprite 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:
sprite - the component to be added.

removeElement

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

Parameters:
sprite - 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