001: /**
002: * $Id: TimeBoundHashMap.java,v 1.4 2003/06/03 10:45:56 sy131129 Exp $
003: * Copyright 2002-2003 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.providers.simplewebservice.util;
014:
015: import java.util.HashMap;
016: import java.util.Map;
017: import java.util.Set;
018: import java.util.Iterator;
019:
020: /**
021: * TimeBoundHashMap is a HashMap sub class that purges entries using
022: * a last accessed time mechanism.
023: * <P>
024: * The time out control is done when using the accessor methods of the Map,
025: * no extra Thread is used for it.
026: * <P>
027: * This class is not Thread-safe, if a TimeBoundHashMap instance is to be
028: * used in a multithreaded fashion it should be wrapped with a synchronized
029: * Map (see the Collections.synchronizedMap() method).
030: * <P>
031: *
032: *
033: */
034: public class TimeBoundHashMap extends HashMap {
035: private long _timeOut;
036: private long _lastCheck;
037: private Map _lastAccess;
038:
039: /**
040: * Creates an empty TimeBoundHashMap with a given last accessed time out.
041: * <P>
042: *
043: * @param timeOut last accessed time out in seconds.
044: *
045: */
046: public TimeBoundHashMap(int timeOut) {
047: super ();
048: init(timeOut);
049: }
050:
051: /**
052: * Creates an empty TimeBoundHashMap with a specific initial capacity
053: * and a given last accessed time out.
054: * <P>
055: *
056: * @param initialCapacity initial capacity of the Map.
057: *
058: * @param timeOut last accessed time out in seconds.
059: *
060: */
061: public TimeBoundHashMap(int initialCapacity, int timeOut) {
062: super (initialCapacity);
063: init(timeOut);
064: }
065:
066: /**
067: * Creates an empty TimeBoundHashMap with a specific initial capacity,
068: * a given load factor and a given last accessed time out.
069: * <P>
070: *
071: * @param initialCapacity initial capacity of the Map.
072: *
073: * @param loadFactor load factor for the Map.
074: *
075: * @param timeOut last accessed time out in seconds.
076: *
077: */
078: public TimeBoundHashMap(int initialCapacity, float loadFactor,
079: int timeOut) {
080: super (initialCapacity, loadFactor);
081: init(timeOut);
082: }
083:
084: /**
085: * Adds time out control and delegates to HashMap.put() method.
086: *
087: */
088: public Object put(Object key, Object value) {
089: long currentTime = System.currentTimeMillis();
090: _lastAccess.put(key, new Long(currentTime));
091: purge();
092: return super .put(key, value);
093: }
094:
095: /**
096: * Adds time out control and delegates to HashMap.get() method.
097: *
098: */
099: public Object get(Object key) {
100: Object obj = super .get(key);
101: if (obj != null) {
102: long currentTime = System.currentTimeMillis();
103: _lastAccess.put(key, new Long(currentTime));
104: }
105: purge();
106: return obj;
107: }
108:
109: /**
110: * Adds time out control and delegates to HashMap.remove() method.
111: *
112: */
113: public Object remove(Object key) {
114: _lastAccess.remove(key);
115: return super .remove(key);
116: }
117:
118: /**
119: * Adds time out control and delegates to HashMap.entrySet() method.
120: *
121: */
122: public Set entrySet() {
123: Set set = super .entrySet();
124: Iterator i = _lastAccess.keySet().iterator();
125: Long currentTime = new Long(System.currentTimeMillis());
126: while (i.hasNext()) {
127: _lastAccess.put(i.next(), currentTime);
128: }
129: return set;
130: }
131:
132: /**
133: * Initialized the instance attributes, called from the constructors.
134: *
135: * @param timeOut last accessed time out in seconds.
136: *
137: */
138: private void init(int timeOut) {
139: _timeOut = timeOut * 1000;
140: _lastCheck = System.currentTimeMillis();
141: _lastAccess = new HashMap();
142: }
143:
144: /**
145: * Resets the cache time out.
146: * ONLY to be called before the put and get are called.
147: *
148: * @param timeOut last accessed time out in seconds.
149: *
150: */
151: public void setTimeOut(int timeOut) {
152: _timeOut = timeOut * 1000;
153: }
154:
155: /**
156: * Gets the cache time out.
157: *
158: */
159: public int getTimeOut() {
160: return (int) _timeOut;
161: }
162:
163: /**
164: * Removes entries that have expired.
165: *
166: */
167: private void purge() {
168: long currentTime = System.currentTimeMillis();
169: if ((currentTime - _lastCheck) > (_timeOut / 8)) {
170: Iterator i = _lastAccess.keySet().iterator();
171: while (i.hasNext()) {
172: Object key = i.next();
173: long lastTime = ((Long) _lastAccess.get(key))
174: .longValue();
175: if ((currentTime - lastTime) > _timeOut) {
176: i.remove();
177: super.remove(key);
178: }
179: }
180: _lastCheck = currentTime;
181: }
182: }
183:
184: }
|