001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of jCommonTk.
006: *
007: * jCommonTk is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * jCommonTk is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with jCommonTk; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jcommontk.utils;
020:
021: import java.util.Enumeration;
022: import java.util.Hashtable;
023:
024: @SuppressWarnings("unchecked")
025: // working to complete a Java 1.5 version
026: public class ExpiringHashtable {
027: Hashtable hash = new Hashtable();
028: long expiration_time = 0;
029: boolean stop_thread;
030:
031: public ExpiringHashtable(long expiration_time) {
032: this .expiration_time = expiration_time;
033:
034: if (expiration_time > 0)
035: new TimedCleanup().start();
036: }
037:
038: public Object get(Object key) {
039: TimedHashObject t_obj = (TimedHashObject) hash.get(key);
040:
041: if (t_obj != null)
042: return t_obj.getObject();
043:
044: return null;
045: }
046:
047: public boolean isEmpty() {
048: return hash.isEmpty();
049: }
050:
051: public Enumeration keys() {
052: return hash.keys();
053: }
054:
055: public Object put(Object key, Object obj) {
056: TimedHashObject t_obj = (TimedHashObject) hash.put(key,
057: new TimedHashObject(obj));
058:
059: if (t_obj != null)
060: return t_obj.getObject();
061:
062: return null;
063: }
064:
065: public boolean containsKey(Object key) {
066: return hash.containsKey(key);
067: }
068:
069: public Object remove(Object key) {
070: TimedHashObject t_obj = (TimedHashObject) hash.remove(key);
071:
072: if (t_obj != null)
073: return t_obj.getObject();
074:
075: return null;
076: }
077:
078: public int size() {
079: return hash.size();
080: }
081:
082: public Enumeration elements() {
083: return new Enumeration() {
084: Enumeration e = hash.elements();
085:
086: public boolean hasMoreElements() {
087: return e.hasMoreElements();
088: }
089:
090: public Object nextElement() {
091: return ((TimedHashObject) e.nextElement()).getObject();
092: }
093: };
094: }
095:
096: protected void finalize() {
097: stop_thread = true;
098: }
099:
100: class TimedCleanup extends Thread {
101: public void run() {
102: while (!stop_thread) {
103: try {
104: sleep(5000);
105: } catch (InterruptedException ex) {
106: } //don't care
107:
108: Enumeration e = keys();
109:
110: while (e.hasMoreElements()) {
111: Object key = e.nextElement();
112: TimedHashObject t_obj = (TimedHashObject) hash
113: .get(key);
114:
115: if (System.currentTimeMillis() - t_obj.getTime() > expiration_time)
116: remove(key);
117: }
118: }
119: }
120: }
121:
122: static class TimedHashObject {
123: long time = System.currentTimeMillis();
124: Object obj;
125:
126: TimedHashObject(Object obj) {
127: this .obj = obj;
128: }
129:
130: long getTime() {
131: return time;
132: }
133:
134: Object getObject() {
135: time = System.currentTimeMillis();
136:
137: return obj;
138: }
139: }
140: }
|