org.magnos.data
Class DataArray<T extends Data>

java.lang.Object
  extended by org.magnos.data.AbstractData
      extended by org.magnos.data.DataArray<T>
Type Parameters:
T - The type of element stored in the array.
All Implemented Interfaces:
Data

public class DataArray<T extends Data>
extends AbstractData

An array of data elements which are the same type and size. An array can be either lazy or not. A lazy array does not allocate an array to store the data elements, but instead creates a new element for each get and doesn't cache the element in the set. An array that is not lazy will have an internal cache of items

Author:
Philip Diffenderfer

Constructor Summary
DataArray(T root, int count, boolean lazy)
          Instantiates a new DataArray.
 
Method Summary
 Data copy()
          Creates a clone of this data.
static
<E extends Data>
DataArray<E>
create(java.lang.Class<E> type, int count, boolean lazy)
          Creates a new DataArray given an element type to which elements in the created array should be instantiated form.
static
<E extends Data>
DataArray<E>
create(E root, int count, boolean lazy)
          Creates a new DataArray given a root element to which elements in the created array should be copied from.
 T get(int index)
          Gets the data at the given index.
 int getLength()
          Returns the number of elements in the array.
protected  void onRead(int location, Store store)
          Reads this data from the given store at the given location.
protected  void onWrite(int location, Store store)
          Writes this data to the given store at the given location.
 void set(int index, T data)
          Sets the data at the given index.
 T update(int index)
          Updates the data element at the given index and returns it.
 
Methods inherited from class org.magnos.data.AbstractData
getActualLocation, getLocation, getParent, getSize, getStore, read, read, read, read, setLocation, setParent, setStore, write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataArray

public DataArray(T root,
                 int count,
                 boolean lazy)
Instantiates a new DataArray.

Parameters:
root - The data to use to create other data objects of similar type and having the same characteristics.
count - The number of data elements to store in the array.
lazy - Whether or not this array is lazy. A lazy array will read data everytime a get is invoked, and will write data everytime a set is invoked. A non-lazy array will only call write and read to each data item when write or read is explicitly called on the array, which also requires the data items to be stored in memory at all times. To save space use a lazy array (true), for a slightly more efficient array choose a non-lazy array (false).
Method Detail

get

public T get(int index)
Gets the data at the given index. If the index is outside the bounds of the array an IndexOutOfBoundsException is thrown.

Parameters:
index - The index of the data in the array.
Returns:
The element at the given index. This element may be null if this array is not lazy and it has not been read from a store yet.

update

public T update(int index)
Updates the data element at the given index and returns it. A data element which is updated is one that was recently read from the store. If the index is outside the bounds of the array an IndexOutOfBoundsException is thrown.

Parameters:
index - The index of the data in the array.
Returns:
The updated element. This will return a non-null element.

set

public void set(int index,
                T data)
Sets the data at the given index. If the index is outside the bounds of the array an IndexOutOfBoundsException is thrown. If data is null the element at the given index will be set to null in the internal array if this array is not null.

Parameters:
index - The index of the data in the array.
data - The data to set.

getLength

public int getLength()
Returns the number of elements in the array.

Returns:
The number of elements in the array.

onRead

protected void onRead(int location,
                      Store store)
Reads this data from the given store at the given location.

Specified by:
onRead in class AbstractData
Parameters:
location - The offset in bytes from the beginning of the store to read from.
store - The store to read from.

onWrite

protected void onWrite(int location,
                       Store store)
Writes this data to the given store at the given location.

Specified by:
onWrite in class AbstractData
Parameters:
location - The offset in bytes from the beginning of the store to write to.
store - The store to write to.

copy

public Data copy()
Creates a clone of this data. The resulting data will be of the same type, have the same location, the same store, and have other similar attributes to this type.

Returns:
A clone of this data.

create

public static <E extends Data> DataArray<E> create(E root,
                                                   int count,
                                                   boolean lazy)
Creates a new DataArray given a root element to which elements in the created array should be copied from.

Type Parameters:
E - The type of element in the array.
Parameters:
root - The element to use for copying.
count - The number of data elements in the array.
lazy - Whether or not this array is lazy. A lazy array will read data everytime a get is invoked, and will write data everytime a set is invoked. A non-lazy array will only call write and read to each data item when write or read is explicitly called on the array, which also requires the data items to be stored in memory at all times. To save space use a lazy array (true), for a slightly more efficient array choose a non-lazy array (false).
Returns:
The reference to the newly instantiated DataArray.

create

public static <E extends Data> DataArray<E> create(java.lang.Class<E> type,
                                                   int count,
                                                   boolean lazy)
Creates a new DataArray given an element type to which elements in the created array should be instantiated form. If the given type does not have a default constructor (one without arguments) a RuntimeException will be thrown.

Type Parameters:
E - The type of element in the array.
Parameters:
type - The class of the data element.
count - The number of data elements in the array.
lazy - Whether or not this array is lazy. A lazy array will read data everytime a get is invoked, and will write data everytime a set is invoked. A non-lazy array will only call write and read to each data item when write or read is explicitly called on the array, which also requires the data items to be stored in memory at all times. To save space use a lazy array (true), for a slightly more efficient array choose a non-lazy array (false).
Returns:
The reference to the newly instantiated DataArray.