Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.meta.reflect
Class WeakReferenceEntry<T>

java.lang.Object
  extended by dk.rode.thesis.meta.reflect.WeakReferenceEntry<T>
Type Parameters:
T - The type of object stored as a weak reference by this entry.

public class WeakReferenceEntry<T>
extends Object

A weak reference entry represents an object stored as a weak reference, but where the hashCode() and equals(Object) methods of the object are used over the identity comparison performed by java.lang.ref.WeakReference.

Since stored as a weak reference, the wrapped object may be garbage collected at any time.

Implementation notes:
Delegation is favoured over inheritance of java.lang.ref.WeakReference.

Author:
Gunni Rode / rode.dk

Field Summary
private  int hashCode
          The hash code of the object stored as a weak reference from the time of creation of this entry.
private  WeakReference<T> reference
          The weak reference storing the object.
 
Constructor Summary
WeakReferenceEntry(T object)
          Constructor.
 
Method Summary
 boolean equals(Object object)
          Returns true if object represents the same object as this entry, false if not.
 T get()
          Returns the object stored as a weak reference, or null if garbage collected.
 int hashCode()
          Returns the hash code of this entry.
 String toString()
          Returns the string representation of this entry.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

hashCode

private final int hashCode
The hash code of the object stored as a weak reference from the time of creation of this entry.


reference

private final WeakReference<T> reference
The weak reference storing the object.

Never null, but the actual object may be if it was garbage collected.

Constructor Detail

WeakReferenceEntry

public WeakReferenceEntry(T object)
Constructor.

Parameters:
object - The object to store as weak reference; cannot be null.
Throws:
NullPointerException - If object is null.
Method Detail

equals

public boolean equals(Object object)
Returns true if object represents the same object as this entry, false if not.

The test for object equality is performed using equals.

Overrides:
equals in class Object
Parameters:
object - The object to test; can be null.
Returns:
True if equal, false if not.

get

public T get()
Returns the object stored as a weak reference, or null if garbage collected.

Returns:
The object; or null if garbage collected.

hashCode

public int hashCode()
Returns the hash code of this entry.

The hash code corresponds to the hash code of the object stored as a weak reference at the time of creation of this entry.

Overrides:
hashCode in class Object
Returns:
The hash code.

toString

public String toString()
Returns the string representation of this entry.

Overrides:
toString in class Object
Returns:
The string representation; never null.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.