diff --git a/src/changes/changes.xml b/src/changes/changes.xml index eb6ab2296..8d68c4287 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -75,6 +75,9 @@ Add CollectionUtils containsAny method for primitive array: org.apache.commons.collections4.CollectionUtils.containsAny(Collection<?>, T...). + + Synchronized queue wrapper in QueueUtils. + diff --git a/src/main/java/org/apache/commons/collections4/QueueUtils.java b/src/main/java/org/apache/commons/collections4/QueueUtils.java index 053581cb3..ee4114bea 100644 --- a/src/main/java/org/apache/commons/collections4/QueueUtils.java +++ b/src/main/java/org/apache/commons/collections4/QueueUtils.java @@ -20,6 +20,7 @@ import java.util.LinkedList; import java.util.Queue; import org.apache.commons.collections4.queue.PredicatedQueue; +import org.apache.commons.collections4.queue.SynchronizedQueue; import org.apache.commons.collections4.queue.TransformedQueue; import org.apache.commons.collections4.queue.UnmodifiableQueue; @@ -43,6 +44,37 @@ public class QueueUtils { //----------------------------------------------------------------------- + /** + * Returns a synchronized (thread-safe) queue backed by the given queue. + * In order to guarantee serial access, it is critical that all access to the + * backing queue is accomplished through the returned queue. + *

+ * It is imperative that the user manually synchronize on the returned queue + * when iterating over it: + * + *

+     * Queue queue = QueueUtils.synchronizedQueue(new CircularFifoQueue());
+     * ...
+     * 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. + * + * @param the element type + * @param queue the queue to synchronize, must not be null + * @return a synchronized queue backed by that queue + * @throws NullPointerException if the queue is null + * @since 4.2 + */ + public static Queue synchronizedQueue(final Queue queue) { + return SynchronizedQueue.synchronizedQueue(queue); + } + /** * Returns an unmodifiable queue backed by the given queue. * diff --git a/src/main/java/org/apache/commons/collections4/queue/SynchronizedQueue.java b/src/main/java/org/apache/commons/collections4/queue/SynchronizedQueue.java new file mode 100644 index 000000000..5eda57fb0 --- /dev/null +++ b/src/main/java/org/apache/commons/collections4/queue/SynchronizedQueue.java @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.collections4.queue; + +import java.util.Queue; + +import org.apache.commons.collections4.collection.SynchronizedCollection; + +/** + * Decorates another {@link Queue} to synchronize its behaviour for a multi-threaded environment. + *

+ * Methods are synchronized, then forwarded to the decorated queue. Iterators must be separately synchronized around the + * loop. + * + * @param the type of the elements in the collection + * @since 4.2 + */ +public class SynchronizedQueue extends SynchronizedCollection implements Queue { + + /** Serialization version */ + private static final long serialVersionUID = 1L; + + /** + * Factory method to create a synchronized queue. + * + * @param + * the type of the elements in the queue + * @param queue + * the queue to decorate, must not be null + * @return a new synchronized Queue + * @throws NullPointerException + * if queue is null + */ + public static SynchronizedQueue synchronizedQueue(final Queue queue) { + return new SynchronizedQueue(queue); + } + + // ----------------------------------------------------------------------- + /** + * Constructor that wraps (not copies). + * + * @param queue + * the queue to decorate, must not be null + * @throws NullPointerException + * if queue is null + */ + protected SynchronizedQueue(final Queue queue) { + super(queue); + } + + /** + * Constructor that wraps (not copies). + * + * @param queue + * the queue to decorate, must not be null + * @param lock + * the lock to use, must not be null + * @throws NullPointerException + * if queue or lock is null + */ + protected SynchronizedQueue(final Queue queue, final Object lock) { + super(queue, lock); + } + + /** + * Gets the queue being decorated. + * + * @return the decorated queue + */ + @Override + protected Queue decorated() { + return (Queue) super.decorated(); + } + + @Override + public E element() { + synchronized (lock) { + return decorated().element(); + } + } + + @Override + public boolean equals(final Object object) { + if (object == this) { + return true; + } + synchronized (lock) { + return decorated().equals(object); + } + } + + // ----------------------------------------------------------------------- + + @Override + public int hashCode() { + synchronized (lock) { + return decorated().hashCode(); + } + } + + @Override + public boolean offer(final E e) { + synchronized (lock) { + return decorated().offer(e); + } + } + + @Override + public E peek() { + synchronized (lock) { + return decorated().peek(); + } + } + + @Override + public E poll() { + synchronized (lock) { + return decorated().poll(); + } + } + + @Override + public E remove() { + synchronized (lock) { + return decorated().remove(); + } + } + +} diff --git a/src/test/java/org/apache/commons/collections4/QueueUtilsTest.java b/src/test/java/org/apache/commons/collections4/QueueUtilsTest.java index 970f9ce55..d75c321cf 100644 --- a/src/test/java/org/apache/commons/collections4/QueueUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/QueueUtilsTest.java @@ -16,13 +16,16 @@ */ package org.apache.commons.collections4; -import static org.junit.Assert.*; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.util.LinkedList; import java.util.Queue; import org.apache.commons.collections4.functors.TruePredicate; import org.apache.commons.collections4.queue.PredicatedQueue; +import org.apache.commons.collections4.queue.SynchronizedQueue; import org.apache.commons.collections4.queue.TransformedQueue; import org.apache.commons.collections4.queue.UnmodifiableQueue; import org.junit.Test; @@ -38,6 +41,18 @@ public class QueueUtilsTest { // ---------------------------------------------------------------------- + @Test + public void testSynchronizedQueue() { + Queue queue = QueueUtils.synchronizedQueue(new LinkedList()); + assertTrue("Returned object should be a SynchronizedQueue.", queue instanceof SynchronizedQueue); + try { + QueueUtils.synchronizedQueue(null); + fail("Expecting NullPointerException for null queue."); + } catch (final NullPointerException ex) { + // expected + } + } + @Test public void testUnmodifiableQueue() { Queue queue = QueueUtils.unmodifiableQueue(new LinkedList<>()); diff --git a/src/test/java/org/apache/commons/collections4/queue/SynchronizedQueueTest.java b/src/test/java/org/apache/commons/collections4/queue/SynchronizedQueueTest.java new file mode 100644 index 000000000..a48f0aca9 --- /dev/null +++ b/src/test/java/org/apache/commons/collections4/queue/SynchronizedQueueTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.collections4.queue; + +import java.util.LinkedList; +import java.util.Queue; + +import org.apache.commons.collections4.BulkTest; +import org.junit.Ignore; + +import junit.framework.Test; + +/** + * Extension of {@link AbstractQueueTest} for exercising the + * {@link SynchronizedQueue} implementation. + * + * @since 4.2 + */ +public class SynchronizedQueueTest extends AbstractQueueTest { + + public static Test suite() { + return BulkTest.makeSuite(SynchronizedQueueTest.class); + } + + public SynchronizedQueueTest(final String testName) { + super(testName); + } + + //----------------------------------------------------------------------- + + @Override + public String getCompatibilityVersion() { + return "4.2"; + } + + @Override + public Queue makeObject() { + return SynchronizedQueue.synchronizedQueue(new LinkedList()); + } + + @Ignore("Run once") + public void testCreate() throws Exception { + Queue queue = makeObject(); + writeExternalFormToDisk((java.io.Serializable) queue, "src/test/resources/data/test/SynchronizedQueue.emptyCollection.version4.2.obj"); + queue = makeFullCollection(); + writeExternalFormToDisk((java.io.Serializable) queue, "src/test/resources/data/test/SynchronizedQueue.fullCollection.version4.2.obj"); + } + +} diff --git a/src/test/resources/data/test/SynchronizedQueue.emptyCollection.version4.2.obj b/src/test/resources/data/test/SynchronizedQueue.emptyCollection.version4.2.obj new file mode 100644 index 000000000..96c1df5b2 Binary files /dev/null and b/src/test/resources/data/test/SynchronizedQueue.emptyCollection.version4.2.obj differ diff --git a/src/test/resources/data/test/SynchronizedQueue.fullCollection.version4.2.obj b/src/test/resources/data/test/SynchronizedQueue.fullCollection.version4.2.obj new file mode 100644 index 000000000..f8cde0faf Binary files /dev/null and b/src/test/resources/data/test/SynchronizedQueue.fullCollection.version4.2.obj differ