You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2018/01/03 02:49:44 UTC
commons-collections git commit: [COLLECTIONS-575] Synchronized queue
wrapper in QueueUtils.
Repository: commons-collections
Updated Branches:
refs/heads/master 611c73889 -> c6dc370ab
[COLLECTIONS-575] Synchronized queue wrapper in QueueUtils.
Project: http://git-wip-us.apache.org/repos/asf/commons-collections/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-collections/commit/c6dc370a
Tree: http://git-wip-us.apache.org/repos/asf/commons-collections/tree/c6dc370a
Diff: http://git-wip-us.apache.org/repos/asf/commons-collections/diff/c6dc370a
Branch: refs/heads/master
Commit: c6dc370abbbf0b487a13bd7f564287a41755bf71
Parents: 611c738
Author: Gary Gregory <gg...@apache.org>
Authored: Tue Jan 2 19:49:41 2018 -0700
Committer: Gary Gregory <gg...@apache.org>
Committed: Tue Jan 2 19:49:41 2018 -0700
----------------------------------------------------------------------
src/changes/changes.xml | 3 +
.../apache/commons/collections4/QueueUtils.java | 32 +++++
.../collections4/queue/SynchronizedQueue.java | 143 +++++++++++++++++++
.../commons/collections4/QueueUtilsTest.java | 17 ++-
.../queue/SynchronizedQueueTest.java | 63 ++++++++
...hronizedQueue.emptyCollection.version4.2.obj | Bin 0 -> 271 bytes
...chronizedQueue.fullCollection.version4.2.obj | Bin 0 -> 678 bytes
7 files changed, 257 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/c6dc370a/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index eb6ab22..8d68c42 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -75,6 +75,9 @@
<action issue="COLLECTIONS-668" dev="ggregory" type="add" due-to="Gary Gregory">
Add CollectionUtils containsAny method for primitive array: org.apache.commons.collections4.CollectionUtils.containsAny(Collection<?>, T...).
</action>
+ <action issue="COLLECTIONS-575" dev="ggregory" type="add" due-to="Guram Savinov, Grzegorz Rożniecki, Bruno P. Kinoshita, Gary Gregory">
+ Synchronized queue wrapper in QueueUtils.
+ </action>
</release>
<release version="4.1" date="2015-11-28" description="This is a security and minor release.">
<action issue="COLLECTIONS-508" dev="tn" type="add">
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/c6dc370a/src/main/java/org/apache/commons/collections4/QueueUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/collections4/QueueUtils.java b/src/main/java/org/apache/commons/collections4/QueueUtils.java
index 053581c..ee4114b 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;
@@ -44,6 +45,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.
+ * <p>
+ * It is imperative that the user manually synchronize on the returned queue
+ * when iterating over it:
+ *
+ * <pre>
+ * Queue queue = QueueUtils.synchronizedQueue(new CircularFifoQueue());
+ * ...
+ * synchronized(queue) {
+ * Iterator i = queue.iterator(); // Must be in synchronized block
+ * while (i.hasNext())
+ * foo(i.next());
+ * }
+ * }
+ * </pre>
+ *
+ * Failure to follow this advice may result in non-deterministic behavior.
+ *
+ * @param <E> 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 <E> Queue<E> synchronizedQueue(final Queue<E> queue) {
+ return SynchronizedQueue.synchronizedQueue(queue);
+ }
+
+ /**
* Returns an unmodifiable queue backed by the given queue.
*
* @param <E> the type of the elements in the queue
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/c6dc370a/src/main/java/org/apache/commons/collections4/queue/SynchronizedQueue.java
----------------------------------------------------------------------
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 0000000..5eda57f
--- /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.
+ * <p>
+ * Methods are synchronized, then forwarded to the decorated queue. Iterators must be separately synchronized around the
+ * loop.
+ *
+ * @param <E> the type of the elements in the collection
+ * @since 4.2
+ */
+public class SynchronizedQueue<E> extends SynchronizedCollection<E> implements Queue<E> {
+
+ /** Serialization version */
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Factory method to create a synchronized queue.
+ *
+ * @param <E>
+ * 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 <E> SynchronizedQueue<E> synchronizedQueue(final Queue<E> queue) {
+ return new SynchronizedQueue<E>(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<E> 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<E> queue, final Object lock) {
+ super(queue, lock);
+ }
+
+ /**
+ * Gets the queue being decorated.
+ *
+ * @return the decorated queue
+ */
+ @Override
+ protected Queue<E> decorated() {
+ return (Queue<E>) 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();
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/c6dc370a/src/test/java/org/apache/commons/collections4/QueueUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/collections4/QueueUtilsTest.java b/src/test/java/org/apache/commons/collections4/QueueUtilsTest.java
index 970f9ce..d75c321 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;
@@ -39,6 +42,18 @@ public class QueueUtilsTest {
// ----------------------------------------------------------------------
@Test
+ public void testSynchronizedQueue() {
+ Queue<Object> queue = QueueUtils.synchronizedQueue(new LinkedList<Object>());
+ 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<Object> queue = QueueUtils.unmodifiableQueue(new LinkedList<>());
assertTrue("Returned object should be an UnmodifiableQueue.", queue instanceof UnmodifiableQueue);
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/c6dc370a/src/test/java/org/apache/commons/collections4/queue/SynchronizedQueueTest.java
----------------------------------------------------------------------
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 0000000..a48f0ac
--- /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<T> extends AbstractQueueTest<T> {
+
+ 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<T> makeObject() {
+ return SynchronizedQueue.synchronizedQueue(new LinkedList<T>());
+ }
+
+ @Ignore("Run once")
+ public void testCreate() throws Exception {
+ Queue<T> 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");
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/c6dc370a/src/test/resources/data/test/SynchronizedQueue.emptyCollection.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 0000000..96c1df5
Binary files /dev/null and b/src/test/resources/data/test/SynchronizedQueue.emptyCollection.version4.2.obj differ
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/c6dc370a/src/test/resources/data/test/SynchronizedQueue.fullCollection.version4.2.obj
----------------------------------------------------------------------
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 0000000..f8cde0f
Binary files /dev/null and b/src/test/resources/data/test/SynchronizedQueue.fullCollection.version4.2.obj differ