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,SequencedCollection<E>
- 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
FieldsModifier and TypeFieldDescriptionObject[]The array holding the elements.intThe size of this list.booleanDetermines 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
ConstructorsConstructorDescriptionConstructs a newObjectList.ObjectList(int capacity) Constructs a newObjectListwith a given initial capacity.ObjectList(int capacity, boolean useEquals) Constructs a newObjectListwith a given initial capacity.ObjectList(E[] elements) Constructs a newObjectListwhose elements are a copy ofelements. -
Method Summary
Modifier and TypeMethodDescriptionvoidInsertsoat positionindexto this list.booleanAddsoas last element to this list.addAll(ObjectList<? extends E> v) Appends all elements ofvto this list.Appendslengthcomponents ofvto this list, starting at indexbegin.booleanaddAll(Collection<? extends E> v) Appends all elements ofvto this list.voidAddsoas last element to this list if is not yet contained as reported bycontains(Object).voidaddInOrder(E o, Comparator<? super E> c) Insertsointo this ordered list, based onc.voidaddInOrder(Comparable<? super E> o) Insertsointo this ordered list.static voidvoidclear()Removes all of the elements from this list.static voidclone()voidReceives a value of typeT.booleanReturnstrueiff this list contains the given elemento.voidensureCapacity(int capacity) Ensures a capacity of the internal array of at leastcapacity.booleanvoidevaluateObject(ObjectConsumer<? super E> cons) This generator method yields all values tocons.get(int index) Returns the list element atindex.inthashCode()intReturns the index ofoin this list.final booleanisEmpty()Returns if this list is empty, i.e., if its size is zero.intReturns the last index ofoin this list.peek(int index) Returns the list element atindexas 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> Pushesoon top of this list, i.e., as last element.final ObjectList<E> Pusheso1ando2on top of this list, i.e., as last elements.final ObjectList<E> Pusheso1 ... o3on top of this list, i.e., as last elements.remove(int index) Removes the element at positionindex.booleanRemoves the elemento.removeAt(int index) Removes the element at positionindex.Sets the element at positionindextoo.voidsetSize(int size) Sets the size of this list to the given value.final intsize()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.voidTrims the capacity of this list to be its current size.voidvalues(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, subListMethods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll, toStringMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
addFirst, addLast, containsAll, getFirst, getLast, removeAll, removeFirst, removeLast, replaceAll, retainAll, reversed, sort, spliterator
-
Field Details
-
elements
The array holding the elements. Only the components from 0 tosize - 1are 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 newObjectListwith a given initial capacity.- Parameters:
capacity- the initial capacity
-
ObjectList
public ObjectList()Constructs a newObjectList. -
ObjectList
Constructs a newObjectListwhose elements are a copy ofelements.- Parameters:
elements- the initial elements of the list
-
ObjectList
public ObjectList(int capacity, boolean useEquals) Constructs a newObjectListwith 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
Pushesoon top of this list, i.e., as last element.- Parameters:
o- the value to push- Returns:
- this list
-
push
Pusheso1ando2on 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 ... o3on 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
Addsoas last element to this list.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Parameters:
o- the value to add- Returns:
true
-
add
Insertsoat positionindexto this list. Ifindexis not less thansize, the list is enlarged and filled withnull-values before. -
addIfNotContained
Addsoas last element to this list if is not yet contained as reported bycontains(Object).- Parameters:
o- the value to add
-
addAll
Appends all elements ofvto this list.- Parameters:
v- the list of elements to add- Returns:
- this list
-
addAll
Appendslengthcomponents ofvto 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 ofoin this list is removed.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
o- the element to be removed- Returns:
trueiffowas found and removed from the list
-
set
Sets the element at positionindextoo. Ifindexis not less thansize, the list is enlarged and filled withnull-values before. -
get
Returns the list element atindex. Ifindexis not less thansize,nullis returned. -
peek
Returns the list element atindexas 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
Returnstrueiff this list contains the given elemento.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceList<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
o- a value- Returns:
trueiffois contained
-
indexOf
Returns the index ofoin this list. -
lastIndexOf
Returns the last index ofoin this list.- Specified by:
lastIndexOfin interfaceList<E>- Overrides:
lastIndexOfin classAbstractList<E>- Parameters:
o- a value- Returns:
- the index of
o, or -1 ofois 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:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>
-
addInOrder
Insertsointo this ordered list, based onc. This list has to be sorted in ascending order as defined byc.owill then be added at a position according to this order.- Parameters:
o- the value to addc- the comparator which defines the order
-
addInOrder
Insertsointo this ordered list. This list has to be sorted in ascending order as defined byo.owill then be added at a position according to this order.- Parameters:
o- the value to add
-
addAll
Appends all elements ofvto this list.- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractCollection<E>- Parameters:
v- the list of elements to add- Returns:
trueiff 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:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein 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:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceList<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
trueiff this list is empty
-
toArray
Returns an array containing the elements of this list.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin 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:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractCollection<E>- Parameters:
array- an array to use- Returns:
- an array copy of this list
-
equals
- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceList<E>- Overrides:
equalsin classAbstractList<E>
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceList<E>- Overrides:
hashCodein classAbstractList<E>
-
consume
Description copied from interface:ObjectConsumerReceives a value of typeT.- Specified by:
consumein interfaceObjectConsumer<E>
-
evaluateObject
This generator method yields all values tocons.- Specified by:
evaluateObjectin interfaceVoidToObjectGenerator<E>- Parameters:
cons- the consumer which receives the values
-
values
This method is an alias forevaluateObject(de.grogra.xl.lang.ObjectConsumer<? super E>).
-