org.jfree.report.util
abstract public class: WeakReferenceList [javadoc |
source]
java.lang.Object
org.jfree.report.util.WeakReferenceList
All Implemented Interfaces:
Cloneable, Serializable
Direct Known Subclasses:
MasterList, MasterList
The WeakReference list uses
java.lang.ref.WeakReferences to store its contents. In contrast to the
WeakHashtable, this list knows how to restore missing content, so that garbage collected elements can be restored
when they are accessed.
By default this list can contain 25 elements, where the first element is stored using a strong reference, which is
not garbage collected.
Restoring the elements is not implemented, concrete implementations will have to override the
restoreChild(int) method. The
getMaxChildCount method defines the maxmimum number of
children in the list. When more than
maxChildCount elements are contained in this list, add will always
return false to indicate that adding the element failed.
To customize the list, override createReference to create a different kind of reference.
This list is able to add or replace elements, but inserting or removing of elements is not possible.
| Method from org.jfree.report.util.WeakReferenceList Detail: |
public boolean add(Object rs) {
if (size == 0)
{
master = rs;
size = 1;
return true;
}
else
{
if (size < getMaxChildCount())
{
childs[size - 1] = createReference(rs);
size++;
return true;
}
else
{
// was not able to add this to this list, maximum number of entries reached.
return false;
}
}
}
Adds the element to the list. If the maximum size of the list is exceeded, this function returns false to indicate
that adding failed. |
protected Object clone() throws CloneNotSupportedException {
final WeakReferenceList list = (WeakReferenceList) super.clone();
list.childs = (Reference[]) childs.clone();
return list;
}
|
public Object get(int index) {
if (isMaster(index))
{
return master;
}
else
{
final Reference ref = childs[getChildPos(index)];
if (ref == null)
{
throw new IllegalStateException("State: " + index);
}
Object ob = ref.get();
if (ob == null)
{
ob = restoreChild(index);
childs[getChildPos(index)] = createReference(ob);
}
return ob;
}
}
Returns the child stored at the given index. If the child has been garbage collected, it gets restored using the
restoreChild function. |
protected int getChildPos(int index) {
return index % getMaxChildCount() - 1;
}
Returns the internal storage position for the child. |
protected Object getMaster() {
return master;
}
Returns the master element of this list. The master element is the element stored by a strong reference and cannot
be garbage collected. |
protected final int getMaxChildCount() {
return maxChilds;
}
Returns the maximum number of children in this list. |
public int getSize() {
return size;
}
Returns the size of the list. |
protected boolean isMaster(int index) {
return index % getMaxChildCount() == 0;
}
Returns true, if the given index denotes a master index of this list. |
abstract protected Object restoreChild(int index)
Attempts to restore the child stored on the given index. |
public void set(Object report,
int index) {
if (isMaster(index))
{
master = report;
}
else
{
childs[getChildPos(index)] = createReference(report);
}
}
Replaces the child stored at the given index with the new child which can be null. |