KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > fjank > jcache > CacheObject


1 /* Open Source Java Caching Service
2 * Copyright (C) 2002 Frank Karlstrøm
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * The author can be contacted by email: fjankk@users.sourceforge.net
18 */

19 package org.fjank.jcache;
20
21 import java.lang.ref.ReferenceQueue JavaDoc;
22 import java.lang.ref.WeakReference JavaDoc;
23 import javax.util.jcache.Attributes;
24 import javax.util.jcache.CacheEvent;
25 import javax.util.jcache.CacheEventListener;
26
27
28 /**
29  * Overrides some of the methods for referenceobjects.
30  *
31  * @author Frank Karlstrøm
32  */

33 public class CacheObject extends WeakReference JavaDoc {
34     /** accesses and refCount is ignored.
35      * @see java.lang.Object#equals(java.lang.Object)
36      */

37     public boolean equals(Object JavaDoc arg0) {
38         if(arg0 instanceof CacheObject) {
39             CacheObject obj = (CacheObject) arg0;
40             if(!attributes.equals(obj.attributes)) return false;
41             if(!group.equals(obj.group))return false;
42             if(!key.equals(obj.key))return false;
43             if(!region.equals(obj.region))return false;
44             if(valid!=obj.valid)return false;
45             
46             return true;
47         }
48         return super.equals(arg0);
49         
50     }
51     /**
52      * the number of references to this object. (only an indication, this value
53      * could be false)
54      */

55     private int refCount;
56
57     /** indicates wether this object is valid or not. */
58     private boolean valid;
59
60     /** the name of this object */
61     private final Object JavaDoc key;
62
63     /** number of total accesses to this object. */
64     private int accesses;
65
66     /** the group this object belongs to. */
67     private final CacheGroup group;
68     /** the region this object belongs to. */
69     private final CacheRegion region;
70
71     /** the attributes for this object */
72     private AttributesImpl attributes;
73
74     /**
75      * Creates a new CacheObject object.
76      *
77      * @param key the name of this object
78      * @param referent the actuall object
79      * @param group the group this object belongs to
80      * @param q the ReferenceQueue this object goes to wneh the GC determines
81      * that this object is garbage.
82      */

83     public CacheObject(final Object JavaDoc key, final Object JavaDoc referent,
84         final CacheGroup group, final CacheRegion region, final ReferenceQueue JavaDoc q) {
85         super(((referent instanceof String JavaDoc) ? new String JavaDoc(((String JavaDoc) referent))
86                                             : referent), q);
87         this.key = key;
88         this.group = group;
89         this.region = region;
90         if(group!=null) {
91             setAttributes(group.getAttributes());
92         }
93     }
94
95     /**
96      * returns a boolean indication wether the object needs loading or not.
97      *
98      * @return a boolean indication wether the object needs loading or not.
99      */

100     public boolean needsLoading() {
101         return needsLoading(NullObject.getInstance());
102     }
103     boolean needsLoading(final Object JavaDoc arguments) {
104         if(arguments!=null && arguments !=NullObject.getInstance()) {
105             return true;
106         }
107         if ((super.get() == NullObject.getInstance())
108                 && (attributes.getLoader() != null)) {
109             return true;
110         }
111         return false;
112     }
113     /**
114      * destroys this object
115      */

116     public void destroy() {
117         this.attributes = null;
118         this.valid = false;
119     }
120
121     /**
122      * invalidates this object
123      */

124     public void invalidate() {
125         this.valid = false;
126         if ((attributes.getLoader() == null) && (group != null)) {
127             group.removeMe(this);
128         }
129         
130         // Notify subscribers
131
CacheEventListener listener = attributes.getListener();
132         if (listener != null) {
133             listener.handleEvent(new CacheEventImpl(CacheEvent.OBJECT_INVALIDATED, getKey()));
134         }
135     }
136
137     /**
138      * gets the wrapped real object.
139      *
140      * @return the wrapped real object.
141      */

142     public Object JavaDoc get() {
143         accesses++;
144         refCount++;
145         return super.get();
146     }
147
148     /**
149      * returns a string representation of this object
150      *
151      * @return a string representation of this object
152      */

153     public String JavaDoc toString() {
154         return "CacheObject {accesses:"+
155             accesses
156         +", attributes:"+
157             attributes
158         +", group:"+
159             group.getName()
160         +", key:"+
161             key
162         +", refCount:"+
163             refCount
164         +", region:"+
165             region.getName()
166         +", valid:"+
167             valid
168         +"}";
169     }
170
171     /**
172      * gets the name of this object
173      *
174      * @return the name of this object
175      */

176     public Object JavaDoc getKey() {
177         return key;
178     }
179
180     /**
181      * sets the attributes for this object
182      *
183      * @param attributes the attributes to set.
184      */

185     public void setAttributes(final AttributesImpl attributes) {
186         if (attributes != null) {
187             this.attributes = attributes;
188         }
189     }
190
191     public void setAttributes(Attributes attributes) {
192         if(attributes!=null) {
193             this.attributes=new AttributesImpl(attributes);
194         }
195     }
196
197     /**
198      * gets the attributes for this object
199      *
200      * @return the attributes for this object
201      */

202     public AttributesImpl getAttributes() {
203         return attributes;
204     }
205
206     /**
207      * gets the group this object belongs to
208      *
209      * @return the group this object belongs to
210      */

211     public CacheGroup getGroup() {
212         return group;
213     }
214
215     /**
216      * returns the number of total accesses to this cacheobject
217      *
218      * @return the number of total accesses to this cacheobject
219      */

220     public int getAccesses() {
221         return accesses;
222     }
223
224     /**
225      * returns an indication on how many object keep an reference to this
226      * object. if this number is positive, its only an indication, not a real
227      * numer. if its 0, the refcount is 0.
228      *
229      * @return an indication on how many object keep an reference to this
230      * object.
231      */

232     public int getRefCount() {
233         return refCount;
234     }
235
236     /**
237      * Resets the refcount.
238      */

239     public void resetRefCount() {
240         refCount = 0;
241     }
242
243     
244
245     /**
246      * @return
247      */

248     public CacheRegion getRegion() {
249         return region;
250     }
251
252     /* (non-Javadoc)
253      * @see java.lang.ref.Reference#clear()
254      */

255     public void clear() {
256        //s super.clear();
257
System.out.println("cleared:"+key);
258     }
259
260
261 }
262
Popular Tags