org.terracotta.collections
Class TerracottaList<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by org.terracotta.collections.TerracottaList<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess

public class TerracottaList<E>
extends AbstractCollection<E>
implements List<E>, Cloneable, Serializable, RandomAccess

TerracottaList is an implementation of List, backed by an array. All optional operations adding, removing, and replacing are supported. The elements can be any objects.

See Also:
Serialized Form

Field Summary
protected  int modCount
           
 
Constructor Summary
TerracottaList()
          Constructs a new instance of TerracottaList with ten capacity.
TerracottaList(Collection<? extends E> collection)
          Constructs a new instance of TerracottaList containing the elements of the specified collection.
TerracottaList(int capacity)
          Constructs a new instance of TerracottaList with the specified capacity.
 
Method Summary
 boolean add(E object)
          Adds the specified object at the end of this TerracottaList.
 void add(int location, E object)
          Inserts the specified object into this TerracottaList at the specified location.
 boolean addAll(Collection<? extends E> collection)
          Adds the objects in the specified collection to this TerracottaList.
 boolean addAll(int location, Collection<? extends E> collection)
          Inserts the objects in the specified collection at the specified location in this List.
 void clear()
          Removes all elements from this TerracottaList, leaving it empty.
 Object clone()
          Returns a new TerracottaList with the same elements, the same size and the same capacity as this TerracottaList.
 boolean contains(Object object)
          Searches this TerracottaList for the specified object.
 void ensureCapacity(int minimumCapacity)
          Ensures that after this operation the TerracottaList can hold the specified number of elements without further growing.
 boolean equals(Object object)
          Compares the specified object to this list and return true if they are equal.
 E get(int location)
           
 int hashCode()
          Returns the hash code of this list.
 int indexOf(Object object)
           
 boolean isEmpty()
           
 Iterator<E> iterator()
          Returns an iterator on the elements of this list.
 int lastIndexOf(Object object)
           
 ListIterator<E> listIterator()
          Returns a ListIterator on the elements of this list.
 ListIterator<E> listIterator(int location)
          Returns a list iterator on the elements of this list.
 E remove(int location)
          Removes the object at the specified location from this list.
 boolean remove(Object object)
           
protected  void removeRange(int start, int end)
          Removes the objects in the specified range from the start to the end, but not including the end index.
 E set(int location, E object)
          Replaces the element at the specified location in this TerracottaList with the specified object.
 int size()
          Returns the number of elements in this TerracottaList.
 List<E> subList(int start, int end)
          Returns a part of consecutive elements of this list as a view.
 Object[] toArray()
          Returns a new array containing all elements contained in this TerracottaList.
<T> T[]
toArray(T[] contents)
          Returns an array containing all elements contained in this TerracottaList.
 void trimToSize()
          Sets the capacity of this TerracottaList to be the same as the current size.
 
Methods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, removeAll, retainAll
 

Field Detail

modCount

protected transient int modCount
Constructor Detail

TerracottaList

public TerracottaList()
Constructs a new instance of TerracottaList with ten capacity.


TerracottaList

public TerracottaList(int capacity)
Constructs a new instance of TerracottaList with the specified capacity.

Parameters:
capacity - the initial capacity of this TerracottaList.

TerracottaList

public TerracottaList(Collection<? extends E> collection)
Constructs a new instance of TerracottaList containing the elements of the specified collection. The initial size of the TerracottaList will be 10% higher than the size of the specified collection.

Parameters:
collection - the collection of elements to add.
Method Detail

add

public void add(int location,
                E object)
Inserts the specified object into this TerracottaList at the specified location. The object is inserted before any previous element at the specified location. If the location is equal to the size of this TerracottaList, the object is added at the end.

Specified by:
add in interface List<E>
Parameters:
location - the index at which to insert the object.
object - the object to add.
Throws:
IndexOutOfBoundsException - when location < 0 || > size()

add

public boolean add(E object)
Adds the specified object at the end of this TerracottaList.

Specified by:
add in interface Collection<E>
Specified by:
add in interface List<E>
Overrides:
add in class AbstractCollection<E>
Parameters:
object - the object to add.
Returns:
always true

addAll

public boolean addAll(int location,
                      Collection<? extends E> collection)
Inserts the objects in the specified collection at the specified location in this List. The objects are added in the order they are returned from the collection's iterator.

Specified by:
addAll in interface List<E>
Parameters:
location - the index at which to insert.
collection - the collection of objects.
Returns:
true if this TerracottaList is modified, false otherwise.
Throws:
IndexOutOfBoundsException - when location < 0 || > size()

addAll

public boolean addAll(Collection<? extends E> collection)
Adds the objects in the specified collection to this TerracottaList.

Specified by:
addAll in interface Collection<E>
Specified by:
addAll in interface List<E>
Overrides:
addAll in class AbstractCollection<E>
Parameters:
collection - the collection of objects.
Returns:
true if this TerracottaList is modified, false otherwise.

clear

public void clear()
Removes all elements from this TerracottaList, leaving it empty.

Specified by:
clear in interface Collection<E>
Specified by:
clear in interface List<E>
Overrides:
clear in class AbstractCollection<E>
See Also:
isEmpty(), size()

clone

public Object clone()
Returns a new TerracottaList with the same elements, the same size and the same capacity as this TerracottaList.

Overrides:
clone in class Object
Returns:
a shallow copy of this TerracottaList
See Also:
Cloneable

contains

public boolean contains(Object object)
Searches this TerracottaList for the specified object.

Specified by:
contains in interface Collection<E>
Specified by:
contains in interface List<E>
Overrides:
contains in class AbstractCollection<E>
Parameters:
object - the object to search for.
Returns:
true if object is an element of this TerracottaList, false otherwise

ensureCapacity

public void ensureCapacity(int minimumCapacity)
Ensures that after this operation the TerracottaList can hold the specified number of elements without further growing.

Parameters:
minimumCapacity - the minimum capacity asked for.

get

public E get(int location)
Specified by:
get in interface List<E>

indexOf

public int indexOf(Object object)
Specified by:
indexOf in interface List<E>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<E>
Specified by:
isEmpty in interface List<E>
Overrides:
isEmpty in class AbstractCollection<E>

lastIndexOf

public int lastIndexOf(Object object)
Specified by:
lastIndexOf in interface List<E>

remove

public E remove(int location)
Removes the object at the specified location from this list.

Specified by:
remove in interface List<E>
Parameters:
location - the index of the object to remove.
Returns:
the removed object.
Throws:
IndexOutOfBoundsException - when location < 0 || >= size()

remove

public boolean remove(Object object)
Specified by:
remove in interface Collection<E>
Specified by:
remove in interface List<E>
Overrides:
remove in class AbstractCollection<E>

removeRange

protected void removeRange(int start,
                           int end)
Removes the objects in the specified range from the start to the end, but not including the end index.

Parameters:
start - the index at which to start removing.
end - the index one after the end of the range to remove.
Throws:
IndexOutOfBoundsException - when start < 0, start > end or end > size()

set

public E set(int location,
             E object)
Replaces the element at the specified location in this TerracottaList with the specified object.

Specified by:
set in interface List<E>
Parameters:
location - the index at which to put the specified object.
object - the object to add.
Returns:
the previous element at the index.
Throws:
IndexOutOfBoundsException - when location < 0 || >= size()

size

public int size()
Returns the number of elements in this TerracottaList.

Specified by:
size in interface Collection<E>
Specified by:
size in interface List<E>
Specified by:
size in class AbstractCollection<E>
Returns:
the number of elements in this TerracottaList.

toArray

public Object[] toArray()
Returns a new array containing all elements contained in this TerracottaList.

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Overrides:
toArray in class AbstractCollection<E>
Returns:
an array of the elements from this TerracottaList

toArray

public <T> T[] toArray(T[] contents)
Returns an array containing all elements contained in this TerracottaList. If the specified array is large enough to hold the elements, the specified array is used, otherwise an array of the same type is created. If the specified array is used and is larger than this TerracottaList, the array element following the collection elements is set to null.

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Overrides:
toArray in class AbstractCollection<E>
Parameters:
contents - the array.
Returns:
an array of the elements from this TerracottaList.
Throws:
ArrayStoreException - when the type of an element in this TerracottaList cannot be stored in the type of the specified array.

trimToSize

public void trimToSize()
Sets the capacity of this TerracottaList to be the same as the current size.

See Also:
size()

equals

public boolean equals(Object object)
Compares the specified object to this list and return true if they are equal. Two lists are equal when they both contain the same objects in the same order.

Specified by:
equals in interface Collection<E>
Specified by:
equals in interface List<E>
Overrides:
equals in class Object
Parameters:
object - the object to compare to this object.
Returns:
true if the specified object is equal to this list, false otherwise.
See Also:
hashCode()

hashCode

public int hashCode()
Returns the hash code of this list. The hash code is calculated by taking each element's hashcode into account.

Specified by:
hashCode in interface Collection<E>
Specified by:
hashCode in interface List<E>
Overrides:
hashCode in class Object
Returns:
the hash code.
See Also:
equals(java.lang.Object), List.hashCode()

iterator

public Iterator<E> iterator()
Returns an iterator on the elements of this list. The elements are iterated in the same order as they occur in the list.

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in interface List<E>
Specified by:
iterator in class AbstractCollection<E>
Returns:
an iterator on the elements of this list.
See Also:
Iterator

listIterator

public ListIterator<E> listIterator()
Returns a ListIterator on the elements of this list. The elements are iterated in the same order that they occur in the list.

Specified by:
listIterator in interface List<E>
Returns:
a ListIterator on the elements of this list
See Also:
ListIterator

listIterator

public ListIterator<E> listIterator(int location)
Returns a list iterator on the elements of this list. The elements are iterated in the same order as they occur in the list. The iteration starts at the specified location.

Specified by:
listIterator in interface List<E>
Parameters:
location - the index at which to start the iteration.
Returns:
a ListIterator on the elements of this list.
Throws:
IndexOutOfBoundsException - if location < 0 || location > size()
See Also:
ListIterator

subList

public List<E> subList(int start,
                       int end)
Returns a part of consecutive elements of this list as a view. The returned view will be of zero length if start equals end. Any change that occurs in the returned subList will be reflected to the original list, and vice-versa. All the supported optional operations by the original list will also be supported by this subList.

This method can be used as a handy method to do some operations on a sub range of the original list, for example list.subList(from, to).clear();

If the original list is modified in other ways than through the returned subList, the behavior of the returned subList becomes undefined.

The returned subList is a subclass of TerracottaList. The subclass stores offset, size of itself, and modCount of the original list. If the original list implements RandomAccess interface, the returned subList also implements RandomAccess interface.

The subList's set(int, Object), get(int), add(int, Object), remove(int), addAll(int, Collection) and removeRange(int, int) methods first check the bounds, adjust offsets and then call the corresponding methods of the original TerracottaList. addAll(Collection c) method of the returned subList calls the original addAll(offset + size, c).

The listIterator(int) method of the subList wraps the original list iterator. The iterator() method of the subList invokes the original listIterator() method, and the size() method merely returns the size of the subList.

All methods will throw a ConcurrentModificationException if the modCount of the original list is not equal to the expected value.

Specified by:
subList in interface List<E>
Parameters:
start - start index of the subList (inclusive).
end - end index of the subList, (exclusive).
Returns:
a subList view of this list starting from start (inclusive), and ending with end (exclusive)
Throws:
IndexOutOfBoundsException - if (start < 0 || end > size())
IllegalArgumentException - if (start > end)


Copyright © 2015 Terracotta, Inc.. All Rights Reserved.