ExpiringHashtable.java :  » Web-Framework » jWebApp » jcommontk » utils » Java Open Source

Java Open Source » Web Framework » jWebApp 
jWebApp » jcommontk » utils » ExpiringHashtable.java
/**
 * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.  
 * All Rights Reserved.
 *
 * This file is part of jCommonTk.
 *
 * jCommonTk is free software; you can redistribute it and/or modify it under 
 * the terms of the GNU General Public License (Version 2) as published by 
 * the Free Software Foundation.
 *
 * jCommonTk is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with jCommonTk; if not, write to the Free Software Foundation, 
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

package jcommontk.utils;

import java.util.Enumeration;
import java.util.Hashtable;

@SuppressWarnings("unchecked") // working to complete a Java 1.5 version
public class ExpiringHashtable 
  {
    Hashtable hash = new Hashtable();
    long expiration_time = 0;
    boolean stop_thread;
    
    public ExpiringHashtable(long expiration_time)
      {
        this.expiration_time = expiration_time;
        
        if (expiration_time > 0)
          new TimedCleanup().start();
      }
    
    public Object get(Object key)
      {
        TimedHashObject t_obj = (TimedHashObject)hash.get(key);
        
        if (t_obj != null)
          return t_obj.getObject();
        
        return null;
      }

    public boolean isEmpty() { return hash.isEmpty(); }
    
    public Enumeration keys() { return hash.keys(); }
    
    public Object put(Object key, Object obj) 
      {
        TimedHashObject t_obj = (TimedHashObject)hash.put(key, new TimedHashObject(obj)); 

        if (t_obj != null)
          return t_obj.getObject();

        return null;
      }
    
    public boolean containsKey(Object key) { return hash.containsKey(key); }
    
    public Object remove(Object key)
      {
        TimedHashObject t_obj = (TimedHashObject)hash.remove(key); 

        if (t_obj != null)
          return t_obj.getObject();

        return null;
      }
    
    public int size() { return hash.size(); }

    public Enumeration elements()
      {
        return new Enumeration() 
          {
            Enumeration e = hash.elements();

            public boolean hasMoreElements()
              {
                return e.hasMoreElements();
              }
            
            public Object nextElement()
              {
                return ((TimedHashObject)e.nextElement()).getObject();
              }
          };
      }
    
    protected void finalize()
      {
        stop_thread = true;
      }
    
    class TimedCleanup extends Thread
      {
        public void run()
          {
            while (! stop_thread)
              {
                try { sleep(5000); } catch (InterruptedException ex) { } //don't care 
                
                Enumeration e = keys();
                
                while (e.hasMoreElements())
                  {
                    Object key = e.nextElement();
                    TimedHashObject t_obj = (TimedHashObject)hash.get(key);

                    if (System.currentTimeMillis() - t_obj.getTime() > expiration_time)
                      remove(key);
                  }
              }
          }
      }
    
    static class TimedHashObject
      {
        long time = System.currentTimeMillis();
        Object obj;

        TimedHashObject(Object obj)
          {
            this.obj = obj;
          }
        
        long getTime() { return time; }
        
        Object getObject() 
          {
            time = System.currentTimeMillis();
            
            return obj; 
          }
      }
  }

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.