Class CyclicThreadSafeCollection<E>
- java.lang.Object
-
- org.jenkinsci.plugins.cloudstats.CyclicThreadSafeCollection<E>
-
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
@ThreadSafe public class CyclicThreadSafeCollection<E> extends Object implements Collection<E>
Circular thread-safe Collection.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 implementsclear()
.All operations, including iteration, are thread safe. Operations
add()
,size()
andisEmpty()
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 callingcontains()
on elements) or enteringsynchronized
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 Constructor Description CyclicThreadSafeCollection(int capacity)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(E e)
boolean
addAll(Collection<? extends E> c)
Add several elements at once.int
capacity()
Maximal collection capacity.void
clear()
boolean
contains(Object o)
boolean
containsAll(Collection<?> c)
boolean
isEmpty()
Iterator<E>
iterator()
Create thread-safe iterator for collection snapshot.boolean
remove(Object o)
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)
List<E>
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
-
-
-
-
Method Detail
-
add
public boolean add(E e)
- Specified by:
add
in interfaceCollection<E>
-
addAll
public boolean addAll(@NonNull Collection<? extends E> c)
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
@NonNull public Iterator<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
public boolean contains(Object o)
- Specified by:
contains
in interfaceCollection<E>
-
containsAll
public boolean containsAll(@NonNull Collection<?> c)
- Specified by:
containsAll
in interfaceCollection<E>
-
toArray
@NonNull public E[] toArray()
- Specified by:
toArray
in interfaceCollection<E>
-
toArray
@NonNull public <T> T[] toArray(@NonNull T[] ret)
- Specified by:
toArray
in interfaceCollection<E>
-
remove
public boolean remove(Object o)
- Specified by:
remove
in interfaceCollection<E>
-
removeAll
public boolean removeAll(@NonNull Collection<?> c)
- Specified by:
removeAll
in interfaceCollection<E>
-
retainAll
public boolean retainAll(@NonNull Collection<?> c)
- Specified by:
retainAll
in interfaceCollection<E>
-
-