Package org.drasyl.util
Class ExpiringSet<E>
- java.lang.Object
-
- org.drasyl.util.ExpiringSet<E>
-
- Type Parameters:
E
- the type of elements maintained by this set
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,Set<E>
public class ExpiringSet<E> extends Object implements Set<E>
ASet
that expires elements based on oldest age (when maximum size has been exceeded) or writeThe expiration policy is only enforced on set access. There will be no automatic expiration handling running in a background thread or similar. For performance reasons the policy is not enforced on every single access, but only once every "expiration window" (
Math
.max(expireAfterWrite, expireAfterAccess)). Therefore, it may happen that elements are kept in the set up to the double expiration window length.This data structure is not thread-safe!
-
-
Constructor Summary
Constructors Constructor Description ExpiringSet(long maximumSize, long expireAfterWrite)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(E e)
boolean
addAll(Collection<? extends E> c)
void
clear()
boolean
contains(Object o)
boolean
containsAll(Collection<?> c)
boolean
isEmpty()
Iterator<E>
iterator()
boolean
remove(Object o)
boolean
removeAll(Collection<?> c)
boolean
retainAll(Collection<?> c)
int
size()
Object[]
toArray()
<T> T[]
toArray(T[] a)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.Set
equals, hashCode, spliterator
-
-
-
-
Constructor Detail
-
ExpiringSet
public ExpiringSet(long maximumSize, long expireAfterWrite)
- Parameters:
maximumSize
- maximum number of entries that the set should contain. On overflow, first elements based on expiration policy are removed.-1
deactivates a size limitation.expireAfterWrite
- time in milliseconds after which elements are automatically removed from the set after being added.- Throws:
IllegalArgumentException
- ifmaximumSize
is0
orexpireAfterWrite
is-1
.
-
-
Method Detail
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
contains
public boolean contains(Object o)
-
toArray
public Object[] toArray()
-
toArray
public <T> T[] toArray(T[] a)
-
add
public boolean add(E e)
-
remove
public boolean remove(Object o)
-
containsAll
public boolean containsAll(Collection<?> c)
- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceSet<E>
-
addAll
public boolean addAll(Collection<? extends E> c)
-
retainAll
public boolean retainAll(Collection<?> c)
-
removeAll
public boolean removeAll(Collection<?> c)
-
-