KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > file > Cacheable


1 package com.quadcap.sql.file;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42
43 import com.quadcap.util.DListItem;
44
45 /**
46  * The construction of these objects needs to be managed by the
47  * cache object. They are initialized using the
48  *
49  * @author Stan Bailes
50  */

51 public abstract class Cacheable extends Object JavaDoc {
52     /** has object been modified while in the cache? */
53     boolean dirty = false;
54
55     /** Is this cache item read only? */
56     boolean readOnly = false;
57
58     /**
59      * We reference-count the cache items to know when it's ok to flush
60      * older items to make room for new ones. We should probably keep
61      * some statistics which keep track of the percentage of cache items
62      * currently "in-use"; i.e., with <code>refCount &gt; 0</code>
63      */

64     int refCount = 0;
65
66     /** Back pointer to my place in the LRU list. */
67     DListItem me;
68
69     /**
70      * The key used to locate this item in the underlying store, as well
71      * as in the cache itself.
72      */

73     protected long key;
74
75     protected Object JavaDoc store;
76
77     /**
78      * Initialization and (recycling)
79      */

80     public void init(Object JavaDoc store, long key) throws IOException JavaDoc {
81         this.store = store;
82     this.key = key;
83     dirty = false;
84     }
85
86     /**
87      * Return the cache to which this cacheable belongs.
88      */

89     public Object JavaDoc getStore() { return store; }
90
91     /**
92      * Am I read only?
93      */

94     public boolean isReadOnly() { return readOnly; }
95
96     /**
97      * Set the 'read only' flag
98      */

99     public void setReadOnly(boolean v) { this.readOnly = v; }
100
101     /**
102      * Read the dirty bit.
103      */

104     public final boolean isDirty() { return dirty; }
105
106     /**
107      * Set the dirty bit.
108      */

109     public void setDirty(boolean d) {
110     if (readOnly) {
111             throw new RuntimeException JavaDoc("Attempt to modify read-only item");
112         }
113     dirty = d;
114     }
115
116     /**
117      * Read the reference count
118      */

119     public final int getRefCount() { return refCount; }
120
121     /**
122      * Set the reference count
123      */

124     public /* synchronized */ final void setRefCount(int x) { refCount = x; }
125
126     /**
127      * Increment the reference count
128      */

129     public /* synchronized */ final void incrRefCount() {
130         refCount++;
131     }
132
133     /**
134      * Decrement the reference count
135      */

136     public /* synchronized */ final void decrRefCount() { refCount--; }
137
138     /**
139      * Set the LRU back pointer.
140      */

141     public final void setDListItem(DListItem d) { me = d; }
142
143     /**
144      * Get the LRU back pointer.
145      */

146     public final DListItem getDListItem() { return me; }
147
148     /**
149      * Get the cache item's key.
150      */

151     public long getKey() { return key; }
152
153     /**
154      * Set the cache item's key.
155      */

156     public void setKey(long key) { this.key = key; }
157
158     /**
159      * Get the cache item's data.
160      */

161     abstract public Object JavaDoc getData();
162
163     /**
164      * Set the cache item's data.
165      */

166     abstract public void setData(Object JavaDoc data);
167     
168     /**
169      * Flush this item and clear the dirty bit.
170      */

171     public void flush() throws IOException JavaDoc {
172         dirty = false;
173     }
174 }
175
Popular Tags