001: package com.quadcap.util.collections;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042:
043: import com.quadcap.util.DListItem;
044:
045: /**
046: * The construction of these objects needs to be managed by the
047: * cache object. They are initialized using the anonymous 'store'
048: * object; it is the responsibility of the derived class to cast
049: * the anonymous store to the correct type.
050: *
051: * @author Stan Bailes
052: */
053: public abstract class Cacheable extends Object {
054: /** has object been modified while in the cache? */
055: boolean dirty = false;
056:
057: /**
058: * We reference-count the cache items to know when it's ok to flush
059: * older items to make room for new ones. We should probably keep
060: * some statistics which keep track of the percentage of cache items
061: * currently "in-use"; i.e., with <code>refCount > 0</code>
062: */
063: int refCount = 0;
064:
065: /** Back pointer to my place in the LRU list. */
066: DListItem me;
067:
068: /**
069: * The key used to locate this item in the underlying store, as well
070: * as in the cache itself.
071: */
072: protected Object key;
073:
074: protected Object store;
075:
076: /**
077: * Initialization and (recycling)
078: */
079: public void init(Object store, Object key) throws IOException {
080: this .store = store;
081: this .key = key;
082: dirty = false;
083: }
084:
085: /**
086: * Read the dirty bit.
087: */
088: public boolean isDirty() {
089: return dirty;
090: }
091:
092: /**
093: * Set the dirty bit.
094: */
095: public void setDirty(boolean d) {
096: dirty = d;
097: }
098:
099: /**
100: * Read the reference count
101: */
102: public int getRefCount() {
103: return refCount;
104: }
105:
106: /**
107: * Set the reference count
108: */
109: public synchronized void setRefCount(int x) {
110: refCount = x;
111: }
112:
113: /**
114: * Increment the reference count
115: */
116: public synchronized void incrRefCount() {
117: refCount++;
118: }
119:
120: /**
121: * Decrement the reference count
122: */
123: public synchronized void decrRefCount() {
124: refCount--;
125: }
126:
127: /**
128: * Set the LRU back pointer.
129: */
130: public void setDListItem(DListItem d) {
131: me = d;
132: }
133:
134: /**
135: * Get the LRU back pointer.
136: */
137: public DListItem getDListItem() {
138: return me;
139: }
140:
141: /**
142: * Get the cache item's key.
143: */
144: public Object getKey() {
145: return key;
146: }
147:
148: /**
149: * Set the cache item's key.
150: */
151: public void setKey(Object key) {
152: this .key = key;
153: }
154:
155: /**
156: * Get the cache item's data.
157: */
158: abstract public Object getData();
159:
160: /**
161: * Set the cache item's data.
162: */
163: abstract public void setData(Object data);
164:
165: /**
166: * Flush this item and clear the dirty bit.
167: */
168: public void flush() throws IOException {
169: dirty = false;
170: }
171: }
|