net.sf.sido.array
Class IndexedArray<T,K>

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

public final class IndexedArray<T,K>
extends Array<T>

Array sub-class that indexes its elements using an indexer function

See Also:
Serialized Form

Constructor Summary
protected IndexedArray(Collection<T> elements, ArrayEvent<T> event, com.google.common.base.Function<? super T,K> indexer)
          Internal constructor used to create indexed arrays with their array events.
  IndexedArray(com.google.common.base.Function<? super T,K> indexer, Array<T> elements)
          Builds an indexed array from another array.
  IndexedArray(com.google.common.base.Function<? super T,K> indexer, Collection<T> elements)
          Builds an indexed array from a collection.
  IndexedArray(com.google.common.base.Function<? super T,K> indexer, T... elements)
          Builds an indexed array from an array.
 
Method Summary
 IndexedArray<T,K> add(Array<T> elements)
          Appends elements
 IndexedArray<T,K> add(Collection<T> elements)
          Appends elements
 IndexedArray<T,K> add(T... elements)
          Appends elements
protected  void checkKeys(Collection<T> elements)
          Checks that keys are unique
protected  Array<T> createNewArray(List<T> newList, ArrayEvent<T> tArrayEvent)
          Creation of an IndexedArray by update/add/delete operations.
 IndexedArray<T,K> delete(int... indexes)
          Deletes one or several elements using their indexes.
 IndexedArray<T,K> filter(com.google.common.base.Predicate<T> tPredicate)
          Filters the array using a predicate
 IndexedArray<T,K> filterOnKeys(com.google.common.base.Predicate<K> predicate)
          Filter this array using a predicate on the keys
 T getByIndex(K key)
          Gets an element by its key
 com.google.common.base.Function<? super T,K> getIndexer()
          Returns the indexer function which is used by the indexed array.
 Array<K> getKeyArray()
          Returns all keys as an array
 List<K> getKeyList()
          Returns a list of keys, ordered the same way of the items.
 Set<K> getKeys()
          Returns all keys as a set
 IndexedArray<T,K> insert(int position, Collection<T> elements)
          Appends elements at a given position
 IndexedArray<T,K> insert(int position, T... items)
          Appends elements at a given position
 IndexedArray<T,K> reverse()
          Creates a reversed version of this array
 IndexedArray<T,K> sub(int startIndex, int endIndex)
          Sub array using a range between two indexes.
 String toString()
          String representation.
<V,L> IndexedArray<V,L>
transform(com.google.common.base.Function<? super T,V> valueTransform, com.google.common.base.Function<? super V,L> newIndexer)
          Transforms this indexed array by transforming the values and re-indexing them using a new indexer function.
 IndexedArray<T,K> update(int index, T newElement)
          Updates an element with a new value
 
Methods inherited from class net.sf.sido.array.Array
empty, equals, get, getArrayEvent, getAt, getLength, getOrNull, hashCode, index, isEmpty, iterator, length, size, toList, transform
 
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

IndexedArray

public IndexedArray(com.google.common.base.Function<? super T,K> indexer,
                    Array<T> elements)
Builds an indexed array from another array.

Parameters:
indexer - Indexer to use
elements - Source array

IndexedArray

public IndexedArray(com.google.common.base.Function<? super T,K> indexer,
                    Collection<T> elements)
Builds an indexed array from a collection.

Parameters:
indexer - Indexer to use
elements - Source collection

IndexedArray

public IndexedArray(com.google.common.base.Function<? super T,K> indexer,
                    T... elements)
Builds an indexed array from an array.

Parameters:
indexer - Indexer to use
elements - Source array

IndexedArray

protected IndexedArray(Collection<T> elements,
                       ArrayEvent<T> event,
                       com.google.common.base.Function<? super T,K> indexer)
Internal constructor used to create indexed arrays with their array events.

Parameters:
elements - Source elements
event - Array event that created this array
indexer - Indexer to use
Method Detail

getIndexer

public com.google.common.base.Function<? super T,K> getIndexer()
Returns the indexer function which is used by the indexed array.

Returns:
Indexer function

getByIndex

public T getByIndex(K key)
Gets an element by its key

Parameters:
key - Key
Returns:
Associated item (can be null)

getKeyArray

public Array<K> getKeyArray()
Returns all keys as an array

Returns:
Array

getKeys

public Set<K> getKeys()
Returns all keys as a set

Returns:
Set of keys

getKeyList

public List<K> getKeyList()
Returns a list of keys, ordered the same way of the items.

Returns:
List of keys

filterOnKeys

public IndexedArray<T,K> filterOnKeys(com.google.common.base.Predicate<K> predicate)
Filter this array using a predicate on the keys

Parameters:
predicate - Predicate to use on the keys
Returns:
Filtered indexed array

transform

public <V,L> IndexedArray<V,L> transform(com.google.common.base.Function<? super T,V> valueTransform,
                                         com.google.common.base.Function<? super V,L> newIndexer)
Transforms this indexed array by transforming the values and re-indexing them using a new indexer function.

Type Parameters:
V - Type of the new values
L - Type of the new keys
Parameters:
valueTransform - Transform function for the values
newIndexer - Indexer for the new values
Returns:
Transformed indexed array

createNewArray

protected Array<T> createNewArray(List<T> newList,
                                  ArrayEvent<T> tArrayEvent)
Creation of an IndexedArray by update/add/delete operations.

Overrides:
createNewArray in class Array<T>
Parameters:
newList - List of elements
tArrayEvent - Associated array of events
Returns:
New instance

add

public IndexedArray<T,K> add(Array<T> elements)
Description copied from class: Array
Appends elements

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

add

public IndexedArray<T,K> add(Collection<T> elements)
Description copied from class: Array
Appends elements

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

add

public IndexedArray<T,K> add(T... elements)
Description copied from class: Array
Appends elements

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

delete

public IndexedArray<T,K> delete(int... indexes)
Description copied from class: Array
Deletes one or several elements using their indexes. Note that the indexes array does not need to be sorted.

Overrides:
delete in class Array<T>
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.

filter

public IndexedArray<T,K> filter(com.google.common.base.Predicate<T> tPredicate)
Description copied from class: Array
Filters the array using a predicate

Overrides:
filter in class Array<T>
Parameters:
tPredicate - 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.

insert

public IndexedArray<T,K> insert(int position,
                                T... items)
Description copied from class: Array
Appends elements at a given position

Overrides:
insert in class Array<T>
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.

sub

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

Overrides:
sub in class Array<T>
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)

update

public IndexedArray<T,K> update(int index,
                                T newElement)
Description copied from class: Array
Updates an element with a new value

Overrides:
update in class Array<T>
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 IndexedArray<T,K> reverse()
Description copied from class: Array
Creates a reversed version of this array

Overrides:
reverse in class Array<T>
Returns:
Reversed array
See Also:
Collections.reverse(java.util.List)

insert

public IndexedArray<T,K> insert(int position,
                                Collection<T> elements)
Description copied from class: Array
Appends elements at a given position

Overrides:
insert in class Array<T>
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.

checkKeys

protected void checkKeys(Collection<T> elements)
Checks that keys are unique


toString

public String toString()
Description copied from class: Array
String representation.

Overrides:
toString in class Array<T>
See Also:
List#toString()


Copyright © 2011. All Rights Reserved.