01: /*
02: * LRUCacheMap.java
03: *
04: * Copyright (c) 2004,2005 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.util;
10:
11: import java.util.*;
12: import org.pnuts.util.LRUCache;
13:
14: public abstract class LRUCacheMap extends LRUCache implements Map {
15:
16: private static Object NULL_OBJECT = new Object();
17:
18: public LRUCacheMap(int max) {
19: super (max);
20: }
21:
22: public synchronized Object get(Object key) {
23: Object k;
24: if (key == null) {
25: k = NULL_OBJECT;
26: } else {
27: k = key;
28: }
29: Object value = super .get(k);
30: if (value == null) {
31: value = construct(key);
32: super .put(k, value);
33: }
34: return value;
35: }
36:
37: /**
38: * Called when the cache entry has been expired
39: *
40: * @param key the key
41: * @return the value for the key
42: */
43: protected abstract Object construct(Object key);
44:
45: /**
46: * Throws an exception, since this operation is not supported in this class.
47: */
48: public boolean isEmpty() {
49: return size() == 0;
50: }
51:
52: /**
53: * Throws an exception, since this operation is not supported in this class.
54: */
55: public boolean containsKey(Object key) {
56: throw new UnsupportedOperationException();
57: }
58:
59: /**
60: * Throws an exception, since this operation is not supported in this class.
61: */
62: public boolean containsValue(Object value) {
63: return values().contains(value);
64: }
65:
66: /**
67: * Throws an exception, since this operation is not supported in this class.
68: */
69: public Object remove(Object key) {
70: throw new UnsupportedOperationException();
71: }
72:
73: /**
74: * Throws an exception, since this operation is not supported in this class.
75: */
76: public void putAll(Map t) {
77: for (Iterator it = t.entrySet().iterator(); it.hasNext();) {
78: Map.Entry entry = (Map.Entry) it.next();
79: put(entry.getKey(), entry.getValue());
80: }
81: }
82:
83: /**
84: * Throws an exception, since this operation is not supported in this class.
85: */
86: public void clear() {
87: reset();
88: }
89: }
|