[Schmitzm-commits] r1548 - in trunk/schmitzm-core/src: main/java/de/schmitzm/lang test/java/de/schmitzm/lang

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Apr 5 15:45:03 CEST 2011


Author: alfonx
Date: 2011-04-05 15:45:01 +0200 (Tue, 05 Apr 2011)
New Revision: 1548

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/TimedHashmap.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/TimedLimitedHashmap.java
   trunk/schmitzm-core/src/test/java/de/schmitzm/lang/TimedHashmapTest.java
   trunk/schmitzm-core/src/test/java/de/schmitzm/lang/TimedLimitedHashmapTest.java
Log:
Added Timehashmap and TimedLimitedhashmap for peer-review by martin ;-)

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/TimedHashmap.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/TimedHashmap.java	2011-04-01 21:00:31 UTC (rev 1547)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/TimedHashmap.java	2011-04-05 13:45:01 UTC (rev 1548)
@@ -0,0 +1,82 @@
+package de.schmitzm.lang;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.HashMap;
+
+import javax.swing.Timer;
+
+/**
+ * Erweiterung der {@link LimitedHashMap}. Erlaubt es in Millisekunden
+ * anzugeben, wie lange ein Eintrag gültig ist.
+ */
+public class TimedHashmap<K, V> extends HashMap<K, V> {
+
+	private final int timeout;
+
+	HashMap<K, Long> inputTimes = new HashMap<K, Long>();
+
+	Timer cleanTask;
+
+	/**
+	 * Erweiterung der {@link LimitedHashMap}. Erlaubt es in Millisekunden
+	 * anzugeben, wie lange ein Eintrag gültig ist.
+	 * 
+	 * @param timeout
+	 *            Anz. ms die Einträge gültig sind.
+	 */
+	public TimedHashmap(int timeout) {
+		this.timeout = timeout;
+	}
+
+	@Override
+	public V put(K key, V value) {
+
+		// Timeour ms Nach dem letzten put die komplette hashmap leeren. 
+		if (cleanTask != null) {
+			cleanTask.stop();
+		}
+
+		cleanTask = new Timer(timeout + 1, new ActionListener() {
+
+			@Override
+			public void actionPerformed(ActionEvent arg0) {
+				clear();
+				inputTimes.clear();
+				cleanTask.stop();
+				cleanTask = null;
+			}
+
+		});
+		cleanTask.setInitialDelay(timeout);
+		cleanTask.setRepeats(false);
+		cleanTask.start();
+
+		inputTimes.put(key, Long.valueOf(System.currentTimeMillis()));
+		return super.put(key, value);
+	}
+
+	@Override
+	public V get(Object key) {
+		V v = super.get(key);
+		if (v != null) {
+			Long inputTime = inputTimes.get(key);
+			if ((System.currentTimeMillis() - inputTime) >= timeout) {
+				remove(key);
+				inputTimes.remove(key);
+				return null;
+			}
+		}
+		return v;
+	}
+
+	/**
+	 * Liefert das eingestelte Timeout
+	 * 
+	 * @return
+	 */
+	public int getTimeout() {
+		return timeout;
+	}
+
+}


Property changes on: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/TimedHashmap.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/TimedLimitedHashmap.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/TimedLimitedHashmap.java	2011-04-01 21:00:31 UTC (rev 1547)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/TimedLimitedHashmap.java	2011-04-05 13:45:01 UTC (rev 1548)
@@ -0,0 +1,94 @@
+package de.schmitzm.lang;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.HashMap;
+
+import javax.swing.Timer;
+
+/**
+ * Erweiterung der {@link LimitedHashMap}. Erlaubt es in Millisekunden
+ * anzugeben, wie lange ein Eintrag gültig ist.
+ */
+public class TimedLimitedHashmap<K, V> extends LimitedHashMap<K, V> {
+
+	private final int timeout;
+
+	HashMap<K, Long> inputTimes = new HashMap<K, Long>();
+
+	/**
+	 * Erweiterung der {@link LimitedHashMap}. Erlaubt es in Millisekunden
+	 * anzugeben, wie lange ein Eintrag gültig ist.
+	 */
+
+	public TimedLimitedHashmap(int timeout, int maxLoad) {
+		super(maxLoad);
+		this.timeout = timeout;
+	}
+
+	/**
+	 * Erweiterung der {@link LimitedHashMap}. Erlaubt es in Millisekunden
+	 * anzugeben, wie lange ein Eintrag gültig ist.
+	 * 
+	 * @param timeout
+	 *            Anz. ms die Einträge gültig sind.
+	 */
+	public TimedLimitedHashmap(int timeout, int maxLoad,
+			TRUNC_METHOD truncMethod) {
+		super(maxLoad, truncMethod);
+		this.timeout = timeout;
+	}
+
+	private volatile Timer cleanTask;
+
+	@Override
+	public V put(K key, V value) {
+
+		// Timeour ms Nach dem letzten put die komplette hashmap leeren.
+		if (cleanTask != null) {
+			cleanTask.stop();
+		}
+
+		cleanTask = new Timer(timeout + 1, new ActionListener() {
+
+			@Override
+			public void actionPerformed(ActionEvent arg0) {
+				clear();
+				inputTimes.clear();
+				cleanTask.stop();
+				cleanTask = null;
+			}
+
+		});
+		cleanTask.setInitialDelay(timeout);
+		cleanTask.setRepeats(false);
+		cleanTask.start();
+
+		inputTimes.put(key, Long.valueOf(System.currentTimeMillis()));
+		return super.put(key, value);
+	}
+
+	@Override
+	public V get(Object key) {
+		V v = super.get(key);
+		if (v != null) {
+			Long inputTime = inputTimes.get(key);
+			if ((System.currentTimeMillis() - inputTime) > timeout) {
+				remove(key);
+				inputTimes.remove(key);
+				return null;
+			}
+		}
+		return v;
+	}
+
+	/**
+	 * Liefert das eingestelte Timeout
+	 * 
+	 * @return
+	 */
+	public int getTimeout() {
+		return timeout;
+	}
+
+}


Property changes on: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/TimedLimitedHashmap.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: trunk/schmitzm-core/src/test/java/de/schmitzm/lang/TimedHashmapTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/lang/TimedHashmapTest.java	2011-04-01 21:00:31 UTC (rev 1547)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/lang/TimedHashmapTest.java	2011-04-05 13:45:01 UTC (rev 1548)
@@ -0,0 +1,43 @@
+package de.schmitzm.lang;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import de.schmitzm.testing.TestingClass;
+
+public class TimedHashmapTest extends TestingClass {
+
+	@Test
+	public void testTimedHashMap() throws InterruptedException {
+		TimedHashmap<String, String> tlhm = new TimedHashmap<String, String>(
+				300);
+
+		tlhm.put("A", "A");
+		Thread.sleep(110);
+		assertNotNull(tlhm.get("A"));
+		Thread.sleep(110);
+		assertNotNull(tlhm.get("A"));
+		Thread.sleep(110);
+		assertNull(tlhm.get("A"));
+	}
+	
+	@Test
+	public void testTimedHashMap_Autoclean() throws InterruptedException {
+		TimedHashmap<String, String> tlhm = new TimedHashmap<String, String>(
+				300);
+		
+		tlhm.put("A", "A");
+		Thread.sleep(110);
+		tlhm.put("B", "A");
+		Thread.sleep(110);
+		
+		assertTrue(tlhm.size() == 2);
+		Thread.sleep(410);
+		assertTrue(tlhm.size() == 0);
+	}
+
+
+}


Property changes on: trunk/schmitzm-core/src/test/java/de/schmitzm/lang/TimedHashmapTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: trunk/schmitzm-core/src/test/java/de/schmitzm/lang/TimedLimitedHashmapTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/lang/TimedLimitedHashmapTest.java	2011-04-01 21:00:31 UTC (rev 1547)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/lang/TimedLimitedHashmapTest.java	2011-04-05 13:45:01 UTC (rev 1548)
@@ -0,0 +1,41 @@
+package de.schmitzm.lang;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+import de.schmitzm.testing.TestingClass;
+
+public class TimedLimitedHashmapTest extends TestingClass {
+
+	@Test
+	public void testTimedLimitedHashMap() throws InterruptedException {
+		TimedLimitedHashmap<String, String> tlhm = new TimedLimitedHashmap<String, String>(
+				300, 1000);
+
+		tlhm.put("A", "A");
+		Thread.sleep(100);
+		assertNotNull(tlhm.get("A"));
+		Thread.sleep(100);
+		assertNotNull(tlhm.get("A"));
+		Thread.sleep(100);
+		assertNull(tlhm.get("A"));
+	}
+	
+
+	@Test
+	public void testTimedLimitedHashMap_Autoclean() throws InterruptedException {
+		TimedLimitedHashmap<String, String> tlhm = new TimedLimitedHashmap<String, String>(
+				300, 1000);
+		
+		tlhm.put("A", "A");
+		Thread.sleep(110);
+		tlhm.put("B", "A");
+		Thread.sleep(110);
+		
+		assertTrue(tlhm.size() == 2);
+		Thread.sleep(410);
+		assertTrue(tlhm.size() == 0);
+	}
+
+}


Property changes on: trunk/schmitzm-core/src/test/java/de/schmitzm/lang/TimedLimitedHashmapTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain



More information about the Schmitzm-commits mailing list