KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreestorage > CacheReference


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.persistence.btreeimpl.btreestorage;
20
21 import java.lang.ref.*;
22
23 import org.netbeans.mdr.persistence.*;
24
25 /** A member of the MDR cache. This is a soft reference to an object
26 * identified by a MOFID.
27 */

28 //class CacheReference extends SoftReference {
29
// BHM - lets try more memory friendly
30
class CacheReference extends WeakReference {
31
32     /* The MOF ID of the object we reference */
33     private Object JavaDoc key;
34
35     private Object JavaDoc hardRef;
36
37     /* Whether the reference is currently in the cache */
38     // boolean inCache;
39

40     /** Create a reference
41     * @param m the MOF ID of the referenced object
42     * @param o the referenced object
43     * @param q our reference queue
44     */

45     CacheReference(Object JavaDoc m, Object JavaDoc o, ReferenceQueue q) {
46         super(o, q);
47         key = m;
48     }
49
50     /** A CacheReference is represented by the same string as its MOF ID */
51     public String JavaDoc toString() {
52         return key.toString();
53     }
54
55     /** A CacheReference hashes to the same number as its MOF ID */
56     public int hashCode() {
57         return key.hashCode();
58     }
59
60     /** Two CacheReferences are equal if their MOF IDs are equal */
61     public boolean equals(Object JavaDoc o) {
62         if (!(o instanceof CacheReference))
63             return false;
64
65         CacheReference cr = (CacheReference)o;
66         return cr.key.equals(key);
67     }
68     
69     /** Calling this method makes the reference to be a hard reference
70      */

71     public void harden() {
72         hardRef = get();
73     }
74     
75     /** Calling this method makes the reference weak again
76      */

77     public void weaken() {
78         hardRef = null;
79     }
80     
81     /** Returns true if the reference is hard
82      */

83     public boolean isHard() {
84         return hardRef != null;
85     }
86     
87     
88     /** Overriden clear to weaken possibly hardened reference
89      */

90     public void clear() {
91         weaken();
92         key = null;
93         super.clear();
94     }
95     
96     
97     /** Returns the key of the referece */
98     Object JavaDoc getKey() {
99         return key;
100     }
101     
102 }
103
Popular Tags