summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2018-04-24 11:34:41 +0200
committerSimon Rettberg2018-04-24 11:34:41 +0200
commit36c2a59ead341539dea9a25c7f514f0ecd8177d8 (patch)
tree44854ff4ecdda5ade8202c4e03d3b1c3ee157bdd
parent[vbox] more cleanup + formatting (diff)
downloadmaster-sync-shared-36c2a59ead341539dea9a25c7f514f0ecd8177d8.tar.gz
master-sync-shared-36c2a59ead341539dea9a25c7f514f0ecd8177d8.tar.xz
master-sync-shared-36c2a59ead341539dea9a25c7f514f0ecd8177d8.zip
Tweak TimeoutHashMap
-rw-r--r--src/main/java/org/openslx/util/TimeoutHashMap.java21
-rw-r--r--src/main/java/org/openslx/util/TimeoutReference.java32
2 files changed, 36 insertions, 17 deletions
diff --git a/src/main/java/org/openslx/util/TimeoutHashMap.java b/src/main/java/org/openslx/util/TimeoutHashMap.java
index 3efb367..8655cc3 100644
--- a/src/main/java/org/openslx/util/TimeoutHashMap.java
+++ b/src/main/java/org/openslx/util/TimeoutHashMap.java
@@ -1,6 +1,7 @@
package org.openslx.util;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@@ -47,9 +48,13 @@ public class TimeoutHashMap<K, V> implements Map<K, V>
TimeoutReference<V> timeoutReference = map.get( key );
if ( timeoutReference == null )
return null;
- return timeoutReference.get();
+ V obj = timeoutReference.get();
+ if ( obj == null && timeoutReference.isInvalid() ) {
+ map.remove( key );
+ }
+ return obj;
}
-
+
@Override
public V put( K key, V value )
{
@@ -86,6 +91,18 @@ public class TimeoutHashMap<K, V> implements Map<K, V>
{
return map.keySet();
}
+
+ public Map<K, V> getImmutableSnapshot()
+ {
+ Map<K, V> copy = new HashMap<>();
+ for (Entry<K, TimeoutReference<V>> i : map.entrySet()) {
+ V v = i.getValue().get();
+ if (i.getValue().isInvalid())
+ continue;
+ copy.put(i.getKey(), v);
+ }
+ return Collections.unmodifiableMap( copy );
+ }
@Override
public Collection<V> values()
diff --git a/src/main/java/org/openslx/util/TimeoutReference.java b/src/main/java/org/openslx/util/TimeoutReference.java
index e86c6c7..ddcca36 100644
--- a/src/main/java/org/openslx/util/TimeoutReference.java
+++ b/src/main/java/org/openslx/util/TimeoutReference.java
@@ -10,9 +10,10 @@ public class TimeoutReference<T>
{
private final long timeoutMs;
+ private final boolean refreshOnGet;
+ private final T item;
private long deadline;
- private boolean refreshOnGet;
- private T item;
+ private boolean invalid;
public TimeoutReference( boolean refreshOnGet, long timeoutMs, T item )
{
@@ -29,33 +30,34 @@ public class TimeoutReference<T>
public synchronized T get()
{
- if ( item != null ) {
- final long now = System.currentTimeMillis();
- if ( deadline < now ) {
- item = null;
- } else if ( refreshOnGet ) {
- deadline = now + timeoutMs;
- }
+ if ( item == null || invalid )
+ return null;
+ final long now = System.currentTimeMillis();
+ if ( deadline < now ) {
+ invalid = true;
+ return null;
+ }
+ if ( refreshOnGet ) {
+ deadline = now + timeoutMs;
}
return item;
}
-
- public synchronized void set( T newItem )
+
+ synchronized boolean isInvalid()
{
- this.item = newItem;
- this.deadline = System.currentTimeMillis() + timeoutMs;
+ return invalid;
}
@Override
public int hashCode()
{
- return item.hashCode();
+ return item == null ? super.hashCode() : item.hashCode();
}
@Override
public boolean equals( Object o )
{
- return this == o || item.equals( o );
+ return ( this == o || this.item == o || ( o != null && o.equals( this.item ) ) );
}
}