KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > util > collections > Cacheable


1 package com.quadcap.util.collections;
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 anonymous 'store'
48  * object; it is the responsibility of the derived class to cast
49  * the anonymous store to the correct type.
50  *
51  * @author Stan Bailes
52  */

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

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

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

79     public void init(Object JavaDoc store, Object JavaDoc key) throws IOException JavaDoc {
80         this.store = store;
81     this.key = key;
82     dirty = false;
83     }
84
85     /**
86      * Read the dirty bit.
87      */

88     public boolean isDirty() { return dirty; }
89
90     /**
91      * Set the dirty bit.
92      */

93     public void setDirty(boolean d) { dirty = d; }
94
95     /**
96      * Read the reference count
97      */

98     public int getRefCount() { return refCount; }
99
100     /**
101      * Set the reference count
102      */

103     public synchronized void setRefCount(int x) { refCount = x; }
104
105     /**
106      * Increment the reference count
107      */

108     public synchronized void incrRefCount() { refCount++; }
109
110     /**
111      * Decrement the reference count
112      */

113     public synchronized void decrRefCount() { refCount--; }
114
115     /**
116      * Set the LRU back pointer.
117      */

118     public void setDListItem(DListItem d) { me = d; }
119
120     /**
121      * Get the LRU back pointer.
122      */

123     public DListItem getDListItem() { return me; }
124
125     /**
126      * Get the cache item's key.
127      */

128     public Object JavaDoc getKey() { return key; }
129
130     /**
131      * Set the cache item's key.
132      */

133     public void setKey(Object JavaDoc key) { this.key = key; }
134
135     /**
136      * Get the cache item's data.
137      */

138     abstract public Object JavaDoc getData();
139
140     /**
141      * Set the cache item's data.
142      */

143     abstract public void setData(Object JavaDoc data);
144     
145     /**
146      * Flush this item and clear the dirty bit.
147      */

148     public void flush() throws IOException JavaDoc {
149         dirty = false;
150     }
151 }
152
Popular Tags