org.deegree_impl.model.resources
Class WeakHashSet

java.lang.Object
  extended byjava.util.AbstractCollection
      extended byjava.util.AbstractSet
          extended byorg.deegree_impl.model.resources.WeakHashSet
All Implemented Interfaces:
java.util.Collection, java.util.Set
Direct Known Subclasses:
DatumType.Pool

public class WeakHashSet
extends java.util.AbstractSet

A set of object hold by weak references. This class is used to implements caches.

Version:
1.0
Author:
Martin Desruisseaux

Nested Class Summary
private  class WeakHashSet.WeakElement
          A weak reference to an element.
 
Field Summary
private  int count
          Number of non-nul elements in table.
private static long HOLD_TIME
          Number of millisecond to wait before to rehash the table for reducing its size.
private  long lastRehashTime
          The timestamp when table was last rehashed.
private static float LOAD_FACTOR
          Load factor.
private static int MIN_CAPACITY
          Minimal capacity for table.
private static java.lang.ref.ReferenceQueue referenceQueue
          List of reference collected by the garbage collector.
private  WeakHashSet.WeakElement[] table
          Table of weak references.
private static java.lang.Thread thread
          Background thread removing references collected by the garbage collector.
private  int threshold
          The next size value at which to resize.
 
Constructor Summary
WeakHashSet()
          Construct a WeakHashSet.
 
Method Summary
 boolean add(java.lang.Object obj)
          Adds the specified element to this set if it is not already present.
 void clear()
          Removes all of the elements from this set.
 boolean contains(java.lang.Object obj)
          Returns true if this set contains the specified element.
protected  boolean equals(java.lang.Object object1, java.lang.Object object2)
          Check two objects for equality.
 java.lang.Object get(java.lang.Object obj)
          Returns an object equals to the specified object, if present.
protected  int hashCode(java.lang.Object object)
          Returns a hash code value for the specified object.
 java.lang.Object intern(java.lang.Object object)
          Returns an object equals to obj if such an object already exist in this WeakHashSet.
 void intern(java.lang.Object[] objects)
          Iteratively call intern(Object) for an array of objects.
private  java.lang.Object intern0(java.lang.Object obj)
          Returns an object equals to obj if such an object already exist in this WeakHashSet.
 java.util.Iterator iterator()
          Returns an iterator over the elements contained in this collection.
private  void rehash(boolean augmentation)
          Rehash table.
private  void remove(WeakHashSet.WeakElement toRemove)
          Invoked by WeakHashSet.WeakElement when an element has been collected by the garbage collector.
 int size()
          Returns the count of element in this set.
 java.lang.Object[] toArray()
          Returns a view of this set as an array.
private  boolean valid()
          Check if this WeakHashSet is valid.
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, remove, retainAll, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, containsAll, isEmpty, remove, retainAll, toArray
 

Field Detail

MIN_CAPACITY

private static final int MIN_CAPACITY
Minimal capacity for table.

See Also:
Constant Field Values

LOAD_FACTOR

private static final float LOAD_FACTOR
Load factor. Control the moment where table must be rebuild.

See Also:
Constant Field Values

referenceQueue

private static final java.lang.ref.ReferenceQueue referenceQueue
List of reference collected by the garbage collector. Those elements must be removed from table.


thread

private static final java.lang.Thread thread
Background thread removing references collected by the garbage collector.


table

private WeakHashSet.WeakElement[] table
Table of weak references.


count

private int count
Number of non-nul elements in table.


threshold

private int threshold
The next size value at which to resize. This value should be table.length*#loadFactor.


lastRehashTime

private long lastRehashTime
The timestamp when table was last rehashed. This information is used to avoir too early table reduction. When the garbage collector collected a lot of elements, we will wait at least 20 seconds before rehashing table in order to avoir to many cycles "reduce", "expand", "reduce", "expand", etc.


HOLD_TIME

private static final long HOLD_TIME
Number of millisecond to wait before to rehash the table for reducing its size.

See Also:
Constant Field Values
Constructor Detail

WeakHashSet

public WeakHashSet()
Construct a WeakHashSet.

Method Detail

remove

private void remove(WeakHashSet.WeakElement toRemove)
Invoked by WeakHashSet.WeakElement when an element has been collected by the garbage collector. This method will remove the weak reference from table.


rehash

private void rehash(boolean augmentation)
Rehash table.

Parameters:
augmentation - true if this method is invoked for augmenting table, or false if it is invoked for making the table smaller.

contains

public boolean contains(java.lang.Object obj)
Returns true if this set contains the specified element.

Parameters:
obj - Object to be checked for containment in this set.
Returns:
true if this set contains the specified element.

get

public final java.lang.Object get(java.lang.Object obj)
Returns an object equals to the specified object, if present. If this set doesn't contains any object equals to obj, then this method returns null.


add

public boolean add(java.lang.Object obj)
Adds the specified element to this set if it is not already present. If this set already contains the specified element, the call leaves this set unchanged and returns false.

Parameters:
obj - Element to be added to this set.
Returns:
true if this set did not already contain the specified element.

intern0

private java.lang.Object intern0(java.lang.Object obj)
Returns an object equals to obj if such an object already exist in this WeakHashSet. Otherwise, add obj to this WeakHashSet. This method is equivalents to the following code:
    if (object!=null)
    {
        final Object current=get(object);
        if (current!=null) return current;
        else add(object);
    }
    return object;
 


intern

public final java.lang.Object intern(java.lang.Object object)
Returns an object equals to obj if such an object already exist in this WeakHashSet. Otherwise, add obj to this WeakHashSet. This method is equivalents to the following code:
    if (object!=null)
    {
        final Object current=get(object);
        if (current!=null) return current;
        else add(object);
    }
    return object;
 


intern

public final void intern(java.lang.Object[] objects)
Iteratively call intern(Object) for an array of objects. This method is equivalents to the following code:
    for (int i=0; i


size

public final int size()
Returns the count of element in this set.


clear

public final void clear()
Removes all of the elements from this set.


valid

private boolean valid()
Check if this WeakHashSet is valid. This method counts the number of elements and compare it to count. If the check fails, the number of elements is corrected (if we didn't, an // assertionError would be thrown for every operations after the first error, which make debugging more difficult). The set is otherwise unchanged, which should help to get similar behaviour as if // assertions hasn't been turned on.


toArray

public final java.lang.Object[] toArray()
Returns a view of this set as an array. Elements will be in an arbitrary order. Note that this array contains strong reference. Consequently, no object reclamation will occurs as long as a reference to this array is hold.


iterator

public java.util.Iterator iterator()
Returns an iterator over the elements contained in this collection. No element from this set will be garbage collected as long as a reference to the iterator is hold.


hashCode

protected int hashCode(java.lang.Object object)
Returns a hash code value for the specified object. Default implementation returns Object#hashCode. Override to compute hash code in a different way.


equals

protected boolean equals(java.lang.Object object1,
                         java.lang.Object object2)
Check two objects for equality. This method should be overriden if hashCode(Object) has been overriden.