Class FastList<E>
- java.lang.Object
-
- javolution37.javolution.realtime.RealtimeObject
-
- javolution37.javolution.util.FastCollection<E>
-
- javolution37.javolution.util.FastList<E>
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>, Reusable, Realtime
public class FastList<E> extends FastCollection<E> implements Reusable, java.util.List<E>
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/deletionat 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 bysplittingthe list into smaller ones.FastList(as for anyFastCollectionsub-class) supports thread-safe, fast iterations without using iterators.FastList<String> list = new FastList<String>(); for (FastList.Node<String> n = list.head(), end = list.tail(); (n = n.getNext()) != end;) { String value = n.getValue(); // No typecast necessary. }FastListare fullyreusable, they maintain internal pools ofnodesobjects. When a node is removed from its list, it is automatically restored to its pool.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classFastList.Node<E>-
Nested classes/interfaces inherited from class javolution37.javolution.util.FastCollection
FastCollection.Record
-
Nested classes/interfaces inherited from class javolution37.javolution.realtime.RealtimeObject
RealtimeObject.Factory<T extends RealtimeObject>
-
Nested classes/interfaces inherited from interface javolution37.javolution.realtime.Realtime
Realtime.ObjectSpace
-
-
Constructor Summary
Constructors Constructor and Description FastList()Creates a list of small initial capacity.FastList(java.util.Collection<? extends E> 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(java.lang.String id)Creates a persistent list associated to the specified unique identifier (convenience method).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description booleanadd(E value)Appends the specified value to the end of this list (equivalent toaddLast(E)).voidadd(int index, E value)Inserts the specified value at the specified position in this list.booleanaddAll(int index, java.util.Collection<? extends E> values)Inserts all of the values in the specified collection into this list at the specified position.voidaddBefore(FastList.Node<E> next, E value)Inserts the specified value before the specified Node.voidaddFirst(E value)Inserts the specified value at the beginning of this list.voidaddLast(E value)Appends the specified value to the end of this list (fast).voidclear()Removes all of the values from this collection (optional operation).voiddelete(FastCollection.Record record)Deletes the specified record from this collection.Eget(int index)Returns the value at the specified position in this list.EgetFirst()Returns the first value of this list.EgetLast()Returns the last value of this list.inthashCode()Returns the hash code value for this list.FastList.Node<E>head()Returns the head record of this collection; it is the record such ashead().getNext()holds the first collection value.intindexOf(java.lang.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.java.util.Iterator<E>iterator()Returns an iterator over the elements in this list (allocated on the stack when executed in aPoolContext).intlastIndexOf(java.lang.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.java.util.ListIterator<E>listIterator()Returns a list iterator over the elements in this list (allocated on the stack when executed in aPoolContext).java.util.ListIterator<E>listIterator(int index)Returns a list iterator from the specified position (allocated on the stack when executed in aPoolContext).booleanmove(Realtime.ObjectSpace os)Moves this real-time object to the specified object space.static <E> FastList<E>newInstance()Returns a list allocated from the stack when executing in aPoolContext).Eremove(int index)Removes the value at the specified position in this list.EremoveFirst()Removes and returns the first value of this list.EremoveLast()Removes and returns the last value of this list (fast).voidreset()Resets the internal state of this object to its default values.Eset(int index, E value)Replaces the value at the specified position in this list with the specified value.intsize()Returns the number of values in this collection.java.util.List<E>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 aPoolContext).FastList.Node<E>tail()Returns the tail record of this collection; it is the record such astail().getPrevious()holds the last collection value.java.util.List<E>unmodifiable()Returns the unmodifiable view associated to this collection.EvalueOf(FastCollection.Record record)Returns the collection value for the specified record.-
Methods inherited from class javolution37.javolution.util.FastCollection
addAll, contains, containsAll, equals, getValueComparator, isEmpty, remove, removeAll, retainAll, setValueComparator, toArray, toArray, toText
-
Methods inherited from class javolution37.javolution.realtime.RealtimeObject
export, moveHeap, preserve, toString, unpreserve
-
-
-
-
Constructor Detail
-
FastList
public FastList()
Creates a list of small initial capacity.
-
FastList
public FastList(java.lang.String id)
Creates a persistent list associated to the specified unique identifier (convenience method).- Parameters:
id- the unique identifier for this map.- Throws:
java.lang.IllegalArgumentException- if the identifier is not unique.- See Also:
PersistentReference
-
FastList
public 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).- Parameters:
capacity- the initial capacity.
-
FastList
public FastList(java.util.Collection<? extends E> values)
Creates a list containing the specified values, in the order they are returned by the collection's iterator.- Parameters:
values- the values to be placed into this list.
-
-
Method Detail
-
newInstance
public static <E> FastList<E> newInstance()
Returns a list allocated from the stack when executing in aPoolContext).- Returns:
- a new, preallocated or recycled list instance.
-
add
public final boolean add(E value)
Appends the specified value to the end of this list (equivalent toaddLast(E)).- Specified by:
addin interfacejava.util.Collection<E>- Specified by:
addin interfacejava.util.List<E>- Overrides:
addin classFastCollection<E>- Parameters:
value- the value to be appended to this list.- Returns:
true(as per the general contract of theCollection.addmethod).
-
hashCode
public int hashCode()
Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:h = 1; Iterator i = list.iterator(); while (i.hasNext()) { Object obj = i.next(); h = 31 * h + this.getValueComparator().hashCodeOf(obj); }- Specified by:
hashCodein interfacejava.util.Collection<E>- Specified by:
hashCodein interfacejava.util.List<E>- Overrides:
hashCodein classFastCollection<E>- Returns:
- the hash code value for this list.
-
get
public final E get(int index)
Returns the value at the specified position in this list.- Specified by:
getin interfacejava.util.List<E>- Parameters:
index- the index of value to return.- Returns:
- the value at the specified position in this list.
- Throws:
java.lang.IndexOutOfBoundsException- if(index < 0) || (index >= size())
-
set
public final E set(int index, E value)
Replaces the value at the specified position in this list with the specified value.- Specified by:
setin interfacejava.util.List<E>- Parameters:
index- the index of value to replace.value- the value to be stored at the specified position.- Returns:
- the value previously at the specified position.
- Throws:
java.lang.IndexOutOfBoundsException- if(index < 0) || (index >= size())
-
add
public final void add(int index, E value)Inserts the specified value at the specified position in this list. Shifts the value currently at that position (if any) and any subsequent values to the right (adds one to their indices).- Specified by:
addin interfacejava.util.List<E>- Parameters:
index- the index at which the specified value is to be inserted.value- the value to be inserted.- Throws:
java.lang.IndexOutOfBoundsException- if(index < 0) || (index > size())
-
addAll
public final boolean addAll(int index, java.util.Collection<? extends E> values)Inserts all of the values in the specified collection into this list at the specified position. Shifts the value currently at that position (if any) and any subsequent values to the right (increases their indices).- Specified by:
addAllin interfacejava.util.List<E>- Parameters:
index- the index at which to insert first value from the specified collection.values- the values to be inserted into this list.- Returns:
trueif this list changed as a result of the call;falseotherwise.- Throws:
java.lang.IndexOutOfBoundsException- if(index < 0) || (index > size())
-
remove
public final E remove(int index)
Removes the value at the specified position in this list. Shifts any subsequent values to the left (subtracts one from their indices). Returns the value that was removed from the list.- Specified by:
removein interfacejava.util.List<E>- Parameters:
index- the index of the value to removed.- Returns:
- the value previously at the specified position.
- Throws:
java.lang.IndexOutOfBoundsException- if(index < 0) || (index >= size())
-
indexOf
public final int indexOf(java.lang.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.- Specified by:
indexOfin interfacejava.util.List<E>- Parameters:
value- the value to search for.- Returns:
- the index in this list of the first occurrence of the specified value, or -1 if this list does not contain this value.
-
lastIndexOf
public final int lastIndexOf(java.lang.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.- Specified by:
lastIndexOfin interfacejava.util.List<E>- Parameters:
value- the value to search for.- Returns:
- the index in this list of the last occurrence of the specified value, or -1 if this list does not contain this value.
-
iterator
public final java.util.Iterator<E> iterator()
Returns an iterator over the elements in this list (allocated on the stack when executed in aPoolContext).
-
listIterator
public final java.util.ListIterator<E> listIterator()
Returns a list iterator over the elements in this list (allocated on the stack when executed in aPoolContext).- Specified by:
listIteratorin interfacejava.util.List<E>- Returns:
- an iterator over this list values.
-
listIterator
public final java.util.ListIterator<E> listIterator(int index)
Returns a list iterator from the specified position (allocated on the stack when executed in aPoolContext). The specified index indicates the first value that would be returned by an initial call to thenextmethod. An initial call to thepreviousmethod would return the value with the specified index minus one.- Specified by:
listIteratorin interfacejava.util.List<E>- Parameters:
index- index of first value to be returned from the list iterator (by a call to thenextmethod).- Returns:
- a list iterator over the values in this list starting at the specified position in this list.
- Throws:
java.lang.IndexOutOfBoundsException- if the index is out of range.(index < 0 || index > size())
-
subList
public final java.util.List<E> 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 aPoolContext). 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 forindexOfandlastIndexOf, and all of the algorithms in theCollectionsclass 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).- Specified by:
subListin interfacejava.util.List<E>- Parameters:
fromIndex- low endpoint (inclusive) of the subList.toIndex- high endpoint (exclusive) of the subList.- Returns:
- a view of the specified range within this list.
- Throws:
java.lang.IndexOutOfBoundsException- if(fromIndex < 0 || toIndex > size || fromIndex < toIndex)
-
getFirst
public final E getFirst()
Returns the first value of this list.- Returns:
- this list's first value.
- Throws:
java.util.NoSuchElementException- if this list is empty.
-
getLast
public final E getLast()
Returns the last value of this list.- Returns:
- this list's last value.
- Throws:
java.util.NoSuchElementException- if this list is empty.
-
addFirst
public final void addFirst(E value)
Inserts the specified value at the beginning of this list.- Parameters:
value- the value to be inserted.
-
addLast
public void addLast(E value)
Appends the specified value to the end of this list (fast).- Parameters:
value- the value to be inserted.
-
removeFirst
public final E removeFirst()
Removes and returns the first value of this list.- Returns:
- this list's first value before this call.
- Throws:
java.util.NoSuchElementException- if this list is empty.
-
removeLast
public final E removeLast()
Removes and returns the last value of this list (fast).- Returns:
- this list's last value before this call.
- Throws:
java.util.NoSuchElementException- if this list is empty.
-
addBefore
public final void addBefore(FastList.Node<E> next, E value)
Inserts the specified value before the specified Node.- Parameters:
next- the Node before which this value is inserted.value- the value to be inserted.
-
head
public final FastList.Node<E> head()
Description copied from class:FastCollectionReturns the head record of this collection; it is the record such ashead().getNext()holds the first collection value.- Specified by:
headin classFastCollection<E>- Returns:
- the head record.
-
tail
public final FastList.Node<E> tail()
Description copied from class:FastCollectionReturns the tail record of this collection; it is the record such astail().getPrevious()holds the last collection value.- Specified by:
tailin classFastCollection<E>- Returns:
- the tail record.
-
valueOf
public final E valueOf(FastCollection.Record record)
Description copied from class:FastCollectionReturns the collection value for the specified record.- Specified by:
valueOfin classFastCollection<E>- Parameters:
record- the record whose current value is returned.- Returns:
- the current value.
-
delete
public final void delete(FastCollection.Record record)
Description copied from class:FastCollectionDeletes the specified record from this collection.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).
- Specified by:
deletein classFastCollection<E>- Parameters:
record- the record to be removed.
-
size
public final int size()
Description copied from class:FastCollectionReturns the number of values in this collection.- Specified by:
sizein interfacejava.util.Collection<E>- Specified by:
sizein interfacejava.util.List<E>- Specified by:
sizein classFastCollection<E>- Returns:
- the number of values.
-
clear
public final void clear()
Description copied from class:FastCollectionRemoves all of the values from this collection (optional operation).- Specified by:
clearin interfacejava.util.Collection<E>- Specified by:
clearin interfacejava.util.List<E>- Overrides:
clearin classFastCollection<E>
-
unmodifiable
public java.util.List<E> unmodifiable()
Description copied from class:FastCollectionReturns the unmodifiable view associated to this collection. Attempts to modify the returned collection result in anUnsupportedOperationExceptionbeing thrown. The view is typically part of the collection itself (created only once) and also an instance ofFastCollectionsupporting direct iterations.- Overrides:
unmodifiablein classFastCollection<E>- Returns:
- the unmodifiable view over this collection.
-
reset
public void reset()
Description copied from interface:ReusableResets the internal state of this object to its default values.
-
move
public boolean move(Realtime.ObjectSpace os)
Description copied from interface:RealtimeMoves this real-time object to the specified object space.- Specified by:
movein interfaceRealtime- Overrides:
movein classRealtimeObject- Parameters:
os- the object space to move this real-time object to.- Returns:
trueif the move has to be propagated to external real-time references;falseotherwise.
-
-
DMelt 3.0 © DataMelt by jWork.ORG