001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: NodeCache.java$
021: * $FileID: 4315$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 4/23/03 5:24 PM$
026: * $Comment: Replaced GPL header with LGPL header.$
027: *
028: * $KeyWordsOff: $
029: */
030:
031: package org.sourcejammer.util;
032:
033: import java.util.Hashtable;
034: import org.sourcejammer.project.Node;
035: import org.sourcejammer.project.view.NodeInfo;
036:
037: /**
038: * Title:
039: * Description:
040: * Copyright: Copyright (c) 2001
041: * Company:
042: * @author
043: * @version 1.0
044: */
045:
046: public class NodeCache implements Cache {
047:
048: private CachedObjectMasterList masterList = null;
049: private Hashtable mhshCache = null;
050:
051: private NodeCache() {
052: }
053:
054: public NodeCache(CachedObjectMasterList master) {
055: masterList = master;
056: mhshCache = new Hashtable();
057: }
058:
059: public synchronized void addToCache(Node nd) {
060: Long lngID = new Long(nd.getUniqueID());
061: //Special case when empty
062: Object objExisting = mhshCache.get(lngID);
063: if (objExisting == null) {
064: //Not in cache, so let's add it.
065: CachedListLink newLink = masterList.append(nd);
066: mhshCache.put(lngID, newLink);
067: newLink.setKey(lngID);
068: newLink.setCache(this );
069: }
070: }
071:
072: /**
073: * Throws CacheException if node is not in the cache.
074: */
075: public Node retriveNodeFromCache(long uniqueID)
076: throws CacheException {
077: Long lngID = new Long(uniqueID);
078: //log request
079: masterList.getInstance().cachedObjectRequested();
080: CachedListLink link = (CachedListLink) mhshCache.get(lngID);
081: if (link == null) {
082: throw new CacheException("Node with id: " + uniqueID
083: + " is not in the cache.");
084: }
085: //log object found
086: masterList.getInstance().cachedObjectFound();
087:
088: masterList.moveToEnd(link);
089: return (Node) link.getObject();
090: }
091:
092: public synchronized Node removeNodeFromCache(long uniqueID)
093: throws CacheException {
094: Long lngID = new Long(uniqueID);
095: CachedListLink link = (CachedListLink) mhshCache.get(lngID);
096: if (link == null) {
097: throw new CacheException("Node with id: " + uniqueID
098: + " is not in the cache.");
099: }
100: masterList.remove(link);
101: return (Node) link.getObject();
102: }
103:
104: public String toString() {
105: return mhshCache.toString();
106: }
107:
108: protected Hashtable getCache() {
109: return mhshCache;
110: }
111:
112: public void remove(CachedObject o) {
113: Long lngKey = (Long) o.getKey();
114: CachedListLink check = (CachedListLink) mhshCache.get(lngKey);
115: if (o == check) {
116: mhshCache.remove(lngKey);
117: }
118: }
119:
120: public static void main(String[] args) {
121: try {
122: CachedObjectMasterList master = CachedObjectMasterList
123: .getInstance(10);
124: NodeCache cache1 = new NodeCache(master);
125: NodeCache cache2 = new NodeCache(master);
126: NodeCache cache3 = new NodeCache(master);
127:
128: NodeInfo nd = new NodeInfo();
129: nd.setUniqueID(1);
130: nd.setNodeName("one");
131: cache1.addToCache(nd);
132:
133: nd = new NodeInfo();
134: nd.setUniqueID(2);
135: nd.setNodeName("two");
136: cache1.addToCache(nd);
137:
138: nd = new NodeInfo();
139: nd.setUniqueID(3);
140: nd.setNodeName("three");
141: cache1.addToCache(nd);
142:
143: nd = new NodeInfo();
144: nd.setUniqueID(4);
145: nd.setNodeName("four");
146: cache1.addToCache(nd);
147:
148: nd = new NodeInfo();
149: nd.setUniqueID(5);
150: nd.setNodeName("five");
151: cache1.addToCache(nd);
152:
153: nd = new NodeInfo();
154: nd.setUniqueID(1);
155: nd.setNodeName("2-one");
156: cache2.addToCache(nd);
157:
158: nd = new NodeInfo();
159: nd.setUniqueID(2);
160: nd.setNodeName("2-two");
161: cache2.addToCache(nd);
162:
163: nd = new NodeInfo();
164: nd.setUniqueID(3);
165: nd.setNodeName("2-three");
166: cache2.addToCache(nd);
167:
168: nd = new NodeInfo();
169: nd.setUniqueID(4);
170: nd.setNodeName("2-four");
171: cache2.addToCache(nd);
172:
173: nd = new NodeInfo();
174: nd.setUniqueID(5);
175: nd.setNodeName("2-five");
176: cache2.addToCache(nd);
177:
178: System.out.println("getting "
179: + (cache1.retriveNodeFromCache(1)).getNodeName());
180: System.out.println("getting "
181: + (cache1.retriveNodeFromCache(2)).getNodeName());
182: System.out.println("getting "
183: + (cache2.retriveNodeFromCache(1)).getNodeName());
184: System.out.println("getting "
185: + (cache2.retriveNodeFromCache(2)).getNodeName());
186:
187: nd = new NodeInfo();
188: nd.setUniqueID(1);
189: nd.setNodeName("3-one");
190: cache3.addToCache(nd);
191:
192: nd = new NodeInfo();
193: nd.setUniqueID(2);
194: nd.setNodeName("3-two");
195: cache3.addToCache(nd);
196:
197: nd = new NodeInfo();
198: nd.setUniqueID(3);
199: nd.setNodeName("3-three");
200: cache3.addToCache(nd);
201:
202: nd = new NodeInfo();
203: nd.setUniqueID(4);
204: nd.setNodeName("3-four");
205: cache3.addToCache(nd);
206:
207: nd = new NodeInfo();
208: nd.setUniqueID(5);
209: nd.setNodeName("3-five");
210: cache3.addToCache(nd);
211:
212: System.out.println(master.toString());
213:
214: System.out.println("-- Cache 1:");
215: System.out.println(cache1.toString());
216: System.out.println("-- Cache 2:");
217: System.out.println(cache2.toString());
218: System.out.println("-- Cache 3:");
219: System.out.println(cache3.toString());
220:
221: } catch (Exception ex) {
222: ex.printStackTrace();
223: }
224: }
225: }
|