com.neeve.lang
Class XLinkedList<E>

java.lang.Object
  extended by com.neeve.lang.XCollection<E>
      extended by com.neeve.lang.XLinkedList<E>
All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>

public class XLinkedList<E>
extends XCollection<E>
implements 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/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.

XLinkedList (as for any XCollection sub-class) supports fast iterations without using iterators.[code] XLinkedList list = new XLinkedList(); for (XLinkedList.Node n = list.head(), end = list.tail(); (n = n.getNext()) != end;) { String value = n.getValue(); // No typecast necessary. }[/code]

Alternatively The list can be iterated over using XCollection.reusableIterator(). Utilization of these iterators does not introduce garbage because they are created once. However, these iterators may not be used concurrently by multiple threads as they are single instance by nature.

Custom list implementations may override the newNode() method in order to return their own XLinkedList.Node implementation (with additional fields for example).

This class was based on collections code from http://javolution.org, but modified for use outside of realtime jvms.


Nested Class Summary
static class XLinkedList.Node<E>
          This class represents a XLinkedList node; it allows for direct iteration over the list values.
 
Nested classes/interfaces inherited from class com.neeve.lang.XCollection
XCollection.Record
 
Constructor Summary
XLinkedList()
          Creates a list of small initial capacity.
XLinkedList(Collection<? extends E> values)
          Creates a list containing the specified values, in the order they are returned by the collection's iterator.
XLinkedList(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).
 
Method Summary
 boolean add(E value)
          Appends the specified value to the end of this list (equivalent to addLast(E)).
 void add(int index, E value)
          Inserts the specified value at the specified position in this list.
 boolean addAll(int index, Collection<? extends E> values)
          Inserts all of the values in the specified collection into this list at the specified position.
 void addBefore(XLinkedList.Node<E> next, E value)
          Inserts the specified value before the specified Node.
 void addFirst(E value)
          Inserts the specified value at the beginning of this list.
 void addLast(E 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(XCollection.Record record)
          Deletes the specified record from this collection.
 E get(int index)
          Returns the value at the specified position in this list.
 E getFirst()
          Returns the first value of this list.
 E getLast()
          Returns the last value of this list.
 XLinkedList.Node<E> 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.
 XIterator<E> iterator()
          Returns a simple iterator over the elements in this list
 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.
 com.neeve.lang.XLinkedList.XLinkedListIterator<E> listIterator()
          Returns a list iterator over the elements in this list.
 ListIterator<E> listIterator(int index)
          Returns a list iterator from the specified position The specified index indicates the first value that would be returned by an initial call to the next method.
static
<E> XLinkedList<E>
newInstance()
          Returns a new list instance.
 E remove(int index)
          Removes the value at the specified position in this list.
 E removeFirst()
          Removes and returns the first value of this list.
 E removeLast()
          Removes and returns the last value of this list (fast).
 void reset()
           
 E set(int index, E value)
          Replaces the value at the specified position in this list with the specified value.
 int size()
          Returns the number of values in this collection.
 List<E> subList(int fromIndex, int toIndex)
          Returns a view of the portion of this list between the specified indexes If the specified indexes are equal, the returned list is empty.
 XLinkedList.Node<E> tail()
          Returns the tail record of this collection; it is the record such as tail().getPrevious() holds the last collection value.
 List<E> unmodifiable()
          Returns the unmodifiable view associated to this collection.
 E valueOf(XCollection.Record record)
          Returns the collection value for the specified record.
 
Methods inherited from class com.neeve.lang.XCollection
addAll, containsAll, copyTo, equals, hashCode, isEmpty, remove, removeAll, retainAll, reusableIterator, shared, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
addAll, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Constructor Detail

XLinkedList

public XLinkedList()
Creates a list of small initial capacity.


XLinkedList

public XLinkedList(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.

XLinkedList

public XLinkedList(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> XLinkedList<E> newInstance()
Returns a new list instance.

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 to addLast(E)).

Specified by:
add in interface Collection<E>
Specified by:
add in interface List<E>
Overrides:
add in class XCollection<E>
Parameters:
value - the value to be appended to this list.
Returns:
true (as per the general contract of the Collection.add method).

get

public final E get(int index)
Returns the value at the specified position in this list.

Specified by:
get in interface List<E>
Parameters:
index - the index of value to return.
Returns:
the value at the specified position in this list.
Throws:
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:
set in interface 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:
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:
add in interface List<E>
Parameters:
index - the index at which the specified value is to be inserted.
value - the value to be inserted.
Throws:
IndexOutOfBoundsException - if (index < 0) || (index > size())

addAll

public final boolean addAll(int index,
                            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:
addAll in interface 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:
true if this list changed as a result of the call; false otherwise.
Throws:
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:
remove in interface List<E>
Parameters:
index - the index of the value to removed.
Returns:
the value previously at the specified position.
Throws:
IndexOutOfBoundsException - if (index < 0) || (index >= size())

indexOf

public final 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.

Specified by:
indexOf in interface 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(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:
lastIndexOf in interface 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 XIterator<E> iterator()
Returns a simple iterator over the elements in this list

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in interface List<E>
Overrides:
iterator in class XCollection<E>
Returns:
an iterator over this list values.

listIterator

public com.neeve.lang.XLinkedList.XLinkedListIterator<E> listIterator()
Returns a list iterator over the elements in this list.

Specified by:
listIterator in interface List<E>
Returns:
an iterator over this list values.

listIterator

public ListIterator<E> listIterator(int index)
Returns a list iterator from the specified position 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.

Specified by:
listIterator in interface List<E>
Parameters:
index - index of first value to be returned from the list iterator (by a call to the next method).
Returns:
a list iterator over the values in this list starting at the specified position in this list.
Throws:
IndexOutOfBoundsException - if the index is out of range [code](index < 0 || index > size())[/code].

subList

public final List<E> subList(int fromIndex,
                             int toIndex)
Returns a view of the portion of this list between the specified indexes 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).

Specified by:
subList in interface 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:
IndexOutOfBoundsException - if [code](fromIndex < 0 || toIndex > size || fromIndex < toIndex)[/code]

getFirst

public final E getFirst()
Returns the first value of this list.

Returns:
this list's first value.
Throws:
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:
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:
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:
NoSuchElementException - if this list is empty.

addBefore

public final void addBefore(XLinkedList.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 XLinkedList.Node<E> head()
Description copied from class: XCollection
Returns the head record of this collection; it is the record such as head().getNext() holds the first collection value.

Specified by:
head in class XCollection<E>
Returns:
the head record.

tail

public final XLinkedList.Node<E> tail()
Description copied from class: XCollection
Returns the tail record of this collection; it is the record such as tail().getPrevious() holds the last collection value.

Specified by:
tail in class XCollection<E>
Returns:
the tail record.

valueOf

public final E valueOf(XCollection.Record record)
Description copied from class: XCollection
Returns the collection value for the specified record.

Specified by:
valueOf in class XCollection<E>
Parameters:
record - the record whose current value is returned.
Returns:
the current value.

delete

public final void delete(XCollection.Record record)
Description copied from class: XCollection
Deletes 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:
delete in class XCollection<E>
Parameters:
record - the record to be removed.

contains

public final boolean contains(Object value)
Description copied from class: XCollection
Indicates if this collection contains the specified value.

Specified by:
contains in interface Collection<E>
Specified by:
contains in interface List<E>
Overrides:
contains in class XCollection<E>
Parameters:
value - the value whose presence in this collection is to be tested.
Returns:
true if this collection contains the specified value;false otherwise.

size

public final int size()
Description copied from class: XCollection
Returns the number of values in this collection.

Specified by:
size in interface Collection<E>
Specified by:
size in interface List<E>
Specified by:
size in class XCollection<E>
Returns:
the number of values.

clear

public final void clear()
Description copied from class: XCollection
Removes all of the values from this collection (optional operation).

Specified by:
clear in interface Collection<E>
Specified by:
clear in interface List<E>
Overrides:
clear in class XCollection<E>

unmodifiable

public List<E> unmodifiable()
Description copied from class: XCollection
Returns the unmodifiable view associated to this collection. Attempts to modify the returned collection result in an UnsupportedOperationException being thrown.

Overrides:
unmodifiable in class XCollection<E>
Returns:
the unmodifiable view over this collection.

reset

public void reset()


Copyright © 2016 Neeve Research, LLC. All Rights Reserved.