com.neeve.lang
Class XLongIndexedList

java.lang.Object
  extended by com.neeve.lang.XLongCollectionBase
      extended by com.neeve.lang.XLongIndexedList
All Implemented Interfaces:
XLongCollection, XLongList, RandomAccess

public class XLongIndexedList
extends XLongCollectionBase
implements XLongList, RandomAccess

This class represents a random access collection with real-time behavior (smooth capacity increase) for a primitivive list of longs

This class has the following advantages over the widely used java.util.ArrayList:

Iterations over the XLongIndexedList values are faster when performed using the get(int) method rather than using collection records or iterators:[code] for (int i = 0, n = list.size(); i < n; i++) { list.get(i); }[/code]

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


Constructor Summary
XLongIndexedList()
          Creates a list of small initial capacity.
XLongIndexedList(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).
XLongIndexedList(XLongCollection values)
          Creates a list containing the specified values, in the order they are returned by the collection's iterator.
 
Method Summary
 void add(int index, long value)
          Inserts the specified value at the specified position in this list.
 boolean add(long value)
          Appends the specified value to the end of this list.
 boolean addAll(int index, XLongCollection values)
          Inserts all of the values in the specified collection into this list at the specified position.
 void addLast(long 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(long value)
          Indicates if this collection contains the specified value.
 void delete(XCollection.Record record)
          Deletes the specified record from this collection.
 long get(int index)
          Returns the element at the specified index.
 long getFirst()
          Returns the first value of this list.
 long getLast()
          Returns the last value of this list.
 XCollection.Record head()
          Returns the head record of this collection; it is the record such as head().getNext() holds the first collection value.
 int indexOf(long 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.
 XLongIterator iterator()
          Returns an iterator over the elements in this list
 int lastIndexOf(long 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.
 XLongListIterator listIterator()
          Returns a list iterator over the elements in this list
 XLongListIterator listIterator(int index)
          Returns a list iterator from the specified position
static XLongIndexedList newInstance()
          Returns a new, preallocated or recycled XLongIndexedList instance
 long remove(int index)
          Removes the value at the specified position from this list.
 long removeLast()
          Removes and returns the last value of this list (fast).
 void removeRange(int fromIndex, int toIndex)
          Removes the values between [fromIndex..toIndex[ from this list.
 void reset()
           
 long set(int index, long value)
          Replaces the value at the specified position in this list with the specified value.
 void setSize(int size)
          Sets the size of this list.
 int size()
          Returns the number of values in this collection.
 void sort(boolean ascending)
           
 XLongList subList(int fromIndex, int toIndex)
          Returns a view of the portion of this list between the specified If the specified indexes are equal, the returned list is empty.
 XCollection.Record tail()
          Returns the tail record of this collection; it is the record such as tail().getPrevious() holds the last collection value.
 void trimToSize()
          Reduces the capacity of this list to the current size (minimize storage space).
 XLongList unmodifiable()
          Returns the unmodifiable view associated to this collection.
 long valueOf(XCollection.Record record)
          Returns the collection value for the specified record.
 
Methods inherited from class com.neeve.lang.XLongCollectionBase
addAll, containsAll, 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 com.neeve.lang.XLongList
addAll, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, toArray, toArray
 
Methods inherited from interface com.neeve.lang.XLongCollection
reusableIterator, shared
 

Constructor Detail

XLongIndexedList

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


XLongIndexedList

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

XLongIndexedList

public XLongIndexedList(XLongCollection 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 XLongIndexedList newInstance()
Returns a new, preallocated or recycled XLongIndexedList instance

Returns:
a new, preallocated or recycled XLongIndexedList instance.

setSize

public void setSize(int size)
Sets the size of this list. If the specified size is greater than the current size then null elements are added; otherwise the last elements are removed until the desired size is reached.

Parameters:
size - the new size.

get

public final long get(int index)
Returns the element at the specified index.

Specified by:
get in interface XLongList
Parameters:
index - 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 long set(int index,
                      long value)
Replaces the value at the specified position in this list with the specified value.

Specified by:
set in interface XLongList
Parameters:
index - index of value to replace.
value - value to be stored at the specified position.
Returns:
previous value.
Throws:
IndexOutOfBoundsException - if (index < 0) || (index >= size())

add

public final boolean add(long value)
Appends the specified value to the end of this list.

Specified by:
add in interface XLongCollection
Specified by:
add in interface XLongList
Overrides:
add in class XLongCollectionBase
Parameters:
value - the value to be appended to this list.
Returns:
true (as per the general contract of the Collection.add method).

getFirst

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

Returns:
this list first value.
Throws:
NoSuchElementException - if this list is empty.

getLast

public final long getLast()
Returns the last value of this list.

Returns:
this list last value.
Throws:
NoSuchElementException - if this list is empty.

addLast

public final void addLast(long value)
Appends the specified value to the end of this list (fast).

Parameters:
value - the value to be added.

removeLast

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

clear

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

Specified by:
clear in interface XLongCollection
Specified by:
clear in interface XLongList
Overrides:
clear in class XLongCollectionBase

reset

public void reset()

addAll

public final boolean addAll(int index,
                            XLongCollection 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).

Note: If this method is used concurrent access must be synchronized (the list is no more thread-safe).

Specified by:
addAll in interface XLongList
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())

add

public final void add(int index,
                      long 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).

Note: If this method is used concurrent access must be synchronized (the list is no more thread-safe).

Specified by:
add in interface XLongList
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())

remove

public final long remove(int index)
Removes the value at the specified position from this list. Shifts any subsequent values to the left (subtracts one from their indices). Returns the value that was removed from the list.

Note: If this method is used concurrent access must be synchronized (the list is no more thread-safe).

Specified by:
remove in interface XLongList
Parameters:
index - the index of the value to removed.
Returns:
the value previously at the specified position.
Throws:
IndexOutOfBoundsException - if (index < 0) || (index >= size())

removeRange

public final void removeRange(int fromIndex,
                              int toIndex)
Removes the values between [fromIndex..toIndex[ from this list.

Note: If this method is used concurrent access must be synchronized (the list is no more thread-safe).

Parameters:
fromIndex - the beginning index, inclusive.
toIndex - the ending index, exclusive.
Throws:
IndexOutOfBoundsException - if (fromIndex < 0) || (toIndex < 0) || (fromIndex > toIndex) || (toIndex > this.size())

indexOf

public final int indexOf(long 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 XLongList
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(long 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 XLongList
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 XLongIterator iterator()
Returns an iterator over the elements in this list

Specified by:
iterator in interface XLongCollection
Specified by:
iterator in interface XLongList
Overrides:
iterator in class XLongCollectionBase
Returns:
an iterator over this list values.

listIterator

public XLongListIterator listIterator()
Returns a list iterator over the elements in this list

Specified by:
listIterator in interface XLongList
Returns:
an iterator over this list values.

listIterator

public XLongListIterator listIterator(int index)
Returns a list iterator from the specified position

Specified by:
listIterator in interface XLongList
Parameters:
index - the index of first value to be returned from the list iterator (by a call to the next method).
Returns:
a list iterator of 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 XLongList subList(int fromIndex,
                               int toIndex)
Returns a view of the portion of this list between the specified 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: [code] list.subList(from, to).clear();[/code] 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 XLongList
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]

trimToSize

public final void trimToSize()
Reduces the capacity of this list to the current size (minimize storage space).


sort

public void sort(boolean ascending)

size

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

Specified by:
size in interface XLongCollection
Specified by:
size in interface XLongList
Specified by:
size in class XLongCollectionBase
Returns:
the number of values.

head

public final XCollection.Record head()
Description copied from class: XLongCollectionBase
Returns the head record of this collection; it is the record such as head().getNext() holds the first collection value.

Specified by:
head in interface XLongCollection
Specified by:
head in class XLongCollectionBase
Returns:
the head record.

tail

public final XCollection.Record tail()
Description copied from class: XLongCollectionBase
Returns the tail record of this collection; it is the record such as tail().getPrevious() holds the last collection value.

Specified by:
tail in interface XLongCollection
Specified by:
tail in class XLongCollectionBase
Returns:
the tail record.

valueOf

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

Specified by:
valueOf in interface XLongCollection
Specified by:
valueOf in class XLongCollectionBase
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: XLongCollectionBase
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 interface XLongCollection
Specified by:
delete in class XLongCollectionBase
Parameters:
record - the record to be removed.

unmodifiable

public XLongList unmodifiable()
Description copied from class: XLongCollectionBase
Returns the unmodifiable view associated to this collection. Attempts to modify the returned collection result in an UnsupportedOperationException being thrown.

Specified by:
unmodifiable in interface XLongCollection
Overrides:
unmodifiable in class XLongCollectionBase
Returns:
the unmodifiable view over this collection.

contains

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

Specified by:
contains in interface XLongCollection
Specified by:
contains in interface XLongList
Overrides:
contains in class XLongCollectionBase
Parameters:
value - the value whose presence in this collection is to be tested.
Returns:
true if this collection contains the specified value;false otherwise.


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