Class CyclicThreadSafeCollection<E>
- All Implemented Interfaces:
Iterable<E>,Collection<E>
At most capacity elements are preserved and when more added, oldest elements are
deleted. Collection preserves insertion order of the elements so the oldest is the first one.
The class does not implement selective deletion operations (remove(),
removeAll(), retainAll()) but it implements clear().
All operations, including iteration, are thread safe. Operations add(),
size() and isEmpty() performs in constant time. The rest of the operations
have linear time complexity, while never holding the lock to perform any custom logic (such as
calling contains() on elements) or entering synchronized section on any
other monitor except for the internal lock. In such cases, lock is used only to make the copy of
the content.
- Author:
- ogondza.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends E> c) Add several elements at once.intcapacity()Maximal collection capacity.voidclear()booleanbooleancontainsAll(Collection<?> c) booleanisEmpty()iterator()Create thread-safe iterator for collection snapshot.booleanbooleanremoveAll(Collection<?> c) booleanretainAll(Collection<?> c) intsize()Number of contained elements, never more than capacity.E[]toArray()<T> T[]toArray(T[] ret) toList()Get elements in separate collection.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Constructor Details
-
CyclicThreadSafeCollection
public CyclicThreadSafeCollection(int capacity)
-
-
Method Details
-
add
- Specified by:
addin interfaceCollection<E>
-
addAll
Add several elements at once.It is not guaranteed the elements will be consecutive in the collection (other thread can add elements in between) but it is guaranteed to preserve order.
- Specified by:
addAllin interfaceCollection<E>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>
-
iterator
Create thread-safe iterator for collection snapshot.Iterated elements represent a snapshot of the collection.
-
size
@Nonnegative public int size()Number of contained elements, never more than capacity.- Specified by:
sizein interfaceCollection<E>
-
capacity
@Nonnegative public int capacity()Maximal collection capacity. -
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<E>
-
contains
- Specified by:
containsin interfaceCollection<E>
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>
-
toList
Get elements in separate collection. -
toArray
- Specified by:
toArrayin interfaceCollection<E>
-
toArray
@NonNull public <T> T[] toArray(@NonNull T[] ret) - Specified by:
toArrayin interfaceCollection<E>
-
remove
- Specified by:
removein interfaceCollection<E>
-
removeAll
- Specified by:
removeAllin interfaceCollection<E>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E>
-