1    /*
2    Copyright 2002 by Ralph Hartley
3    This software is licenced under the terms of the
4    Gnu Public Licence
5    */
6    
7    import java.lang.ref.*;
8    
9    public class CacheRef {
10     
11   //  static CacheRef
12   
13     transient SoftReference ref = null;
14     transient private Object lock = null;
15   
16     CacheRef() {}
17   
18     public CacheRef(Object target) {
19       ref = new SoftReference(target);
20     }
21   
22     public void set(Object target) {
23       ref = new SoftReference(target);
24     }
25   
26     public Object get() {
27       if (ref==null) return(null);
28       return(ref.get());
29     }
30   
31     /**
32      * Lock or unlock the cached object.<br>
33      * lock(true) makes the CacheRef act like a hard reference.
34      * The object cannot be reclaimed.
35      * returns true iff the reference is not null. <br>
36      * lock(false) unlocks the reference so it may cleared, and
37      * always returns true.
38      */
39     public boolean lock(boolean hold) {
40       if (hold) {
41         if (ref==null) return(false);
42         lock = ref.get();
43         return(lock!=null);
44       }
45       lock = null;
46       return(true);
47     }
48   
49     public static void uses(Object user, CacheRef target) {
50     }
51   }
52