summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util/TimeoutReference.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openslx/util/TimeoutReference.java')
-rw-r--r--src/main/java/org/openslx/util/TimeoutReference.java49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/util/TimeoutReference.java b/src/main/java/org/openslx/util/TimeoutReference.java
new file mode 100644
index 0000000..4d7a992
--- /dev/null
+++ b/src/main/java/org/openslx/util/TimeoutReference.java
@@ -0,0 +1,49 @@
+package org.openslx.util;
+
+/**
+ * A reference holder that will only return the object it is holding if
+ * the given timeout has not been reached.
+ * The timeout will start anew if you set this reference to hold a new object,
+ * or if you retrieve the object, and refreshOnGet is set to true.
+ */
+public class TimeoutReference<T>
+{
+
+ private final long timeoutMs;
+ private long deadline;
+ private boolean refreshOnGet;
+ private T item;
+
+ public TimeoutReference( boolean refreshOnGet, long timeoutMs, T item )
+ {
+ this.refreshOnGet = refreshOnGet;
+ this.item = item;
+ this.timeoutMs = timeoutMs;
+ this.deadline = System.currentTimeMillis() + timeoutMs;
+ }
+
+ public TimeoutReference( long timeoutMs, T item )
+ {
+ this( false, timeoutMs, item );
+ }
+
+ public synchronized T get()
+ {
+ if ( item != null ) {
+ final long now = System.currentTimeMillis();
+ if ( deadline < now ) {
+ item = null;
+ } else if ( refreshOnGet ) {
+ deadline = now + timeoutMs;
+ }
+ }
+ return item;
+ }
+
+ public synchronized void set( T newItem )
+ {
+ this.item = newItem;
+ this.deadline = System.currentTimeMillis() + timeoutMs;
+ }
+
+}