public class XShortIndexedList extends XShortCollectionBase implements XShortList, RandomAccess
This class represents a random access collection with real-time behavior (smooth capacity increase) for a primitivive list of shorts
This class has the following advantages over the widely used
java.util.ArrayList
:
short
and Short
.shared
. Iterations over the XShortIndexedList
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 and Description |
---|
XShortIndexedList()
Creates a list of small initial capacity.
|
XShortIndexedList(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).
|
XShortIndexedList(XShortCollection values)
Creates a list containing the specified values, in the order they
are returned by the collection's iterator.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int index,
short value)
Inserts the specified value at the specified position in this list.
|
boolean |
add(short value)
Appends the specified value to the end of this list.
|
boolean |
addAll(int index,
XShortCollection values)
Inserts all of the values in the specified collection into this
list at the specified position.
|
void |
addLast(short 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(short value)
Indicates if this collection contains the specified value.
|
void |
delete(XCollection.Record record)
Deletes the specified record from this collection.
|
short |
get(int index)
Returns the element at the specified index.
|
short |
getFirst()
Returns the first value of this list.
|
short |
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(short 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.
|
XShortIterator |
iterator()
Returns an iterator over the elements in this list
|
int |
lastIndexOf(short 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.
|
XShortListIterator |
listIterator()
Returns a list iterator over the elements in this list
|
XShortListIterator |
listIterator(int index)
Returns a list iterator from the specified position
|
static XShortIndexedList |
newInstance()
Returns a new, preallocated or recycled
XShortIndexedList instance |
short |
remove(int index)
Removes the value at the specified position from this list.
|
short |
removeLast()
Removes and returns the last value of this list (fast).
|
void |
removeRange(int fromIndex,
int toIndex)
Removes the values between
[fromIndex..toIndex[ |
void |
reset() |
short |
set(int index,
short 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) |
XShortList |
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).
|
XShortList |
unmodifiable()
Returns the unmodifiable view associated to this collection.
|
short |
valueOf(XCollection.Record record)
Returns the collection value for the specified record.
|
addAll, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, reusableIterator, shared, supportsIteratorModifications, toArray, toArray, toString
addAll, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, toArray, toArray
reusableIterator, shared, supportsIteratorModifications
public XShortIndexedList()
public XShortIndexedList(int capacity)
capacity
- the initial capacity.public XShortIndexedList(XShortCollection values)
values
- the values to be placed into this list.public static XShortIndexedList newInstance()
XShortIndexedList
instanceXShortIndexedList
instance.public void setSize(int size)
null
elements are added; otherwise
the last elements are removed until the desired size is reached.size
- the new size.public final short get(int index)
get
in interface XShortList
index
- index of value to return.IndexOutOfBoundsException
- if (index < 0) ||
(index >= size())
public final short set(int index, short value)
set
in interface XShortList
index
- index of value to replace.value
- value to be stored at the specified position.IndexOutOfBoundsException
- if (index < 0) ||
(index >= size())
public final boolean add(short value)
add
in interface XShortCollection
add
in interface XShortList
add
in class XShortCollectionBase
value
- the value to be appended to this list.true
(as per the general contract of the
Collection.add
method).public final short getFirst()
NoSuchElementException
- if this list is empty.public final short getLast()
NoSuchElementException
- if this list is empty.public final void addLast(short value)
value
- the value to be added.public final short removeLast()
NoSuchElementException
- if this list is empty.public final void clear()
XShortCollectionBase
clear
in interface XShortCollection
clear
in interface XShortList
clear
in class XShortCollectionBase
public void reset()
public final boolean addAll(int index, XShortCollection values)
Note: If this method is used concurrent access must be synchronized (the list is no more thread-safe).
addAll
in interface XShortList
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 void add(int index, short value)
Note: If this method is used concurrent access must be synchronized (the list is no more thread-safe).
add
in interface XShortList
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 short remove(int index)
Note: If this method is used concurrent access must be synchronized (the list is no more thread-safe).
remove
in interface XShortList
index
- the index of the value to removed.IndexOutOfBoundsException
- if (index < 0) ||
(index >= size())
public final void removeRange(int fromIndex, int toIndex)
[fromIndex..toIndex[ from
this list.
Note: If this method is used concurrent access must be synchronized
(the list is no more thread-safe).
fromIndex
- the beginning index, inclusive.toIndex
- the ending index, exclusive.IndexOutOfBoundsException
- if (fromIndex < 0) || (toIndex < 0)
|| (fromIndex > toIndex) || (toIndex > this.size())
public final int indexOf(short value)
indexOf
in interface XShortList
value
- the value to search for.public final int lastIndexOf(short value)
lastIndexOf
in interface XShortList
value
- the value to search for.public XShortIterator iterator()
iterator
in interface XShortCollection
iterator
in interface XShortList
iterator
in class XShortCollectionBase
public XShortListIterator listIterator()
listIterator
in interface XShortList
public XShortListIterator listIterator(int index)
listIterator
in interface XShortList
index
- the 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 XShortList subList(int fromIndex, int toIndex)
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 XShortList
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 void trimToSize()
public void sort(boolean ascending)
public final int size()
XShortCollectionBase
size
in interface XShortCollection
size
in interface XShortList
size
in class XShortCollectionBase
public final XCollection.Record head()
XShortCollectionBase
head().getNext()
holds the first collection value.head
in interface XShortCollection
head
in class XShortCollectionBase
public final XCollection.Record tail()
XShortCollectionBase
tail().getPrevious()
holds the last collection value.tail
in interface XShortCollection
tail
in class XShortCollectionBase
public final short valueOf(XCollection.Record record)
XShortCollectionBase
valueOf
in interface XShortCollection
valueOf
in class XShortCollectionBase
record
- the record whose current value is returned.public final void delete(XCollection.Record record)
XShortCollectionBase
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 interface XShortCollection
delete
in class XShortCollectionBase
record
- the record to be removed.public XShortList unmodifiable()
XShortCollectionBase
UnsupportedOperationException
being thrown.unmodifiable
in interface XShortCollection
unmodifiable
in class XShortCollectionBase
public final boolean contains(short value)
XShortCollectionBase
contains
in interface XShortCollection
contains
in interface XShortList
contains
in class XShortCollectionBase
value
- the value whose presence in this collection
is to be tested.true
if this collection contains the specified
value;false
otherwise.Copyright © 2019 Neeve Research, LLC. All Rights Reserved.