From 36c2a59ead341539dea9a25c7f514f0ecd8177d8 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 24 Apr 2018 11:34:41 +0200 Subject: Tweak TimeoutHashMap --- src/main/java/org/openslx/util/TimeoutHashMap.java | 21 ++++++++++++-- .../java/org/openslx/util/TimeoutReference.java | 32 ++++++++++++---------- 2 files changed, 36 insertions(+), 17 deletions(-) (limited to 'src/main') 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 implements Map TimeoutReference 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 implements Map { return map.keySet(); } + + public Map getImmutableSnapshot() + { + Map copy = new HashMap<>(); + for (Entry> 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 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 { 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 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 ) ) ); } } -- cgit v1.2.3-55-g7522