org.openbp.common.util
Class WeakArrayList

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList
          extended by org.openbp.common.util.WeakArrayList
All Implemented Interfaces:
java.lang.Iterable, java.util.Collection, java.util.List

public class WeakArrayList
extends java.util.AbstractList

A weak list works pretty identical to the java.util.ArrayList class except that it usually refers its elements using a weak reference. Weak references enable the garbage collector to remove objects though they are still referenced through the weak ref. In this case, the reference returns null (i. e. through the get(int) method).
Note that the list does not change its size though the references may be garbage-collected. However, if you invoke the trim() method, 'dead' references are removed. Though the implementation of the java.util.List methods always add the elements as weak references, it is also possible to insert elements as hard references using the addHardReference(Object) method, allowing to mix weak and hard references in a single list. This is e. g. used by the ListenerSupport class, which stores the listeners as weak references, but the listener classes as regular references in order to minimize object allocation. Note the the Iterator returned by the AbstractList.iterator() method always returns elements that are validly referenced by the list. The references in the list can be copied to a list that contains regular (hard) references using the createHardList() method.

Author:
Heiko Erhardt

Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
WeakArrayList()
          Default constructor.
WeakArrayList(int initialCapacity)
          Default constructor.
 
Method Summary
 void add(int index, java.lang.Object element)
          Inserts the object at the given position in the list as a weak reference.
 void addHardReference(int index, java.lang.Object element)
          Inserts the object at the given position in the list as a regular (hard) reference.
 void addHardReference(java.lang.Object element)
          Adds the object at the end of the list as a regular (hard) reference.
 java.util.List createHardList()
          Creates a list of hard references to the objects.
 java.lang.Object get(int index)
          Gets the element at the given index.
 java.lang.Object remove(int index)
          Removes the object at the given index.
 java.lang.Object set(int index, java.lang.Object element)
          Sets the object at the given index as a weak reference.
 int size()
          Gets the size of the list.
 void trim()
          Trims the list by removing 'dead' references, i\. e\. collected elements.
 
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, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Constructor Detail

WeakArrayList

public WeakArrayList()
Default constructor.


WeakArrayList

public WeakArrayList(int initialCapacity)
Default constructor.

Parameters:
initialCapacity - Initial capacity of the list
Method Detail

get

public java.lang.Object get(int index)
Gets the element at the given index.

Specified by:
get in interface java.util.List
Specified by:
get in class java.util.AbstractList
Parameters:
index - Index into the list
Returns:
The element at the given position or null if the element has been garbage-collected.

size

public int size()
Gets the size of the list.

Specified by:
size in interface java.util.Collection
Specified by:
size in interface java.util.List
Specified by:
size in class java.util.AbstractCollection
Returns:
The size includes already collected objects

set

public java.lang.Object set(int index,
                            java.lang.Object element)
Sets the object at the given index as a weak reference.

Specified by:
set in interface java.util.List
Overrides:
set in class java.util.AbstractList
Parameters:
index - List index
element - Element to set
Returns:
The previous object at the given index or null if the object was already collected

add

public void add(int index,
                java.lang.Object element)
Inserts the object at the given position in the list as a weak reference. Automatically creates a weak reference to the object.

Specified by:
add in interface java.util.List
Overrides:
add in class java.util.AbstractList
Parameters:
index - List index
element - Element to add

remove

public java.lang.Object remove(int index)
Removes the object at the given index.

Specified by:
remove in interface java.util.List
Overrides:
remove in class java.util.AbstractList
Parameters:
index - The index of the element to be removed
Returns:
The removed element or null if it was already collected

addHardReference

public void addHardReference(java.lang.Object element)
Adds the object at the end of the list as a regular (hard) reference.

Parameters:
element - Element to add

addHardReference

public void addHardReference(int index,
                             java.lang.Object element)
Inserts the object at the given position in the list as a regular (hard) reference.

Parameters:
index - List index
element - Element to add

createHardList

public java.util.List createHardList()
Creates a list of hard references to the objects.

Returns:
A new list containing only the valid references of this list

trim

public void trim()
Trims the list by removing 'dead' references, i\. e\. collected elements.



Copyright © 2011. All Rights Reserved.