1
6
7 import java.lang.ref.*;
8
9 public class TransparentWeakReference extends WeakReference implements Comparable {
10
11 int hash;
12
13 public TransparentWeakReference(Object target) {
14 super(target);
15 hash = target.hashCode();
16 }
17
18 public int hashCode() {
19 return(hash);
20 }
21
22 public boolean equals(Object that) {
23 if (hash!=that.hashCode()) return(false);
24 return(compareTo(that)==0);
25 }
26
27 public int compareTo(Object that) {
28 if (that==this) return(0);
29 if (that==null) return(-1);
30 Object target = get();
31 if (that instanceof WeakReference)
32 that = ((WeakReference)that).get();
33 if (target==that) return(0);
34 if (target==null) return(1);
35 if (target instanceof Comparable)
36 return(((Comparable)target).compareTo(that));
37 return(-1);
38 }
39 }
40