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 TypeMethodDescriptionboolean
boolean
addAll
(Collection<? extends E> c) Add several elements at once.int
capacity()
Maximal collection capacity.void
clear()
boolean
boolean
containsAll
(Collection<?> c) boolean
isEmpty()
iterator()
Create thread-safe iterator for collection snapshot.boolean
boolean
removeAll
(Collection<?> c) boolean
retainAll
(Collection<?> c) int
size()
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, wait
Methods 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:
add
in 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:
addAll
in interfaceCollection<E>
-
clear
public void clear()- Specified by:
clear
in 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:
size
in interfaceCollection<E>
-
capacity
@Nonnegative public int capacity()Maximal collection capacity. -
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<E>
-
contains
- Specified by:
contains
in interfaceCollection<E>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<E>
-
toList
Get elements in separate collection. -
toArray
- Specified by:
toArray
in interfaceCollection<E>
-
toArray
@NonNull public <T> T[] toArray(@NonNull T[] ret) - Specified by:
toArray
in interfaceCollection<E>
-
remove
- Specified by:
remove
in interfaceCollection<E>
-
removeAll
- Specified by:
removeAll
in interfaceCollection<E>
-
retainAll
- Specified by:
retainAll
in interfaceCollection<E>
-