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 * http://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 * 059 * @param <E> the type of the elements in the queue 060 * @param queue the queue to predicate, must not be null 061 * @param predicate the predicate used to evaluate new elements, must not be null 062 * @return a predicated queue 063 * @throws NullPointerException if the queue or predicate is null 064 */ 065 public static <E> Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate) { 066 return PredicatedQueue.predicatedQueue(queue, predicate); 067 } 068 069 /** 070 * Returns a synchronized (thread-safe) queue backed by the given queue. 071 * In order to guarantee serial access, it is critical that all access to the 072 * backing queue is accomplished through the returned queue. 073 * <p> 074 * It is imperative that the user manually synchronize on the returned queue 075 * when iterating over it: 076 * 077 * <pre> 078 * Queue queue = QueueUtils.synchronizedQueue(new CircularFifoQueue()); 079 * ... 080 * synchronized(queue) { 081 * Iterator i = queue.iterator(); // Must be in synchronized block 082 * while (i.hasNext()) 083 * foo(i.next()); 084 * } 085 * } 086 * </pre> 087 * 088 * Failure to follow this advice may result in non-deterministic behavior. 089 * 090 * @param <E> the element type 091 * @param queue the queue to synchronize, must not be null 092 * @return a synchronized queue backed by that queue 093 * @throws NullPointerException if the queue is null 094 * @since 4.2 095 */ 096 public static <E> Queue<E> synchronizedQueue(final Queue<E> queue) { 097 return SynchronizedQueue.synchronizedQueue(queue); 098 } 099 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, final Transformer<? super E, ? extends E> transformer) { 117 return TransformedQueue.transformingQueue(queue, transformer); 118 } 119 120 /** 121 * Returns an unmodifiable queue backed by the given queue. 122 * 123 * @param <E> the type of the elements in the queue 124 * @param queue the queue to make unmodifiable, must not be null 125 * @return an unmodifiable queue backed by that queue 126 * @throws NullPointerException if the queue is null 127 */ 128 public static <E> Queue<E> unmodifiableQueue(final Queue<? extends E> queue) { 129 return UnmodifiableQueue.unmodifiableQueue(queue); 130 } 131 132 /** 133 * Don't allow instances. 134 */ 135 private QueueUtils() { 136 // empty 137 } 138}