View Javadoc
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  
19  import java.util.LinkedList;
20  import java.util.Queue;
21  
22  import org.apache.commons.collections4.queue.PredicatedQueue;
23  import org.apache.commons.collections4.queue.SynchronizedQueue;
24  import org.apache.commons.collections4.queue.TransformedQueue;
25  import org.apache.commons.collections4.queue.UnmodifiableQueue;
26  
27  /**
28   * Provides utility methods and decorators for {@link Queue} instances.
29   *
30   * @since 4.0
31   */
32  public class QueueUtils {
33  
34      /**
35       * An empty unmodifiable queue.
36       */
37      @SuppressWarnings("rawtypes") // OK, empty queue is compatible with any type
38      public static final Queue EMPTY_QUEUE = UnmodifiableQueue.unmodifiableQueue(new LinkedList<>());
39  
40      /**
41       * Gets an empty {@code Queue}.
42       *
43       * @param <E> the type of the elements in the queue
44       * @return an empty {@link Queue}
45       */
46      @SuppressWarnings("unchecked") // OK, empty queue is compatible with any type
47      public static <E> Queue<E> emptyQueue() {
48          return EMPTY_QUEUE;
49      }
50  
51      /**
52       * Returns a predicated (validating) queue backed by the given queue.
53       * <p>
54       * Only objects that pass the test in the given predicate can be added to the queue.
55       * Trying to add an invalid object results in an IllegalArgumentException.
56       * It is important not to use the original queue after invoking this method,
57       * as it is a backdoor for adding invalid objects.
58       *
59       * @param <E> the type of the elements in the queue
60       * @param queue  the queue to predicate, must not be null
61       * @param predicate  the predicate used to evaluate new elements, must not be null
62       * @return a predicated queue
63       * @throws NullPointerException if the queue or predicate is null
64       */
65      public static <E> Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate) {
66          return PredicatedQueue.predicatedQueue(queue, predicate);
67      }
68  
69      /**
70       * Returns a synchronized (thread-safe) queue backed by the given queue.
71       * In order to guarantee serial access, it is critical that all access to the
72       * backing queue is accomplished through the returned queue.
73       * <p>
74       * It is imperative that the user manually synchronize on the returned queue
75       * when iterating over it:
76       *
77       * <pre>
78       * Queue queue = QueueUtils.synchronizedQueue(new CircularFifoQueue());
79       * ...
80       * synchronized(queue) {
81       *     Iterator i = queue.iterator(); // Must be in synchronized block
82       *     while (i.hasNext())
83       *         foo(i.next());
84       *     }
85       * }
86       * </pre>
87       *
88       * Failure to follow this advice may result in non-deterministic behavior.
89       *
90       * @param <E> the element type
91       * @param queue the queue to synchronize, must not be null
92       * @return a synchronized queue backed by that queue
93       * @throws NullPointerException if the queue is null
94       * @since 4.2
95       */
96      public static <E> Queue<E> synchronizedQueue(final Queue<E> queue) {
97          return SynchronizedQueue.synchronizedQueue(queue);
98      }
99  
100     /**
101      * Returns a transformed queue backed by the given queue.
102      * <p>
103      * Each object is passed through the transformer as it is added to the
104      * Queue. It is important not to use the original queue after invoking this
105      * method, as it is a backdoor for adding untransformed objects.
106      * <p>
107      * Existing entries in the specified queue will not be transformed.
108      * If you want that behavior, see {@link TransformedQueue#transformedQueue}.
109      *
110      * @param <E> the type of the elements in the queue
111      * @param queue  the queue to predicate, must not be null
112      * @param transformer  the transformer for the queue, must not be null
113      * @return a transformed queue backed by the given queue
114      * @throws NullPointerException if the queue or transformer is null
115      */
116     public static <E> Queue<E> transformingQueue(final Queue<E> queue,
117                                                  final Transformer<? super E, ? extends E> transformer) {
118         return TransformedQueue.transformingQueue(queue, transformer);
119     }
120 
121     /**
122      * Returns an unmodifiable queue backed by the given queue.
123      *
124      * @param <E> the type of the elements in the queue
125      * @param queue  the queue to make unmodifiable, must not be null
126      * @return an unmodifiable queue backed by that queue
127      * @throws NullPointerException if the queue is null
128      */
129     public static <E> Queue<E> unmodifiableQueue(final Queue<? extends E> queue) {
130         return UnmodifiableQueue.unmodifiableQueue(queue);
131     }
132 
133     /**
134      * Don't allow instances.
135      */
136     private QueueUtils() {}
137 }