QueueUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4;

  18. import java.util.LinkedList;
  19. import java.util.Queue;

  20. import org.apache.commons.collections4.queue.PredicatedQueue;
  21. import org.apache.commons.collections4.queue.SynchronizedQueue;
  22. import org.apache.commons.collections4.queue.TransformedQueue;
  23. import org.apache.commons.collections4.queue.UnmodifiableQueue;

  24. /**
  25.  * Provides utility methods and decorators for {@link Queue} instances.
  26.  *
  27.  * @since 4.0
  28.  */
  29. public class QueueUtils {

  30.     /**
  31.      * An empty unmodifiable queue.
  32.      */
  33.     @SuppressWarnings("rawtypes") // OK, empty queue is compatible with any type
  34.     public static final Queue EMPTY_QUEUE = UnmodifiableQueue.unmodifiableQueue(new LinkedList<>());

  35.     /**
  36.      * Gets an empty {@code Queue}.
  37.      *
  38.      * @param <E> the type of the elements in the queue
  39.      * @return an empty {@link Queue}
  40.      */
  41.     @SuppressWarnings("unchecked") // OK, empty queue is compatible with any type
  42.     public static <E> Queue<E> emptyQueue() {
  43.         return EMPTY_QUEUE;
  44.     }

  45.     /**
  46.      * Returns a predicated (validating) queue backed by the given queue.
  47.      * <p>
  48.      * Only objects that pass the test in the given predicate can be added to the queue.
  49.      * Trying to add an invalid object results in an IllegalArgumentException.
  50.      * It is important not to use the original queue after invoking this method,
  51.      * as it is a backdoor for adding invalid objects.
  52.      * </p>
  53.      *
  54.      * @param <E> the type of the elements in the queue
  55.      * @param queue  the queue to predicate, must not be null
  56.      * @param predicate  the predicate used to evaluate new elements, must not be null
  57.      * @return a predicated queue
  58.      * @throws NullPointerException if the queue or predicate is null
  59.      */
  60.     public static <E> Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate) {
  61.         return PredicatedQueue.predicatedQueue(queue, predicate);
  62.     }

  63.     /**
  64.      * Returns a synchronized (thread-safe) queue backed by the given queue.
  65.      * In order to guarantee serial access, it is critical that all access to the
  66.      * backing queue is accomplished through the returned queue.
  67.      * <p>
  68.      * It is imperative that the user manually synchronize on the returned queue
  69.      * when iterating over it:
  70.      * </p>
  71.      * <pre>
  72.      * Queue queue = QueueUtils.synchronizedQueue(new CircularFifoQueue());
  73.      * ...
  74.      * synchronized(queue) {
  75.      *     Iterator i = queue.iterator(); // Must be in synchronized block
  76.      *     while (i.hasNext())
  77.      *         foo(i.next());
  78.      *     }
  79.      * }
  80.      * </pre>
  81.      * <p>
  82.      * Failure to follow this advice may result in non-deterministic behavior.
  83.      * </p>
  84.      *
  85.      * @param <E> the element type
  86.      * @param queue the queue to synchronize, must not be null
  87.      * @return a synchronized queue backed by that queue
  88.      * @throws NullPointerException if the queue is null
  89.      * @since 4.2
  90.      */
  91.     public static <E> Queue<E> synchronizedQueue(final Queue<E> queue) {
  92.         return SynchronizedQueue.synchronizedQueue(queue);
  93.     }

  94.     /**
  95.      * Returns a transformed queue backed by the given queue.
  96.      * <p>
  97.      * Each object is passed through the transformer as it is added to the
  98.      * Queue. It is important not to use the original queue after invoking this
  99.      * method, as it is a backdoor for adding untransformed objects.
  100.      * </p>
  101.      * <p>
  102.      * Existing entries in the specified queue will not be transformed.
  103.      * If you want that behavior, see {@link TransformedQueue#transformedQueue}.
  104.      * </p>
  105.      *
  106.      * @param <E> the type of the elements in the queue
  107.      * @param queue  the queue to predicate, must not be null
  108.      * @param transformer  the transformer for the queue, must not be null
  109.      * @return a transformed queue backed by the given queue
  110.      * @throws NullPointerException if the queue or transformer is null
  111.      */
  112.     public static <E> Queue<E> transformingQueue(final Queue<E> queue, final Transformer<? super E, ? extends E> transformer) {
  113.         return TransformedQueue.transformingQueue(queue, transformer);
  114.     }

  115.     /**
  116.      * Returns an unmodifiable queue backed by the given queue.
  117.      *
  118.      * @param <E> the type of the elements in the queue
  119.      * @param queue  the queue to make unmodifiable, must not be null
  120.      * @return an unmodifiable queue backed by that queue
  121.      * @throws NullPointerException if the queue is null
  122.      */
  123.     public static <E> Queue<E> unmodifiableQueue(final Queue<? extends E> queue) {
  124.         return UnmodifiableQueue.unmodifiableQueue(queue);
  125.     }

  126.     /**
  127.      * Don't allow instances.
  128.      */
  129.     private QueueUtils() {
  130.         // empty
  131.     }
  132. }