java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
de.grogra.xl.util.ObjectList<E>
- All Implemented Interfaces:
ObjectConsumer<E>
,VoidToObjectGenerator<E>
,Serializable
,Cloneable
,Iterable<E>
,Collection<E>
,List<E>
,RandomAccess
- Direct Known Subclasses:
EventSupport.Queue
,TreeIterator
,Visitor2D
public class ObjectList<E>
extends AbstractList<E>
implements RandomAccess, Cloneable, Serializable, ObjectConsumer<E>, VoidToObjectGenerator<E>
A
ObjectList
represents a list of Object
values.
It provides list- and stack-oriented methods for insertion, addition,
and removal, of values. The methods are not thread-safe.- Author:
- Ole Kniemeyer
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionObject[]
The array holding the elements.int
The size of this list.boolean
Determines whether comparisons between objects should be based on theequals
-method or on the equality operator==
.Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
ConstructorDescriptionConstructs a newObjectList
.ObjectList
(int capacity) Constructs a newObjectList
with a given initial capacity.ObjectList
(int capacity, boolean useEquals) Constructs a newObjectList
with a given initial capacity.ObjectList
(E[] elements) Constructs a newObjectList
whose elements are a copy ofelements
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Insertso
at positionindex
to this list.boolean
Addso
as last element to this list.addAll
(ObjectList<? extends E> v) Appends all elements ofv
to this list.Appendslength
components ofv
to this list, starting at indexbegin
.boolean
addAll
(Collection<? extends E> v) Appends all elements ofv
to this list.void
Addso
as last element to this list if is not yet contained as reported bycontains(Object)
.void
addInOrder
(E o, Comparator<? super E> c) Insertso
into this ordered list, based onc
.void
addInOrder
(Comparable<? super E> o) Insertso
into this ordered list.static void
void
clear()
Removes all of the elements from this list.static void
clone()
void
Receives a value of typeT
.boolean
Returnstrue
iff this list contains the given elemento
.void
ensureCapacity
(int capacity) Ensures a capacity of the internal array of at leastcapacity
.boolean
void
evaluateObject
(ObjectConsumer<? super E> cons) This generator method yields all values tocons
.get
(int index) Returns the list element atindex
.int
hashCode()
int
Returns the index ofo
in this list.final boolean
isEmpty()
Returns if this list is empty, i.e., if its size is zero.int
Returns the last index ofo
in this list.peek
(int index) Returns the list element atindex
as seen from the top, i.e., at absolute positionsize - index
.pop()
Removes and returns the object at the top of this list.final ObjectList<E>
Pusheso
on top of this list, i.e., as last element.final ObjectList<E>
Pusheso1
ando2
on top of this list, i.e., as last elements.final ObjectList<E>
Pusheso1 ... o3
on top of this list, i.e., as last elements.remove
(int index) Removes the element at positionindex
.boolean
Removes the elemento
.removeAt
(int index) Removes the element at positionindex
.Sets the element at positionindex
too
.void
setSize
(int size) Sets the size of this list to the given value.final int
size()
Returns the size of this list.Object[]
toArray()
Returns an array containing the elements of this list.<T> T[]
toArray
(T[] array) Returns an array containing the elements of this list.void
Trims the capacity of this list to be its current size.void
values
(ObjectConsumer<? super E> cons) This method is an alias forevaluateObject(de.grogra.xl.lang.ObjectConsumer<? super E>)
.Methods inherited from class java.util.AbstractList
addAll, iterator, listIterator, listIterator, removeRange, subList
Methods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.List
containsAll, removeAll, replaceAll, retainAll, sort, spliterator
-
Field Details
-
elements
The array holding the elements. Only the components from 0 tosize - 1
are valid. Direct operation on this array has to be designed carefully to avoid inconsistencies. -
size
public int sizeThe size of this list. Direct operation on this field has to be designed carefully to avoid inconsistencies. -
useEquals
public boolean useEqualsDetermines whether comparisons between objects should be based on theequals
-method or on the equality operator==
.
-
-
Constructor Details
-
ObjectList
public ObjectList(int capacity) Constructs a newObjectList
with a given initial capacity.- Parameters:
capacity
- the initial capacity
-
ObjectList
public ObjectList()Constructs a newObjectList
. -
ObjectList
Constructs a newObjectList
whose elements are a copy ofelements
.- Parameters:
elements
- the initial elements of the list
-
ObjectList
public ObjectList(int capacity, boolean useEquals) Constructs a newObjectList
with a given initial capacity.- Parameters:
capacity
- the initial capacityuseEquals
- the value foruseEquals
-
-
Method Details
-
clone
-
ensureCapacity
public void ensureCapacity(int capacity) Ensures a capacity of the internal array of at leastcapacity
.- Parameters:
capacity
- the desired minimum capacity
-
trimToSize
public void trimToSize()Trims the capacity of this list to be its current size. -
push
Pusheso
on top of this list, i.e., as last element.- Parameters:
o
- the value to push- Returns:
- this list
-
push
Pusheso1
ando2
on top of this list, i.e., as last elements. The effect is the same as the invocationlist.push(o1).push(o2)
.- Parameters:
o1
- the first value to pusho2
- the second value to push- Returns:
- this list
-
push
Pusheso1 ... o3
on top of this list, i.e., as last elements. The effect is the same as the invocationlist.push(o1).push(o2).push(o3)
.- Parameters:
o1
- the first value to pusho2
- the second value to pusho3
- the third value to push- Returns:
- this list
-
add
Addso
as last element to this list.- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceList<E>
- Overrides:
add
in classAbstractList<E>
- Parameters:
o
- the value to add- Returns:
true
-
add
Insertso
at positionindex
to this list. Ifindex
is not less thansize
, the list is enlarged and filled withnull
-values before. -
addIfNotContained
Addso
as last element to this list if is not yet contained as reported bycontains(Object)
.- Parameters:
o
- the value to add
-
addAll
Appends all elements ofv
to this list.- Parameters:
v
- the list of elements to add- Returns:
- this list
-
addAll
Appendslength
components ofv
to this list, starting at indexbegin
.- Parameters:
v
- the array of elements to addbegin
- the array index to begin withlength
- the number of elements to add- Returns:
- this list
-
removeAt
Removes the element at positionindex
.- Parameters:
index
- the position of the element to be removed- Returns:
- the value of the removed element
-
remove
Removes the element at positionindex
. -
remove
Removes the elemento
. The last occurence ofo
in this list is removed.- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceList<E>
- Overrides:
remove
in classAbstractCollection<E>
- Parameters:
o
- the element to be removed- Returns:
true
iffo
was found and removed from the list
-
set
Sets the element at positionindex
too
. Ifindex
is not less thansize
, the list is enlarged and filled withnull
-values before. -
get
Returns the list element atindex
. Ifindex
is not less thansize
,null
is returned. -
peek
Returns the list element atindex
as seen from the top, i.e., at absolute positionsize - index
. Thus, the topmost element has index 1.- Parameters:
index
- the position as seen from the top- Returns:
- the value at that position
-
contains
Returnstrue
iff this list contains the given elemento
.- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceList<E>
- Overrides:
contains
in classAbstractCollection<E>
- Parameters:
o
- a value- Returns:
true
iffo
is contained
-
indexOf
Returns the index ofo
in this list. -
lastIndexOf
Returns the last index ofo
in this list.- Specified by:
lastIndexOf
in interfaceList<E>
- Overrides:
lastIndexOf
in classAbstractList<E>
- Parameters:
o
- a value- Returns:
- the index of
o
, or -1 ofo
is not contained
-
clear
public void clear()Removes all of the elements from this list. The list will be empty after this call returns.- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceList<E>
- Overrides:
clear
in classAbstractList<E>
-
addInOrder
Insertso
into this ordered list, based onc
. This list has to be sorted in ascending order as defined byc
.o
will then be added at a position according to this order.- Parameters:
o
- the value to addc
- the comparator which defines the order
-
addInOrder
Insertso
into this ordered list. This list has to be sorted in ascending order as defined byo
.o
will then be added at a position according to this order.- Parameters:
o
- the value to add
-
addAll
Appends all elements ofv
to this list.- Specified by:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceList<E>
- Overrides:
addAll
in classAbstractCollection<E>
- Parameters:
v
- the list of elements to add- Returns:
true
iff this list changed as a result of the invocation
-
arraycopy
-
clear
-
pop
Removes and returns the object at the top of this list.- Returns:
- the removed object from the top of this list
-
setSize
public void setSize(int size) Sets the size of this list to the given value. If the new size is greater than the old size, the new elements are initialized withnull
-values.- Parameters:
size
- the new size
-
size
public final int size()Returns the size of this list.- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceList<E>
- Specified by:
size
in classAbstractCollection<E>
- Returns:
- the size
-
isEmpty
public final boolean isEmpty()Returns if this list is empty, i.e., if its size is zero.- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceList<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
- Returns:
true
iff this list is empty
-
toArray
Returns an array containing the elements of this list.- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceList<E>
- Overrides:
toArray
in classAbstractCollection<E>
- Returns:
- an array copy of this list
-
toArray
public <T> T[] toArray(T[] array) Returns an array containing the elements of this list. The type of the returned array is that of the specifiedarray
. If this list fits in the specifiedarray
, it is returned therein. Otherwise, a new array is allocated whose length is the size of this list's size, the values of this list are copied into the new array, and this array is returned.If there is room for an additional element in the
array
, anull
-value is written behind the last copied element.- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceList<E>
- Overrides:
toArray
in classAbstractCollection<E>
- Parameters:
array
- an array to use- Returns:
- an array copy of this list
-
equals
- Specified by:
equals
in interfaceCollection<E>
- Specified by:
equals
in interfaceList<E>
- Overrides:
equals
in classAbstractList<E>
-
hashCode
public int hashCode()- Specified by:
hashCode
in interfaceCollection<E>
- Specified by:
hashCode
in interfaceList<E>
- Overrides:
hashCode
in classAbstractList<E>
-
consume
Description copied from interface:ObjectConsumer
Receives a value of typeT
.- Specified by:
consume
in interfaceObjectConsumer<E>
-
evaluateObject
This generator method yields all values tocons
.- Specified by:
evaluateObject
in interfaceVoidToObjectGenerator<E>
- Parameters:
cons
- the consumer which receives the values
-
values
This method is an alias forevaluateObject(de.grogra.xl.lang.ObjectConsumer<? super E>)
.
-