org.multiverse.transactional.arrays
Class TransactionalReferenceArray<E>

java.lang.Object
  extended by org.multiverse.transactional.arrays.TransactionalReferenceArray<E>
Type Parameters:
E -

public final class TransactionalReferenceArray<E>
extends Object

An Transactional array. The elements in the array are of type Ref. The elements are created eagerly. In the future some kind of lazy creation mechanism will be added as well, but for the time being this is the simplest solution.

Just as with normal arrays, the length of the TransactionalReferenceArray can't change after is is created. See the TransactionalArrayList for an alternative.

Author:
Peter Veentjer.

Constructor Summary
TransactionalReferenceArray(int length)
          Creates a new TransactionalReferenceArray with the specified length.
 
Method Summary
 boolean atomicCompareAndSet(int index, E expected, E update)
          Executes an atomic compare and set, so this call doesn't look at a running transaction, but essentially runs with its own transaction.
 E atomicGet(int index)
          Returns the currently stored item at the specified index.
 E atomicSet(int index, E update)
          Atomically sets the element at the specified index.
 TransactionalReferenceArray<E> copyToBiggerArray(int newLength)
          Copies the content of this TransactionalReferenceArray to a bigger TransactionalReferenceArray.
 E get(int index)
          Gets the element at the specified index.
 int length()
          Returns the length of this TransactionalReferenceArray .
 E set(int index, E item)
          Sets the element at the specified index.
 void shiftLeft(int firstIndex, int lastIndex)
          Shifts all the items in the array one to the left (so essentially removes items).
 void shiftRight(int firstIndex, int lastIndex)
          Create room
 Object[] toArray(int size)
          Sends back an array containing the value of the references in this TransactionalReferenceArray.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TransactionalReferenceArray

public TransactionalReferenceArray(int length)
Creates a new TransactionalReferenceArray with the specified length.

Parameters:
length - the length of the TransactionalReferenceArray.
Throws:
IllegalArgumentException - if length smaller than 0.
Method Detail

get

public E get(int index)
Gets the element at the specified index.

Parameters:
index - the index of the element to get.
Returns:
the element at the specified index.
Throws:
IndexOutOfBoundsException - if the index is out of bounds.

atomicGet

public E atomicGet(int index)
Returns the currently stored item at the specified index. This call doesn't look at a transaction running in the ThreadLocalTransaction.

Parameters:
index - the index of the item to get
Returns:
the value of the item
Throws:
IndexOutOfBoundsException - if index is out of bounds.

set

public E set(int index,
             E item)
Sets the element at the specified index.

If a transaction currently is running in the ThreadLocalTransaction, that transaction is used, otherwise this call is executed atomically (and very very fast).

Parameters:
index - the index of the element to set.
item - the item to set.
Returns:
the previous content of element at the specified index.
Throws:
IndexOutOfBoundsException - if the index is out of bounds.

atomicSet

public E atomicSet(int index,
                   E update)
Atomically sets the element at the specified index. If a transaction is running in the ThreadLocalTransaction, it is ignored. That is why this call is very fast.

Parameters:
index - the index of the element to atomically set.
update - the new value
Returns:
the previous stored value.
Throws:
IndexOutOfBoundsException - if the index is out of bounds

atomicCompareAndSet

public boolean atomicCompareAndSet(int index,
                                   E expected,
                                   E update)
Executes an atomic compare and set, so this call doesn't look at a running transaction, but essentially runs with its own transaction.

Parameters:
index - the index of the reference where the compare and set action needs to be executed on.
expected - the value the reference is expected to have.
update - the new value
Returns:
true if it was a success, false otherwise.
Throws:
IndexOutOfBoundsException - if index is smaller than 0, or larger than array.size.

length

public int length()
Returns the length of this TransactionalReferenceArray .

Returns:
the length of this TransactionalReferenceArray .

shiftLeft

public void shiftLeft(int firstIndex,
                      int lastIndex)
Shifts all the items in the array one to the left (so essentially removes items).

Parameters:
firstIndex - the first index of the item to shift to the left.
lastIndex - the index of the last item (inclusive) to shift to the left.
Throws:
IndexOutOfBoundsException - if firstIndex smaller than 1, or firstIndex larger or equal to the array.length.

shiftRight

public void shiftRight(int firstIndex,
                       int lastIndex)
Create room

Parameters:
firstIndex -
lastIndex -

copyToBiggerArray

public TransactionalReferenceArray<E> copyToBiggerArray(int newLength)
Copies the content of this TransactionalReferenceArray to a bigger TransactionalReferenceArray.

Parameters:
newLength - the new capacity of the bigger TransactionalReferenceArray
Returns:
the new TransactionalReferenceArray
Throws:
IllegalArgumentException - if newCapacity is smaller than the current capacity.

toString

public String toString()
Overrides:
toString in class Object

toArray

public Object[] toArray(int size)
Sends back an array containing the value of the references in this TransactionalReferenceArray.

Parameters:
size - the number of items to copy. If the size is smaller than the array length, it will only copy till that length.
Returns:
the copied array.
Throws:
IllegalArgumentException - if size smaller than 0 or larger than the length


Copyright © 2008-2010 Multiverse. All Rights Reserved.