- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<T>
-
- org.arakhne.afc.references.WeakArrayList<T>
-
- Type Parameters:
T
- is the type of the array's elements.
- All Implemented Interfaces:
Iterable<T>
,Collection<T>
,List<T>
public class WeakArrayList<T> extends AbstractList<T>
A array-based List implementation with weak keys. An entry in a WeakArrayList will automatically be removed when it is no longer in ordinary use.The behavior of the WeakArrayList class depends in part upon the actions of the garbage collector, so several familiar (though not required) List invariants do not hold for this class. Because the garbage collector may discard values at any time, a WeakArrayList may behave as though an unknown thread is silently removing entries. In particular, even if you synchronize on a WeakArrayList instance and invoke none of its mutator methods, it is possible for the size method to return smaller values over time, for the isEmpty method to return false and then true, for the contains method to return true and later false for a given value, for the get method to return a value for a given key but later return null, for the add method to return null and the remove method to return false for a value that previously appeared to be in the list.
If this map does not use a "deep expurge" of the released references, it could contains
null
values that corresponds to values that are released by the garbage collector. If a "deep expurge" is used, all the values released by the garbage collector will be removed from the map."Deep expurge" consumes much more time that "No deep expurge". This is the reason why this feature is not activated by default.
The "deep expurge" feature was added to fix the uncoherent behavior of the garbage collector which seems to not always enqueued the released values (sometimes the queue is empty even if a value was released).
- Version:
- 17.0 2020-01-04 14:41:38
- Author:
- Stéphane GALLAND
- Maven Group Id:
- org.arakhne.afc.core
- Maven Artifact Id:
- references
-
-
Field Summary
-
Fields inherited from class java.util.AbstractList
modCount
-
-
Constructor Summary
Constructors Constructor Description WeakArrayList()
Constructs an empty list with an initial capacity of ten.WeakArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.WeakArrayList(Collection<? extends T> collection)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(int index, T element)
void
addReferenceListener(ReferenceListener listener)
Add listener on reference's release.protected void
assertRange(int index, boolean allowLast)
Verify if the specified index is inside the array.void
ensureCapacity(int minCapacity)
Increases the capacity of this WeakArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.int
expurge()
Clean the references that was released.protected void
fireReferenceRelease(int released)
Fire the reference release event.T
get(int index)
T
remove(int index)
void
removeReferenceListener(ReferenceListener listener)
Remove listener on reference's release.T
set(int index, T element)
int
size()
String
toString()
void
trimToSize()
Trims the capacity of this WeakArrayList instance to be the list's current size.-
Methods inherited from class java.util.AbstractList
add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, removeRange, subList
-
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
-
Methods inherited from class java.lang.Object
clone, 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
addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
-
-
-
-
Constructor Detail
-
WeakArrayList
public WeakArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.- Parameters:
initialCapacity
- the initial capacity of the list- Throws:
IllegalArgumentException
- if the specified initial capacity is negative
-
WeakArrayList
public WeakArrayList()
Constructs an empty list with an initial capacity of ten.
-
WeakArrayList
public WeakArrayList(Collection<? extends T> collection)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.- Parameters:
collection
- the collection whose elements are to be placed into this list- Throws:
NullPointerException
- if the specified collection is null
-
-
Method Detail
-
toString
@Pure public String toString()
- Overrides:
toString
in classAbstractCollection<T>
-
ensureCapacity
public void ensureCapacity(int minCapacity)
Increases the capacity of this WeakArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.- Parameters:
minCapacity
- the desired minimum capacity
-
trimToSize
public void trimToSize()
Trims the capacity of this WeakArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an WeakArrayList instance.
-
expurge
public int expurge()
Clean the references that was released.Notifies the listeners if a reference was released.
- Returns:
- the size
-
assertRange
protected void assertRange(int index, boolean allowLast)
Verify if the specified index is inside the array.- Parameters:
index
- is the index totestallowLast
- indicates if the last elements is assumed to be valid or not.
-
size
@Pure public int size()
- Specified by:
size
in interfaceCollection<T>
- Specified by:
size
in interfaceList<T>
- Specified by:
size
in classAbstractCollection<T>
-
get
@Pure public T get(int index)
-
add
public void add(int index, T element)
-
remove
public T remove(int index)
-
addReferenceListener
public void addReferenceListener(ReferenceListener listener)
Add listener on reference's release.- Parameters:
listener
- the listener.
-
removeReferenceListener
public void removeReferenceListener(ReferenceListener listener)
Remove listener on reference's release.- Parameters:
listener
- the listener.
-
fireReferenceRelease
protected void fireReferenceRelease(int released)
Fire the reference release event.- Parameters:
released
- is the count of released objects.
-
-