    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.collections4;
    018
    019import java.util.LinkedList;
    020import java.util.Queue;
    021
    022import org.apache.commons.collections4.queue.PredicatedQueue;
    023import org.apache.commons.collections4.queue.SynchronizedQueue;
    024import org.apache.commons.collections4.queue.TransformedQueue;
    025import org.apache.commons.collections4.queue.UnmodifiableQueue;
    026
    027/**
    028 * Provides utility methods and decorators for {@link Queue} instances.
    029 *
    030 * @since 4.0
    031 */
    032public class QueueUtils {
    033
    034    /**
    035     * An empty unmodifiable queue.
    036     */
    037    @SuppressWarnings("rawtypes") // OK, empty queue is compatible with any type
    038    public static final Queue EMPTY_QUEUE = UnmodifiableQueue.unmodifiableQueue(new LinkedList<>());
    039
    040    /**
    041     * Gets an empty {@code Queue}.
    042     *
    043     * @param <E> The type of the elements in the queue
    044     * @return An empty {@link Queue}
    045     */
    046    @SuppressWarnings("unchecked") // OK, empty queue is compatible with any type
    047    public static <E> Queue<E> emptyQueue() {
    048        return EMPTY_QUEUE;
    049    }
    050
    051    /**
    052     * Returns a predicated (validating) queue backed by the given queue.
    053     * <p>
    054     * Only objects that pass the test in the given predicate can be added to the queue.
    055     * Trying to add an invalid object results in an IllegalArgumentException.
    056     * It is important not to use the original queue after invoking this method,
    057     * as it is a backdoor for adding invalid objects.
    058     * </p>
    059     *
    060     * @param <E> The type of the elements in the queue
    061     * @param queue  The queue to predicate, must not be null
    062     * @param predicate  The predicate used to evaluate new elements, must not be null
    063     * @return A predicated queue
    064     * @throws NullPointerException if the queue or predicate is null
    065     */
    066    public static <E> Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate) {
    067        return PredicatedQueue.predicatedQueue(queue, predicate);
    068    }
    069
    070    /**
    071     * Returns a synchronized (thread-safe) queue backed by the given queue.
    072     * In order to guarantee serial access, it is critical that all access to the
    073     * backing queue is accomplished through the returned queue.
    074     * <p>
    075     * It is imperative that the user manually synchronize on the returned queue
    076     * when iterating over it:
    077     * </p>
    078     * <pre>
    079     * Queue queue = QueueUtils.synchronizedQueue(new CircularFifoQueue());
    080     * ...
    081     * synchronized(queue) {
    082     *     Iterator i = queue.iterator(); // Must be in synchronized block
    083     *     while (i.hasNext())
    084     *         foo(i.next());
    085     *     }
    086     * }
    087     * </pre>
    088     * <p>
    089     * Failure to follow this advice may result in non-deterministic behavior.
    090     * </p>
    091     *
    092     * @param <E> The element type
    093     * @param queue The queue to synchronize, must not be null
    094     * @return A synchronized queue backed by that queue
    095     * @throws NullPointerException if the queue is null
    096     * @since 4.2
    097     */
    098    public static <E> Queue<E> synchronizedQueue(final Queue<E> queue) {
    099        return SynchronizedQueue.synchronizedQueue(queue);
    100    }
    101
    102    /**
    103     * Returns a transformed queue backed by the given queue.
    104     * <p>
    105     * Each object is passed through the transformer as it is added to the
    106     * Queue. It is important not to use the original queue after invoking this
    107     * method, as it is a backdoor for adding untransformed objects.
    108     * </p>
    109     * <p>
    110     * Existing entries in the specified queue will not be transformed.
    111     * If you want that behavior, see {@link TransformedQueue#transformedQueue}.
    112     * </p>
    113     *
    114     * @param <E> The type of the elements in the queue
    115     * @param queue  The queue to predicate, must not be null
    116     * @param transformer  The transformer for the queue, must not be null
    117     * @return A transformed queue backed by the given queue
    118     * @throws NullPointerException if the queue or transformer is null
    119     */
    120    public static <E> Queue<E> transformingQueue(final Queue<E> queue, final Transformer<? super E, ? extends E> transformer) {
    121        return TransformedQueue.transformingQueue(queue, transformer);
    122    }
    123
    124    /**
    125     * Returns an unmodifiable queue backed by the given queue.
    126     *
    127     * @param <E> The type of the elements in the queue
    128     * @param queue  The queue to make unmodifiable, must not be null
    129     * @return An unmodifiable queue backed by that queue
    130     * @throws NullPointerException if the queue is null
    131     */
    132    public static <E> Queue<E> unmodifiableQueue(final Queue<? extends E> queue) {
    133        return UnmodifiableQueue.unmodifiableQueue(queue);
    134    }
    135
    136    /**
    137     * Don't allow instances.
    138     */
    139    private QueueUtils() {
    140        // empty
    141    }
    142}