diff options
| author | Simon Rettberg | 2015-06-05 11:25:50 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2015-06-05 11:25:50 +0200 |
| commit | 693392fe6c0022e7ec5060192ee322c7753b0d90 (patch) | |
| tree | 855245927778c6b069714909929684d1da7d449f /src/main/java/org/openslx/util | |
| parent | Cleanup thrift shandling stuff (diff) | |
| download | master-sync-shared-693392fe6c0022e7ec5060192ee322c7753b0d90.tar.gz master-sync-shared-693392fe6c0022e7ec5060192ee322c7753b0d90.tar.xz master-sync-shared-693392fe6c0022e7ec5060192ee322c7753b0d90.zip | |
Changes for Dozmod v1.1
Diffstat (limited to 'src/main/java/org/openslx/util')
| -rw-r--r-- | src/main/java/org/openslx/util/TimeoutReference.java | 49 |
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; + } + +} |
