Package org.eclipse.scanning.event.queue
Class SynchronizedModifiableIdQueue<E extends IdBean>
java.lang.Object
java.util.AbstractCollection<E>
org.eclipse.scanning.event.queue.SynchronizedModifiableIdQueue<E>
- Type Parameters:
E- the class of objects in the queue
- All Implemented Interfaces:
AutoCloseable,Iterable<E>,Collection<E>,Queue<E>,IModifiableIdQueue<E>,IPersistentModifiableIdQueue<E>
public class SynchronizedModifiableIdQueue<E extends IdBean>
extends AbstractCollection<E>
implements IPersistentModifiableIdQueue<E>
A persistent thread-safe modifiable
Queue.
In addition to implementing Queue in a thread-safe way, this class also
implements IModifiableIdQueue to add methods to reorder and replace items in
the queue.
The beans in the queue must be IdBeans (or some subclass), and this class uses
the bean's unique id (as returned by IdBean.getUniqueId() rather than
object equality. Note: it is the responsibility of client code to ensure does not
contain multiple beans with the same unique id. If this requirement is not upheld
the behaviour of this class is undefined.
It is imperative that the user manually synchronize on the returned queue when iterating over it:
Queue queue = new SynchronizedModifiableIdQueue();
...
synchronized (queue) {
Iterator i = queue.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
Failure to follow this advice may result in non-deterministic behavior.
This warning also applies to any other behavior that the client wishes to
be performed in an atomic manner.-
Constructor Summary
ConstructorsConstructorDescriptionSynchronizedModifiableIdQueue(IEventConnectorService eventConnectorService, String dbStoreFilename, String queueName) SynchronizedModifiableIdQueue(IEventConnectorService eventConnectorService, org.h2.mvstore.MVStore store, String queueName) -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends E> c) voidclear()voidclose()booleanbooleancontainsAll(Collection<?> c) element()Return a list of contents of the queue, in the same order as they are in the queue.iterator()Returns an iterator over the elements in this queue.booleanMoves the element in the queue with the same id as that given down the queue, i.e.booleanMoves the element in the queue with the same id as that given up the queue, i.e.booleanpeek()poll()remove()booleanbooleanremoveAll(Collection<?> c) booleanReplaces the existing element in the queue with the same id as the given element with the given element.booleanretainAll(Collection<?> c) intsize()Object[]toArray()<T> T[]toArray(T[] a) Methods inherited from class java.util.AbstractCollection
isEmpty, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
equals, hashCode, isEmpty, parallelStream, removeIf, spliterator, stream, toArray
-
Constructor Details
-
SynchronizedModifiableIdQueue
public SynchronizedModifiableIdQueue(IEventConnectorService eventConnectorService, String dbStoreFilename, String queueName) -
SynchronizedModifiableIdQueue
public SynchronizedModifiableIdQueue(IEventConnectorService eventConnectorService, org.h2.mvstore.MVStore store, String queueName)
-
-
Method Details
-
getQueueName
- Specified by:
getQueueNamein interfaceIPersistentModifiableIdQueue<E extends IdBean>
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
getElements
Description copied from interface:IModifiableIdQueueReturn a list of contents of the queue, in the same order as they are in the queue. Note that the list is a snapshot of the contents of the queue at the time that this method is called, changes to this list will not be reflected in the underlying queue.- Specified by:
getElementsin interfaceIModifiableIdQueue<E extends IdBean>- Returns:
- a list of the contents of the queue
-
contains
- Specified by:
containsin interfaceCollection<E extends IdBean>- Overrides:
containsin classAbstractCollection<E extends IdBean>
-
toArray
- Specified by:
toArrayin interfaceCollection<E extends IdBean>- Overrides:
toArrayin classAbstractCollection<E extends IdBean>
-
toArray
public <T> T[] toArray(T[] a) - Specified by:
toArrayin interfaceCollection<E extends IdBean>- Overrides:
toArrayin classAbstractCollection<E extends IdBean>
-
containsAll
- Specified by:
containsAllin interfaceCollection<E extends IdBean>- Overrides:
containsAllin classAbstractCollection<E extends IdBean>
-
addAll
- Specified by:
addAllin interfaceCollection<E extends IdBean>- Overrides:
addAllin classAbstractCollection<E extends IdBean>
-
removeAll
- Specified by:
removeAllin interfaceCollection<E extends IdBean>- Overrides:
removeAllin classAbstractCollection<E extends IdBean>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E extends IdBean>- Overrides:
retainAllin classAbstractCollection<E extends IdBean>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E extends IdBean>- Overrides:
clearin classAbstractCollection<E extends IdBean>
-
offer
-
add
-
remove
-
poll
-
element
-
peek
-
replace
Description copied from interface:IModifiableIdQueueReplaces the existing element in the queue with the same id as the given element with the given element. If the queue does not contain an element with the same id as the one given, the queue is unmodified.- Specified by:
replacein interfaceIModifiableIdQueue<E extends IdBean>- Parameters:
e- the new element- Returns:
trueif the queue was modified as a result of this call (i.e. the queue contained an element with the same id as the that given),falseotherwise
-
moveUp
Description copied from interface:IModifiableIdQueueMoves the element in the queue with the same id as that given up the queue, i.e. towards the head. If the queue does not contain an element with the same id as the one given, the queue is unmodified.- Specified by:
moveUpin interfaceIModifiableIdQueue<E extends IdBean>- Parameters:
e- the element to move up the queue- Returns:
trueif the queue was modified as a result of this call (i.e. the queue contained an element with the same id as the that given),falseotherwise
-
moveDown
Description copied from interface:IModifiableIdQueueMoves the element in the queue with the same id as that given down the queue, i.e. towards the tail. If the queue does not contain an element with the same id as the one given, the queue is unmodified.- Specified by:
moveDownin interfaceIModifiableIdQueue<E extends IdBean>- Parameters:
e- the element to move down the queue- Returns:
trueif the queue was modified as a result of this call (i.e. the queue contained an element with the same id as the that given),falseotherwise
-
remove
- Specified by:
removein interfaceCollection<E extends IdBean>- Overrides:
removein classAbstractCollection<E extends IdBean>
-
iterator
Returns an iterator over the elements in this queue. Note: client code must manually sync on this queue object. -
size
public int size()- Specified by:
sizein interfaceCollection<E extends IdBean>- Specified by:
sizein classAbstractCollection<E extends IdBean>
-