public class YList extends Object implements List<Object>, ICollection<Object>
The cells are represented by class ListCell
.
This class supports fast access and removal operations, specifically, it is possible to remove an element in constant time (i.e. O(1)) given a reference to its list cell.
Class YList supports iteration over the elements either by using the list cells directly (methods
FirstCell
/LastCell
together with
succCell(ListCell)
/predCell(ListCell)
, respectively) or by means of a cursor (cursor()
).
Furthermore, YList offers its own sort()
method. Note that this class also provides all relevant methods to use
the list like a stack data type.
This implementation permits null
as values. It implements the List
interface but does not support the subList(int, int)
method. The implementation of this method will throw an UnsupportedOperationException
if invoked. The cursor()
returned by instances of this class is not fail fast.
EMPTY
Constructor and Description |
---|
YList()
Creates an empty doubly linked list.
|
YList(ICursor c)
Creates a list that is initialized with the elements provided by the given YCursor object.
|
YList(ICursor c,
IDataProvider predicate)
Creates a list that is initialized with those elements from the given YCursor object for which the given data provider
returns
true upon calling its getBool method. |
YList(IEnumerator e)
Creates a list that is initialized with the elements provided by the given enumerator object.
|
YList(Iterable<Object> c)
Creates a list that is initialized with the elements provided by the given Collection object.
|
YList(Object[] a)
Creates a list that is initialized with the elements provided by the given array of objects.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int index,
Object element) |
boolean |
add(Object o)
Same as
addLast(Object) . |
boolean |
addAll(Collection<? extends Object> collection)
Appends all elements provided by the given collection to this list.
|
void |
addAll(ICursor c)
Appends all elements provided by the given cursor to this list.
|
boolean |
addAll(int index,
Collection<? extends Object> c) |
ListCell |
addFirst(Object o)
Inserts the given object at the head of this list.
|
void |
addFirstCell(ListCell cell)
Adds a formerly removed ListCell object at the head of this list.
|
ListCell |
addLast(Object o)
Inserts the given object at the tail of this list.
|
void |
addLastCell(ListCell cell)
Adds a formerly removed ListCell object at the tail of this list.
|
void |
clear()
Removes all elements from this list.
|
boolean |
contains(Object o)
Whether or not this list contains the given element.
|
boolean |
containsAll(Collection<? extends Object> collection)
Whether or not this list contains all the elements in the given collection.
|
void |
copyTo(Object[] array,
int arrayIndex)
Copies the elements of this collection to an array, starting at the specified array index.
|
void |
copyTo(Object array,
int index)
|
ICursor |
cursor()
Returns a cursor for this list.
|
ListCell |
cyclicPred(ListCell c)
Returns the cyclic predecessor cell of the given list cell.
|
ListCell |
cyclicSucc(ListCell c)
Returns the cyclic successor cell of the given list cell.
|
Object |
elementAt(int i)
Returns the i-th element of this list.
|
IEnumerator<Object> |
enumerator()
Returns an enumerator that iterates through this collection.
|
boolean |
equals(Object other) |
ListCell |
findCell(Object o)
Returns the
ListCell where object o is stored. |
Object |
first()
Gets the first element of this list.
|
ListCell |
firstCell()
Gets the first cell of this list.
|
Object |
get(int index) |
ListCell |
getCell(int index)
Gets the cell at the given index.
|
Object |
getInfo(ListCell c)
Returns the element stored in the given list cell.
|
int |
hashCode() |
int |
indexOf(Object obj)
Returns the zero-based index of the given element in this list.
|
ListCell |
insertAfter(Object o,
ListCell refCell)
Inserts the given object into this list with respect to a given reference list cell.
|
ListCell |
insertBefore(Object o,
ListCell refCell)
Inserts the given object into this list with respect to a given reference list cell.
|
void |
insertCellAfter(ListCell cellToInsert,
ListCell refCell)
Inserts a formerly removed ListCell object into this list with respect to a given reference list cell.
|
void |
insertCellBefore(ListCell cellToInsert,
ListCell refCell)
Inserts a formerly removed ListCell object into this list with respect to a given reference list cell.
|
boolean |
isEmpty()
Checks whether this list contains elements.
|
Object |
last()
Gets the last element of this list.
|
ListCell |
lastCell()
Gets the last cell of this list.
|
int |
lastIndexOf(Object o) |
ListIterator |
listIterator() |
Stream<Object> |
parallelStream()
Returns a parallel
Stream using the elements of this enumerable. |
Object |
peek()
Equivalent to
First . |
Object |
pop()
Removes the first element from this list and returns it.
|
Object |
popLast()
Removes the last element from this list and returns it.
|
ListCell |
predCell(ListCell c)
Returns the predecessor cell of the given list cell.
|
ListCell |
push(Object o)
Equivalent to
addFirst(Object) . |
boolean |
remove(Object o)
Removes the given object from this list.
|
boolean |
removeAll(Collection<? extends Object> collection)
Removes the given collection of objects from this list.
|
Object |
removeAt(ICursor c)
Removes the element pointed to by the given YCursor object.
|
Object |
removeCell(ListCell c)
Removes the given list cell, and hence the element stored in it, from this list.
|
boolean |
retainAll(Collection<? extends Object> collection)
Retains only those elements in this list which are contained in the given collection.
|
void |
reverse()
Reverses the sequence of elements in this list.
|
Object |
set(int index,
Object element) |
void |
setInfo(ListCell c,
Object value)
Updates the element stored in the given list cell with the given object.
|
int |
size()
Gets the number of elements in this list.
|
void |
sort()
Sorts the elements in this list into ascending order, according to their natural ordering.
|
void |
sort(Comparator<Object> comp)
Sorts the elements in this list according to the given comparator.
|
void |
splice(YList list)
Transfers the contents of the given list to the end of this list.
|
Stream<Object> |
stream()
Returns a sequential
Stream using the elements of this enumerable. |
List<Object> |
subList(int fromIndex,
int toIndex) |
ListCell |
succCell(ListCell c)
Returns the successor cell of the given list cell.
|
Object[] |
toArray()
Returns an array representation of this list.
|
Object[] |
toArray(Object[] a)
Returns an array containing all list elements in the correct order.
|
String |
toString()
Returns a string representation of this List.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
iterator, listIterator, remove, replaceAll, spliterator
iterator
concat, concat, create, create
removeIf
public YList()
public YList(ICursor c)
public YList(ICursor c, IDataProvider predicate)
true
upon calling its getBool
method.c
- A cursor providing objects that should be added to this list.predicate
- A data provider that acts as a inclusion predicate for each object accessible by the given cursor.public YList(IEnumerator e)
Creates a list that is initialized with the elements provided by the given enumerator object.
public YList(Iterable<Object> c)
public YList(Object[] a)
public final boolean add(Object o)
addLast(Object)
.public final boolean addAll(Collection<? extends Object> collection)
addAll
in interface ICollection<Object>
addAll
in interface Collection<Object>
addAll
in interface List<Object>
public final void addAll(ICursor c)
The cursor will be moved from its given position to the end.
Be aware that a statement like aList.append(aList.cursor())
results in an infinite recursion.
public final boolean addAll(int index, Collection<? extends Object> c)
public final ListCell addFirst(Object o)
public final void addFirstCell(ListCell cell)
Attention: If the ListCell object is still part of any list, then that list will be corrupted afterwards.
cell
- A list cell which is not part of any list.public final ListCell addLast(Object o)
public final void addLastCell(ListCell cell)
Attention: If the ListCell object is still part of any list, then that list will be corrupted afterwards.
cell
- A list cell which is not part of any list.public final void clear()
public final boolean contains(Object o)
Equality of elements is defined by the Object.equals(Object)
method.
public final boolean containsAll(Collection<? extends Object> collection)
Equality of elements is defined by the Object.equals(Object)
method.
containsAll
in interface ICollection<Object>
containsAll
in interface Collection<Object>
containsAll
in interface List<Object>
public final void copyTo(Object[] array, int arrayIndex)
array
- The one-dimensional array that is the destination of the elements copied from this collection.arrayIndex
- The zero-based index in array
at which copying begins.public void copyTo(Object array, int index)
public final ICursor cursor()
All cursor operations are supported. This cursor implementation is not fail-fast and continues to work if this list is modified during the traversal as long as the current ListCell the cursor points at is this in this list or has been removed from this list but has not been added to another instance since then.
public final ListCell cyclicPred(ListCell c)
The last cell is returned as the cyclic predecessor of the first list cell.
public final ListCell cyclicSucc(ListCell c)
The first cell is returned as the cyclic successor of the last list cell.
public final Object elementAt(int i)
i
is a valid index, i.e., i >= 0 && i < size()
.public final IEnumerator<Object> enumerator()
enumerator
in interface IEnumerable<Object>
IEnumerator
that can be used to iterate through this collection.public boolean equals(Object other)
public final ListCell findCell(Object o)
ListCell
where object o
is stored.
This operation returns null
, if no such cell exists. Equality of elements is defined by the Object.equals(Object)
method. The first element in the list that matches that criteria is returned.
null
if no such ListCell was foundpublic final Object first()
!isEmpty()
.public final ListCell firstCell()
!isEmpty()
.public final ListCell getCell(int index)
IndexOutOfBoundsException
- if the index is negative or greater or equal than the Count
index
- the zero-based index of the cell in this list.public int hashCode()
public final int indexOf(Object obj)
If the given element is not in the list, -1 is returned.
public final ListCell insertAfter(Object o, ListCell refCell)
The (newly created) list cell that stores the object is inserted right after the reference list cell refCell
.
If refCell == null
, the given object is inserted at the head of the list.
refCell
must be part of this list.o
- The object to be inserted.refCell
- The list cell used to reference the position.o
.public final ListCell insertBefore(Object o, ListCell refCell)
The (newly created) list cell that stores the object is inserted right before the reference list cell refCell
.
If refCell == null
, the given object is appended to the list.
refCell
must be part of this list.o
- The object to be inserted.refCell
- The list cell used to reference the position.o
.public final void insertCellAfter(ListCell cellToInsert, ListCell refCell)
The ListCell object is inserted right after the reference list cell refCell
.
Attention: If the ListCell object is still part of any list, then that list will be corrupted afterwards.
refCell
must be part of this list.cellToInsert
- A list cell which is not part of any list.refCell
- The list cell used to reference the position.public final void insertCellBefore(ListCell cellToInsert, ListCell refCell)
The ListCell object is inserted right before the reference list cell refCell
.
Attention: If the ListCell object is still part of any list, then that list will be corrupted afterwards.
refCell
must be part of this list.cellToInsert
- A list cell which is not part of any list.refCell
- The list cell used to reference the position.public final boolean isEmpty()
isEmpty
in interface ICollection<Object>
isEmpty
in interface Collection<Object>
isEmpty
in interface List<Object>
public final Object last()
!isEmpty()
.public final ListCell lastCell()
!isEmpty()
.public final int lastIndexOf(Object o)
lastIndexOf
in interface List<Object>
public final ListIterator listIterator()
listIterator
in interface List<Object>
public Stream<Object> parallelStream()
ICollection
Stream
using the elements of this enumerable.parallelStream
in interface ICollection<Object>
parallelStream
in interface IEnumerable<Object>
parallelStream
in interface Collection<Object>
Stream
using the elements of this enumerable.public final Object pop()
public final Object popLast()
public final ListCell predCell(ListCell c)
public final ListCell push(Object o)
addFirst(Object)
.public final boolean remove(Object o)
Only the first element for which equality to o
holds gets removed.
remove
in interface ICollection<Object>
remove
in interface Collection<Object>
remove
in interface List<Object>
public final boolean removeAll(Collection<? extends Object> collection)
removeAll
in interface ICollection<Object>
removeAll
in interface Collection<Object>
removeAll
in interface List<Object>
public final Object removeAt(ICursor c)
cursor()
method and the element pointed to by it is
contained in this list.public final Object removeCell(ListCell c)
public final boolean retainAll(Collection<? extends Object> collection)
retainAll
in interface ICollection<Object>
retainAll
in interface Collection<Object>
retainAll
in interface List<Object>
public final void reverse()
public final void setInfo(ListCell c, Object value)
public final int size()
public final void sort()
All elements must implement the Comparable
interface. Furthermore, all elements in this list must be
mutually comparable (that is, e1.compareTo(e2)
must not throw a ClassCastException for any elements e1
and e2
in this list).
NOTE: The elements will be assigned to different list cells by this method.
public final void sort(Comparator<Object> comp)
NOTE: The elements will be assigned to different list cells by this method.
public final void splice(YList list)
The given list will be empty after this operation.
Note that this operation transfers the list cells of the given list to this list. No new list cells are created by this operation.
public Stream<Object> stream()
ICollection
Stream
using the elements of this enumerable.stream
in interface ICollection<Object>
stream
in interface IEnumerable<Object>
stream
in interface Collection<Object>
Stream
using the elements of this enumerable.public final ListCell succCell(ListCell c)
public final Object[] toArray()
toArray
in interface ICollection<Object>
toArray
in interface Collection<Object>
toArray
in interface List<Object>
public Object[] toArray(Object[] a)
The runtime type of the returned array is that of the given array.
If the list does not fit in the specified array, a new array is allocated with the runtime type of the specified array and the size of this list.
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the
element in the array immediately following the end of the collection is set to null
. This is useful in
determining the length of the list only if the caller knows that the list does not contain any null
elements.
toArray
in interface ICollection<Object>
toArray
in interface Collection<Object>
toArray
in interface List<Object>
a
- The array into which the elements of the list are to be stored, if it is big enough. Otherwise, a new array of the same
runtime type is allocated for this purpose.