001: /**********************************************************************
002: Copyright (c) 2003 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: ...
018: **********************************************************************/package org.jpox.cache;
019:
020: import java.util.Collection;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import org.jpox.util.WeakValueMap;
025:
026: /**
027: * Level 1 Cache using Weak referenced objects in a Map.
028: * <p>If the garbage collector clears the reference, the corresponding key is
029: * automatically removed from the map.
030: *
031: * @see java.lang.ref.WeakReference
032: * @version $Revision: 1.4 $
033: */
034: public class WeakRefCache implements Level1Cache {
035: private Map weakCache = new WeakValueMap();
036:
037: /**
038: * Default constructor (required)
039: */
040: public WeakRefCache() {
041: //default constructor
042: }
043:
044: public Object put(Object key, Object value) {
045: return weakCache.put(key, value);
046: }
047:
048: public Object get(Object key) {
049: return weakCache.get(key);
050: }
051:
052: public boolean containsKey(Object key) {
053: return weakCache.containsKey(key);
054: }
055:
056: public Object remove(Object key) {
057: return weakCache.remove(key);
058: }
059:
060: public void clear() {
061: weakCache.clear();
062: }
063:
064: /* (non-Javadoc)
065: * @see java.util.Map#containsValue(java.lang.Object)
066: */
067: public boolean containsValue(Object value) {
068: return weakCache.containsValue(value);
069: }
070:
071: /* (non-Javadoc)
072: * @see java.util.Map#entrySet()
073: */
074: public Set entrySet() {
075: return weakCache.entrySet();
076: }
077:
078: /* (non-Javadoc)
079: * @see java.util.Map#isEmpty()
080: */
081: public boolean isEmpty() {
082: return weakCache.isEmpty();
083: }
084:
085: /* (non-Javadoc)
086: * @see java.util.Map#keySet()
087: */
088: public Set keySet() {
089: return weakCache.keySet();
090: }
091:
092: /* (non-Javadoc)
093: * @see java.util.Map#putAll(java.util.Map)
094: */
095: public void putAll(Map t) {
096: weakCache.putAll(t);
097: }
098:
099: /* (non-Javadoc)
100: * @see java.util.Map#size()
101: */
102: public int size() {
103: return weakCache.size();
104: }
105:
106: /* (non-Javadoc)
107: * @see java.util.Map#values()
108: */
109: public Collection values() {
110: return weakCache.values();
111: }
112: }
|