public class FastList extends FastCollection implements List, Reusable
This class represents a linked list with real-time behavior; smooth capacity increase and no memory allocation as long as the list size does not exceed its initial capacity.
All of the operations perform as could be expected for a doubly-linked
list (insertion
/deletion
at the end of the list are nonetheless the fastest).
Operations that index into the list will traverse the list from
the begining or the end whichever is closer to the specified index.
Random access operations can be significantly accelerated by
splitting
the list into smaller ones.
FastList
(as for any FastCollection
sub-class) supports
fast iterations without using iterators.[code]
FastList
FastList
are reusable
, they maintain
internal pools of nodes
objects. When a node is removed
from its list, it is automatically restored to its pool.
Custom list implementations may override the newNode()
method
in order to return their own FastList.Node
implementation (with
additional fields for example).
Modifier and Type | Class and Description |
---|---|
static class |
FastList.Node
|
FastCollection.Record
Constructor and Description |
---|
FastList()
Creates a list of small initial capacity.
|
FastList(Collection values)
Creates a list containing the specified values, in the order they
are returned by the collection's iterator.
|
FastList(int capacity)
Creates a list of specified initial capacity; unless the list size
reaches the specified capacity, operations on this list will not allocate
memory (no lazy object creation).
|
FastList(String id)
Creates a persistent list associated to the specified unique identifier
(convenience method).
|
Modifier and Type | Method and Description |
---|---|
void |
add(int index,
Object value)
Inserts the specified value at the specified position in this list.
|
boolean |
add(Object value)
Appends the specified value to the end of this list
(equivalent to
addLast(java.lang.Object) ). |
boolean |
addAll(int index,
Collection values)
Inserts all of the values in the specified collection into this
list at the specified position.
|
void |
addBefore(FastList.Node next,
Object value)
Inserts the specified value before the specified Node.
|
void |
addFirst(Object value)
Inserts the specified value at the beginning of this list.
|
void |
addLast(Object value)
Appends the specified value to the end of this list (fast).
|
void |
clear()
Removes all of the values from this collection (optional operation).
|
boolean |
contains(Object value)
Indicates if this collection contains the specified value.
|
void |
delete(FastCollection.Record record)
Deletes the specified record from this collection.
|
Object |
get(int index)
Returns the value at the specified position in this list.
|
Object |
getFirst()
Returns the first value of this list.
|
Object |
getLast()
Returns the last value of this list.
|
FastComparator |
getValueComparator()
Returns the value comparator for this collection (default
FastComparator.DEFAULT ). |
FastCollection.Record |
head()
Returns the head record of this collection; it is the record such as
head().getNext() holds the first collection value. |
int |
indexOf(Object value)
Returns the index in this list of the first occurrence of the specified
value, or -1 if this list does not contain this value.
|
Iterator |
iterator()
Returns a simple iterator over the elements in this list
(allocated on the stack when executed in a
StackContext ). |
int |
lastIndexOf(Object value)
Returns the index in this list of the last occurrence of the specified
value, or -1 if this list does not contain this value.
|
ListIterator |
listIterator()
Returns a list iterator over the elements in this list
(allocated on the stack when executed in a
StackContext ). |
ListIterator |
listIterator(int index)
Returns a list iterator from the specified position
(allocated on the stack when executed in a
StackContext ). |
static FastList |
newInstance()
Returns a new, preallocated or
recycled list instance
(on the stack when executing in a StackContext ). |
protected FastList.Node |
newNode()
Returns a new node for this list; this method can be overriden by
custom list implementation.
|
static void |
recycle(FastList instance)
Recycles a list
instance immediately
(on the stack when executing in a StackContext ). |
Object |
remove(int index)
Removes the value at the specified position in this list.
|
Object |
removeFirst()
Removes and returns the first value of this list.
|
Object |
removeLast()
Removes and returns the last value of this list (fast).
|
void |
reset()
Resets the internal state of this object to its default values.
|
Object |
set(int index,
Object value)
Replaces the value at the specified position in this list with the
specified value.
|
FastList |
setValueComparator(FastComparator comparator)
Sets the comparator to use for value equality.
|
int |
size()
Returns the number of values in this collection.
|
List |
subList(int fromIndex,
int toIndex)
Returns a view of the portion of this list between the specified
indexes (allocated from the "stack" when executing in a
StackContext ). |
FastCollection.Record |
tail()
Returns the tail record of this collection; it is the record such as
tail().getPrevious() holds the last collection value. |
Collection |
unmodifiable()
Returns the unmodifiable view associated to this collection.
|
Object |
valueOf(FastCollection.Record record)
Returns the collection value for the specified record.
|
addAll, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, shared, toArray, toArray, toString, toText
public FastList()
public FastList(String id)
id
- the unique identifier for this map.IllegalArgumentException
- if the identifier is not unique.PersistentContext.Reference
public FastList(int capacity)
capacity
- the initial capacity.public FastList(Collection values)
values
- the values to be placed into this list.public static FastList newInstance()
recycled
list instance
(on the stack when executing in a StackContext
).public static void recycle(FastList instance)
instance
immediately
(on the stack when executing in a StackContext
).public final boolean add(Object value)
addLast(java.lang.Object)
).add
in interface Collection
add
in interface List
add
in class FastCollection
value
- the value to be appended to this list.true
(as per the general contract of the
Collection.add
method).public final Object get(int index)
get
in interface List
index
- the index of value to return.IndexOutOfBoundsException
- if (index < 0) ||
(index >= size())
public final Object set(int index, Object value)
set
in interface List
index
- the index of value to replace.value
- the value to be stored at the specified position.IndexOutOfBoundsException
- if (index < 0) ||
(index >= size())
public final void add(int index, Object value)
add
in interface List
index
- the index at which the specified value is to be inserted.value
- the value to be inserted.IndexOutOfBoundsException
- if (index < 0) ||
(index > size())
public final boolean addAll(int index, Collection values)
addAll
in interface List
index
- the index at which to insert first value from the specified
collection.values
- the values to be inserted into this list.true
if this list changed as a result of the call;
false
otherwise.IndexOutOfBoundsException
- if (index < 0) ||
(index > size())
public final Object remove(int index)
remove
in interface List
index
- the index of the value to removed.IndexOutOfBoundsException
- if (index < 0) ||
(index >= size())
public final int indexOf(Object value)
public final int lastIndexOf(Object value)
lastIndexOf
in interface List
value
- the value to search for.public Iterator iterator()
StackContext
).iterator
in interface Iterable
iterator
in interface Collection
iterator
in interface List
iterator
in class FastCollection
public ListIterator listIterator()
StackContext
).listIterator
in interface List
public ListIterator listIterator(int index)
StackContext
).
The specified index indicates the first value that would be returned by
an initial call to the next
method. An initial call to
the previous
method would return the value with the
specified index minus one.listIterator
in interface List
index
- index of first value to be returned from the
list iterator (by a call to the next
method).IndexOutOfBoundsException
- if the index is out of range
[code](index < 0 || index > size())[/code].public final List subList(int fromIndex, int toIndex)
StackContext
).
If the specified indexes are equal, the returned list is empty.
The returned list is backed by this list, so non-structural changes in
the returned list are reflected in this list, and vice-versa.
This method eliminates the need for explicit range operations (of
the sort that commonly exist for arrays). Any operation that expects
a list can be used as a range operation by passing a subList view
instead of a whole list. For example, the following idiom
removes a range of values from a list:
list.subList(from, to).clear();
Similar idioms may be constructed for indexOf
and
lastIndexOf
, and all of the algorithms in the
Collections
class can be applied to a subList.
The semantics of the list returned by this method become undefined if
the backing list (i.e., this list) is structurally modified in
any way other than via the returned list (structural modifications are
those that change the size of this list, or otherwise perturb it in such
a fashion that iterations in progress may yield incorrect results).subList
in interface List
fromIndex
- low endpoint (inclusive) of the subList.toIndex
- high endpoint (exclusive) of the subList.IndexOutOfBoundsException
- if [code](fromIndex < 0 ||
toIndex > size || fromIndex < toIndex)[/code]public final Object getFirst()
NoSuchElementException
- if this list is empty.public final Object getLast()
NoSuchElementException
- if this list is empty.public final void addFirst(Object value)
value
- the value to be inserted.public void addLast(Object value)
value
- the value to be inserted.public final Object removeFirst()
NoSuchElementException
- if this list is empty.public final Object removeLast()
NoSuchElementException
- if this list is empty.public final void addBefore(FastList.Node next, Object value)
next
- the Node before which this value is inserted.value
- the value to be inserted.public final FastCollection.Record head()
FastCollection
head().getNext()
holds the first collection value.head
in class FastCollection
public final FastCollection.Record tail()
FastCollection
tail().getPrevious()
holds the last collection value.tail
in class FastCollection
public final Object valueOf(FastCollection.Record record)
FastCollection
valueOf
in class FastCollection
record
- the record whose current value is returned.public final void delete(FastCollection.Record record)
FastCollection
Implementation must ensure that removing a record from the collection does not affect in any way the records preceding the record being removed (it might affect the next records though, e.g. in a list collection, the indices of the subsequent records will change).
delete
in class FastCollection
record
- the record to be removed.public final boolean contains(Object value)
FastCollection
contains
in interface Collection
contains
in interface List
contains
in class FastCollection
value
- the value whose presence in this collection
is to be tested.true
if this collection contains the specified
value;false
otherwise.public final int size()
FastCollection
size
in interface Collection
size
in interface List
size
in class FastCollection
public final void clear()
FastCollection
clear
in interface Collection
clear
in interface List
clear
in class FastCollection
public FastList setValueComparator(FastComparator comparator)
comparator
- the value comparator.this
public FastComparator getValueComparator()
FastCollection
FastComparator.DEFAULT
).getValueComparator
in class FastCollection
public Collection unmodifiable()
FastCollection
UnsupportedOperationException
being thrown.unmodifiable
in class FastCollection
protected FastList.Node newNode()
Copyright © 2005–2018 Javolution. All rights reserved.