net.sf.sido.array
Class Array<T>

java.lang.Object
  extended by net.sf.sido.common.AnnotatedSupport
      extended by net.sf.sido.array.Array<T>
Type Parameters:
T - Type of element in the array
All Implemented Interfaces:
Serializable, Iterable<T>, Annotated
Direct Known Subclasses:
IndexedArray

public class Array<T>
extends AnnotatedSupport
implements Iterable<T>, Serializable

The Array class represents an immutable and ordered collection. The only ways to update such an Array is to create a new one, using operations like add(Array), delete(int...), etc. that return other instances of arrays.

When an Array is created, it is associated with an ArrayEvent that defines the list of events that have occured on a source to create this new array.

Author:
Damien Coraboeuf
See Also:
Serialized Form

Constructor Summary
  Array(Collection<T> elements)
          Constructor from a list or collection
protected Array(Collection<T> elements, ArrayEvent<T> event)
          Protected constructor for event
  Array(T... elements)
          Constructor from an array of elements
 
Method Summary
 Array<T> add(Array<T> elements)
          Appends elements
 Array<T> add(Collection<T> elements)
          Appends elements
 Array<T> add(T... elements)
          Appends elements
protected  Array<T> createNewArray(List<T> newList, ArrayEvent<T> event)
          Creates a new instance of an array
 Array<T> delete(int... indexes)
          Deletes one or several elements using their indexes.
static
<V> Array<V>
empty()
          Creates an empty array.
 boolean equals(Object o)
           
 Array<T> filter(com.google.common.base.Predicate<T> predicate)
          Filters the array using a predicate
 T get(int index)
          Gets an element using its index
 ArrayEvent<T> getArrayEvent()
          Gets the ArrayEvent associated with this Array.
 T getAt(Integer index)
          Same method than get(int) but is useable for script engines like Groovy.
 int getLength()
          Length of this array
 T getOrNull(int index)
          Gets an element using its index and returns null if the index is not available.
 int hashCode()
           
<K> Map<K,T>
index(com.google.common.base.Function<T,K> indexer)
          Returns an immutable index of the array using an indexing function.
 Array<T> insert(int position, Collection<T> elements)
          Appends elements at a given position
 Array<T> insert(int position, T... items)
          Appends elements at a given position
 boolean isEmpty()
          Checks if this array is empty
 Iterator<T> iterator()
           
 int length()
          Length (short version) - synonym for getLength()
 Array<T> reverse()
          Creates a reversed version of this array
 int size()
          Length of this array.
 Array<T> sub(int startIndex, int endIndex)
          Sub array using a range between two indexes.
 List<T> toList()
          Returns a list that contains all the elements of this array and in the same order.
 String toString()
          String representation.
<V> Array<V>
transform(com.google.common.base.Function<? super T,V> transform)
          Transforms an array in another array.
 Array<T> update(int index, T newElement)
          Updates an element with a new value
 
Methods inherited from class net.sf.sido.common.AnnotatedSupport
getAnnotation, putAnnotation
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Array

public Array(Collection<T> elements)
Constructor from a list or collection

Parameters:
elements - List of elements to build the array from. The final array is disconnected from the initial list: modifying the list won't have any impact on the array.

Array

protected Array(Collection<T> elements,
                ArrayEvent<T> event)
Protected constructor for event


Array

public Array(T... elements)
Constructor from an array of elements

Parameters:
elements - Array of elements to build this Array from.
Method Detail

empty

public static <V> Array<V> empty()
Creates an empty array.

Type Parameters:
V - Type of element
Returns:
Empty and invariant array

add

public Array<T> add(Array<T> elements)
Appends elements

Parameters:
elements - Array of elements to add
Returns:
A new Array, that contains this array's elements, plus the ones in elements

add

public Array<T> add(Collection<T> elements)
Appends elements

Parameters:
elements - Collection of elements to add
Returns:
A new Array, that contains this array's elements, plus the ones in elements

add

public Array<T> add(T... elements)
Appends elements

Parameters:
elements - Array of elements to add
Returns:
A new Array, that contains this array's elements, plus the ones in elements

delete

public Array<T> delete(int... indexes)
Deletes one or several elements using their indexes. Note that the indexes array does not need to be sorted.

Parameters:
indexes - List of indexes to remove from the array.
Returns:
A new Array the elements have been removed from. This new array will be associated with an ArrayEvent that describes the deletion.

equals

public boolean equals(Object o)
Overrides:
equals in class Object

filter

public Array<T> filter(com.google.common.base.Predicate<T> predicate)
Filters the array using a predicate

Parameters:
predicate - Function that returns true for the elements that must be kept
Returns:
A new Array the elements have been removed from. This new array will be associated with an ArrayEvent that describes the deletion.

get

public T get(int index)
Gets an element using its index

Parameters:
index - Index of the element to get
Returns:
Element at this index
Throws:
IndexOutOfBoundsException - If the index is not valid
See Also:
List.get(int)

getArrayEvent

public ArrayEvent<T> getArrayEvent()
Gets the ArrayEvent associated with this Array.

Returns:
ArrayEvent that describes the changes that have created this Array

getAt

public T getAt(Integer index)
Same method than get(int) but is useable for script engines like Groovy.

Parameters:
index - Index of the element to get
Returns:
Element at this index
Throws:
IllegalArgumentException - If index is null
See Also:
get(int)

getLength

public int getLength()
Length of this array

Returns:
Length
See Also:
List.size(), length()

size

public int size()
Length of this array.

Returns:
Length
See Also:
length()

getOrNull

public T getOrNull(int index)
Gets an element using its index and returns null if the index is not available. It still returns an IndexOutOfBoundsException if the index is less than 0.

Parameters:
index - Index of the element
Returns:
An element or null

hashCode

public int hashCode()
Overrides:
hashCode in class Object

index

public <K> Map<K,T> index(com.google.common.base.Function<T,K> indexer)
Returns an immutable index of the array using an indexing function.

Type Parameters:
K - Type for the index key
Parameters:
indexer - Function that extracts the index key from the elements in the array
Returns:
Immutable map that indexes the elements of the array

insert

public Array<T> insert(int position,
                       Collection<T> elements)
Appends elements at a given position

Parameters:
position - Position in the array to insert the new elements at
elements - List of elements to insert at this position
Returns:
A new Array where the elements have been inserted into. This new array will be associated with an ArrayEvent that describes the insertion.

createNewArray

protected Array<T> createNewArray(List<T> newList,
                                  ArrayEvent<T> event)
Creates a new instance of an array

Parameters:
newList - List of elements
event - Associated array of events
Returns:
New instance

insert

public Array<T> insert(int position,
                       T... items)
Appends elements at a given position

Parameters:
position - Position in the array to insert the new elements at
items - List of elements to insert at this position
Returns:
A new Array where the elements have been inserted into. This new array will be associated with an ArrayEvent that describes the insertion.

isEmpty

public boolean isEmpty()
Checks if this array is empty

Returns:
true is the array is empty, false otherwise
See Also:
List.isEmpty()

iterator

public Iterator<T> iterator()
Specified by:
iterator in interface Iterable<T>

length

public int length()
Length (short version) - synonym for getLength()

Returns:
Size of the array
See Also:
getLength()

sub

public Array<T> sub(int startIndex,
                    int endIndex)
Sub array using a range between two indexes. If the indexes are outside of the array boundaries, they adjusted to fit in.

Parameters:
startIndex - Start index
endIndex - End index
Returns:
A new Array instance that comprises only the elements between startIndex and endIndex. An ArrayEvent is associated with this array, which describes the operation.
See Also:
List.subList(int, int)

toList

public List<T> toList()
Returns a list that contains all the elements of this array and in the same order.

Returns:
Immutable list

toString

public String toString()
String representation.

Overrides:
toString in class Object
See Also:
List#toString()

transform

public <V> Array<V> transform(com.google.common.base.Function<? super T,V> transform)
Transforms an array in another array.

Type Parameters:
V - Type of the target element
Parameters:
transform - Function that transforms each array element
Returns:
Array instance that contains a transformed version of each element of this array

update

public Array<T> update(int index,
                       T newElement)
Updates an element with a new value

Parameters:
index - Index to update the element at
newElement - Element to replace the initial element with
Returns:
A new Array where the elements have been updated. This new array will be associated with an ArrayEvent that describes the update.

reverse

public Array<T> reverse()
Creates a reversed version of this array

Returns:
Reversed array
See Also:
Collections.reverse(java.util.List)


Copyright © 2011. All Rights Reserved.