![]() |
Quadcap Embeddable Server |
00001 package com.quadcap.util.collections; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.IOException; 00042 00043 import com.quadcap.util.DListItem; 00044 00045 /** 00046 * The construction of these objects needs to be managed by the 00047 * cache object. They are initialized using the anonymous 'store' 00048 * object; it is the responsibility of the derived class to cast 00049 * the anonymous store to the correct type. 00050 * 00051 * @author Stan Bailes 00052 */ 00053 public abstract class Cacheable extends Object { 00054 /** has object been modified while in the cache? */ 00055 boolean dirty = false; 00056 00057 /** 00058 * We reference-count the cache items to know when it's ok to flush 00059 * older items to make room for new ones. We should probably keep 00060 * some statistics which keep track of the percentage of cache items 00061 * currently "in-use"; i.e., with <code>refCount > 0</code> 00062 */ 00063 int refCount = 0; 00064 00065 /** Back pointer to my place in the LRU list. */ 00066 DListItem me; 00067 00068 /** 00069 * The key used to locate this item in the underlying store, as well 00070 * as in the cache itself. 00071 */ 00072 protected Object key; 00073 00074 protected Object store; 00075 00076 /** 00077 * Initialization and (recycling) 00078 */ 00079 public void init(Object store, Object key) throws IOException { 00080 this.store = store; 00081 this.key = key; 00082 dirty = false; 00083 } 00084 00085 /** 00086 * Read the dirty bit. 00087 */ 00088 public boolean isDirty() { return dirty; } 00089 00090 /** 00091 * Set the dirty bit. 00092 */ 00093 public void setDirty(boolean d) { dirty = d; } 00094 00095 /** 00096 * Read the reference count 00097 */ 00098 public int getRefCount() { return refCount; } 00099 00100 /** 00101 * Set the reference count 00102 */ 00103 public synchronized void setRefCount(int x) { refCount = x; } 00104 00105 /** 00106 * Increment the reference count 00107 */ 00108 public synchronized void incrRefCount() { refCount++; } 00109 00110 /** 00111 * Decrement the reference count 00112 */ 00113 public synchronized void decrRefCount() { refCount--; } 00114 00115 /** 00116 * Set the LRU back pointer. 00117 */ 00118 public void setDListItem(DListItem d) { me = d; } 00119 00120 /** 00121 * Get the LRU back pointer. 00122 */ 00123 public DListItem getDListItem() { return me; } 00124 00125 /** 00126 * Get the cache item's key. 00127 */ 00128 public Object getKey() { return key; } 00129 00130 /** 00131 * Set the cache item's key. 00132 */ 00133 public void setKey(Object key) { this.key = key; } 00134 00135 /** 00136 * Get the cache item's data. 00137 */ 00138 abstract public Object getData(); 00139 00140 /** 00141 * Set the cache item's data. 00142 */ 00143 abstract public void setData(Object data); 00144 00145 /** 00146 * Flush this item and clear the dirty bit. 00147 */ 00148 public void flush() throws IOException { 00149 dirty = false; 00150 } 00151 }