mirror of
https://github.com/apache/commons-collections.git
synced 2025-02-17 15:35:00 +00:00
[COLLECTIONS-575] Synchronized queue wrapper in QueueUtils.
This commit is contained in:
parent
611c73889b
commit
c6dc370abb
@ -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">
|
||||
|
@ -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.
|
||||
* <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.
|
||||
*
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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<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<>());
|
||||
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user