E
- element class.public class UnorderedCollection<E> extends AbstractSmartCollection<E> implements CapacityManagement
Iterator.remove()
method).Constructor and Description |
---|
UnorderedCollection()
Default constructor.
|
UnorderedCollection(Collection<? extends E> coll)
Constructor.
|
UnorderedCollection(int initialCapacity)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
addAll(Collection<? extends E> coll) |
<T extends E> |
addAll(T[] array)
Adds all elements from the specified array.
|
E |
choose()
Retrieves an arbitrary element from the collection.
|
ElementReference |
chooseRef()
Retrieves the reference to an arbitrary element from the collection.
|
void |
clear() |
void |
deepClear()
Thoroughly clears the collection, fixing all issues that may have been
caused by a call of the above
SmartCollection.quickClear() . |
boolean |
ensureAdditionalCapacity(int additionalSpace)
Ensures that the internal storage has room for at least
the provided number of additional elements.
|
boolean |
ensureCapacity(int minCapacity)
Ensures that the internal storage has room for at least
the provided number of elements.
|
E |
get(ElementReference ref)
Retrieves an element by its reference.
|
void |
hintNextCapacity(int nextCapacityHint)
Gives a hint regarding the capacity that should be reserved when
resizing the internal storage for the next time.
|
boolean |
isEmpty() |
Iterator<E> |
iterator() |
void |
quickClear()
Quickly clears this collection.
|
ElementReference |
referencedAdd(E elem)
Adds an element to the collection, returning a reference to the
newly added element.
|
Iterator<ElementReference> |
referenceIterator()
Retrieves an iterator for iterating over the references of elements
in this collection.
|
Iterable<ElementReference> |
references()
This is a method provided for convenience, which allows iterating
over the element references using a foreach-style
for -loop. |
void |
remove(ElementReference ref)
Removes an element (by its reference) from the collection.
|
boolean |
remove(Object elem)
This function is deprecated and should not be used, in favor of
the removal by reference
SmartCollection.remove(ElementReference) . |
void |
replace(ElementReference ref,
E newElement)
Replaces the element referenced by the given reference with
the specified element.
|
int |
size() |
void |
swap(UnorderedCollection<E> other)
Swaps the contents of this
UnorderedCollection with another one
storing the same elements. |
add, addAll, find
contains, containsAll, removeAll, retainAll, toArray, toArray, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
contains, containsAll, equals, hashCode, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray
public UnorderedCollection()
public UnorderedCollection(int initialCapacity)
initialCapacity
- the number of elements to reserve capacity
for.public UnorderedCollection(Collection<? extends E> coll)
coll
- the collection.public E get(ElementReference ref)
SmartCollection
get
in interface SmartCollection<E>
ref
- the element's reference.public ElementReference referencedAdd(E elem)
SmartCollection
referencedAdd
in interface SmartCollection<E>
elem
- the element to be added.public <T extends E> void addAll(T[] array)
SmartCollection
addAll
in interface SmartCollection<E>
addAll
in class AbstractSmartCollection<E>
T
- array element class, may be a subclass of E
.array
- the array of elements to be added.public boolean addAll(Collection<? extends E> coll)
addAll
in interface Collection<E>
addAll
in class AbstractCollection<E>
public boolean isEmpty()
isEmpty
in interface Collection<E>
isEmpty
in class AbstractCollection<E>
public Iterator<ElementReference> referenceIterator()
SmartCollection
referenceIterator
in interface SmartCollection<E>
public Iterable<ElementReference> references()
SmartCollection
for
-loop.references
in interface SmartCollection<E>
references
in class AbstractSmartCollection<E>
Iterable
with the above SmartCollection.referenceIterator()
as its iterator.public void remove(ElementReference ref)
SmartCollection
remove
in interface SmartCollection<E>
ref
- the reference to the element to be removed.public boolean remove(Object elem)
SmartCollection
SmartCollection.remove(ElementReference)
.remove
in interface Collection<E>
remove
in interface SmartCollection<E>
remove
in class AbstractSmartCollection<E>
Collection.remove(Object)
public int size()
size
in interface Collection<E>
size
in class AbstractCollection<E>
public Iterator<E> iterator()
iterator
in interface Iterable<E>
iterator
in interface Collection<E>
iterator
in class AbstractSmartCollection<E>
public void clear()
clear
in interface Collection<E>
clear
in class AbstractCollection<E>
public E choose()
SmartCollection
null
is returned. Note, however, that a
null
return value doesn't necessary mean that the
collection is empty, since it may contain null
elements.choose
in interface SmartCollection<E>
choose
in class AbstractSmartCollection<E>
null
.public ElementReference chooseRef()
SmartCollection
null
is returned. In contrast
to the above SmartCollection.choose()
, this method returns null
if and only if the collection is empty.chooseRef
in interface SmartCollection<E>
chooseRef
in class AbstractSmartCollection<E>
null
.public void replace(ElementReference ref, E newElement)
SmartCollection
replace
in interface SmartCollection<E>
ref
- the reference of the element to be replaced.newElement
- the replacement.public boolean ensureCapacity(int minCapacity)
CapacityManagement
ensureCapacity
in interface CapacityManagement
minCapacity
- the minimal number of elements the storage should
have room for.true
iff the internal storage had to be resized,
false
otherwise.public boolean ensureAdditionalCapacity(int additionalSpace)
CapacityManagement
CapacityManagement.ensureCapacity(int)
with an argument of
size() + additionalCapacity
.ensureAdditionalCapacity
in interface CapacityManagement
additionalSpace
- the number of additional elements the storage
should have room for.true
iff the internal storage had to be resized,
false
otherwise.public void hintNextCapacity(int nextCapacityHint)
CapacityManagement
CapacityManagement.ensureCapacity(int)
, i.e. it reserves the
specified capacity at the time the next resizing of the internal
storage is performed.
This method is useful when a not too unprecise upper bound on the
elements that will in consequence be added is known. Since the actual
number of elements added may be lower than the specified upper bound,
a resizing that would have been performed by
CapacityManagement.ensureCapacity(int)
might not be necessary.hintNextCapacity
in interface CapacityManagement
nextCapacityHint
- the next capacity hint.public void deepClear()
SmartCollection
SmartCollection.quickClear()
.deepClear
in interface SmartCollection<E>
deepClear
in class AbstractSmartCollection<E>
public void quickClear()
SmartCollection
Collection.clear()
. However, this could also have side-effects
like hampering the garbage collection or such.
After calling this method, even a call of the normal
Collection.clear()
is not guaranteed to fix all these issues.
This can only be achieved by the method SmartCollection.deepClear()
below.quickClear
in interface SmartCollection<E>
quickClear
in class AbstractSmartCollection<E>
public void swap(UnorderedCollection<E> other)
UnorderedCollection
with another one
storing the same elements.
This operation runs in constant time, by only swapping storage references.other
- the UnorderedCollection
to swap contents with.Copyright © 2015. All Rights Reserved.