package speiger.src.collections.PACKAGE.utils; #if TYPE_OBJECT import java.util.Collection; import java.util.Comparator; import java.util.function.Consumer; #endif import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.COLLECTION; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.functions.COMPARATOR; #endif #if DEQUEUE_FEATURE import speiger.src.collections.PACKAGE.queues.PRIORITY_DEQUEUE; #endif import speiger.src.collections.PACKAGE.queues.PRIORITY_QUEUE; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.functions.CONSUMER; #endif import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; /** * A Helper class for PriorityQueues */ public class PRIORITY_QUEUES { /** * Returns a synchronized PriorityQueue instance based on the instance given. * @param queue that should be synchronized * @Type(T) * @return a synchronized PriorityQueue wrapper. */ public static GENERIC_KEY_BRACES PRIORITY_QUEUE KEY_GENERIC_TYPE synchronize(PRIORITY_QUEUE KEY_GENERIC_TYPE queue) { return queue instanceof SynchronizedPriorityQueue ? (SynchronizedPriorityQueue KEY_GENERIC_TYPE)queue : new SynchronizedPriorityQueueBRACES(queue); } /** * Returns a synchronized PriorityQueue instance based on the instance given. * @param queue that should be synchronized * @param mutex is the controller of the synchronization block. * @Type(T) * @return a synchronized PriorityQueue wrapper. */ public static GENERIC_KEY_BRACES PRIORITY_QUEUE KEY_GENERIC_TYPE synchronize(PRIORITY_QUEUE KEY_GENERIC_TYPE queue, Object mutex) { return queue instanceof SynchronizedPriorityQueue ? (SynchronizedPriorityQueue KEY_GENERIC_TYPE)queue : new SynchronizedPriorityQueueBRACES(queue, mutex); } #if DEQUEUE_FEATURE /** * Returns a synchronized PriorityDequeue instance based on the instance given. * @param dequeue that should be synchronized * @Type(T) * @return a synchronized PriorityDequeue wrapper. */ public static GENERIC_KEY_BRACES PRIORITY_DEQUEUE KEY_GENERIC_TYPE synchronize(PRIORITY_DEQUEUE KEY_GENERIC_TYPE dequeue) { return dequeue instanceof SynchronizedPriorityDequeue ? (SynchronizedPriorityDequeue KEY_GENERIC_TYPE)dequeue : new SynchronizedPriorityDequeueBRACES(dequeue); } /** * Returns a synchronized PriorityDequeue instance based on the instance given. * @param dequeue that should be synchronized * @param mutex is the controller of the synchronization block. * @Type(T) * @return a synchronized PriorityDequeue wrapper. */ public static GENERIC_KEY_BRACES PRIORITY_DEQUEUE KEY_GENERIC_TYPE synchronize(PRIORITY_DEQUEUE KEY_GENERIC_TYPE dequeue, Object mutex) { return dequeue instanceof SynchronizedPriorityDequeue ? (SynchronizedPriorityDequeue KEY_GENERIC_TYPE)dequeue : new SynchronizedPriorityDequeueBRACES(dequeue, mutex); } #endif /** * Wrapper class for synchronization * @Type(T) */ public static class SynchronizedPriorityQueue KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_GENERIC_TYPE { PRIORITY_QUEUE KEY_GENERIC_TYPE queue; protected Object mutex; protected SynchronizedPriorityQueue(PRIORITY_QUEUE KEY_GENERIC_TYPE queue) { this.queue = queue; mutex = this; } protected SynchronizedPriorityQueue(PRIORITY_QUEUE KEY_GENERIC_TYPE queue, Object mutex) { this.queue = queue; this.mutex = mutex; } @Override public ITERATOR KEY_GENERIC_TYPE iterator() { return queue.iterator(); } @Override public int size() { synchronized(mutex) { return queue.size(); } } @Override public void clear() { synchronized(mutex) { queue.clear(); } } @Override public void enqueue(KEY_TYPE e) { synchronized(mutex) { queue.enqueue(e); } } @Override public void enqueueAll(KEY_TYPE[] e, int offset, int length) { synchronized(mutex) { queue.enqueueAll(e, offset, length); } } @Override public void enqueueAll(COLLECTION KEY_GENERIC_TYPE c) { synchronized(mutex) { queue.enqueueAll(c); } } #if TYPE_OBJECT @Override public void enqueueAll(Collection c) { synchronized(mutex) { queue.enqueueAll(c); } } #endif @Override public KEY_TYPE dequeue() { synchronized(mutex) { return queue.dequeue(); } } @Override public KEY_TYPE peek(int index) { synchronized(mutex) { return queue.peek(index); } } @Override public boolean removeFirst(KEY_TYPE e) { synchronized(mutex) { return queue.removeFirst(e); } } @Override public boolean removeLast(KEY_TYPE e) { synchronized(mutex) { return queue.removeLast(e); } } @Override public void onChanged() { synchronized(mutex) { queue.onChanged(); } } @Override public COMPARATOR KEY_SUPER_GENERIC_TYPE comparator() { synchronized(mutex) { return queue.comparator(); } } @Override public GENERIC_SPECIAL_KEY_BRACES KEY_SPECIAL_TYPE[] TO_ARRAY(KEY_SPECIAL_TYPE[] input) { synchronized(mutex) { return queue.TO_ARRAY(input); } } @Override public PRIORITY_QUEUE KEY_GENERIC_TYPE copy() { synchronized(mutex) { return queue.copy(); } } @Override public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) { synchronized(mutex) { queue.forEach(action); } } @Override public void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE action) { synchronized(mutex) { queue.forEach(input, action); } } @Override public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.matchesAny(filter); } } @Override public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.matchesNone(filter); } } @Override public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.matchesAll(filter); } } @Override public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.findFirst(filter); } } @Override public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.count(filter); } } } #if DEQUEUE_FEATURE /** * Wrapper class for synchronization * @Type(T) */ public static class SynchronizedPriorityDequeue KEY_GENERIC_TYPE extends SynchronizedPriorityQueue KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_GENERIC_TYPE { PRIORITY_DEQUEUE KEY_GENERIC_TYPE dequeue; protected SynchronizedPriorityDequeue(PRIORITY_DEQUEUE KEY_GENERIC_TYPE queue) { super(queue); dequeue = queue; } protected SynchronizedPriorityDequeue(PRIORITY_DEQUEUE KEY_GENERIC_TYPE queue, Object mutex) { super(queue, mutex); dequeue = queue; } @Override public void enqueueFirst(KEY_TYPE e) { synchronized(mutex) { dequeue.enqueueFirst(e); } } @Override public void enqueueAllFirst(KEY_TYPE[] e, int offset, int length) { synchronized(mutex) { dequeue.enqueueAllFirst(e, offset, length); } } @Override public void enqueueAllFirst(COLLECTION KEY_GENERIC_TYPE c) { synchronized(mutex) { dequeue.enqueueAllFirst(c); } } #if TYPE_OBJECT @Override public void enqueueAllFirst(Collection c) { synchronized(mutex) { dequeue.enqueueAllFirst(c); } } #endif @Override public KEY_TYPE dequeueLast() { synchronized(mutex) { return dequeue.dequeueLast(); } } @Override public PRIORITY_DEQUEUE KEY_GENERIC_TYPE copy() { synchronized(mutex) { return dequeue.copy(); } } } #endif }