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.queue; 18 19 import java.util.Queue; 20 21 import org.apache.commons.collections4.Predicate; 22 import org.apache.commons.collections4.collection.PredicatedCollection; 23 24 /** 25 * Decorates another {@link Queue} to validate that additions 26 * match a specified predicate. 27 * <p> 28 * This queue exists to provide validation for the decorated queue. 29 * It is normally created to decorate an empty queue. 30 * If an object cannot be added to the queue, an IllegalArgumentException is thrown. 31 * </p> 32 * <p> 33 * One usage would be to ensure that no null entries are added to the queue. 34 * </p> 35 * <pre>Queue queue = PredicatedQueue.predicatedQueue(new UnboundedFifoQueue(), NotNullPredicate.INSTANCE);</pre> 36 * 37 * @param <E> the type of elements held in this queue 38 * @since 4.0 39 */ 40 public class PredicatedQueue<E> extends PredicatedCollection<E> implements Queue<E> { 41 42 /** Serialization version */ 43 private static final long serialVersionUID = 2307609000539943581L; 44 45 /** 46 * Factory method to create a predicated (validating) queue. 47 * <p> 48 * If there are any elements already in the queue being decorated, they 49 * are validated. 50 * 51 * @param <E> the type of the elements in the queue 52 * @param Queue the queue to decorate, must not be null 53 * @param predicate the predicate to use for validation, must not be null 54 * @return a new predicated queue 55 * @throws NullPointerException if queue or predicate is null 56 * @throws IllegalArgumentException if the queue contains invalid elements 57 */ 58 public static <E> PredicatedQueue<E> predicatedQueue(final Queue<E> Queue, 59 final Predicate<? super E> predicate) { 60 return new PredicatedQueue<>(Queue, predicate); 61 } 62 63 /** 64 * Constructor that wraps (not copies). 65 * <p> 66 * If there are any elements already in the collection being decorated, they 67 * are validated. 68 * 69 * @param queue the queue to decorate, must not be null 70 * @param predicate the predicate to use for validation, must not be null 71 * @throws NullPointerException if queue or predicate is null 72 * @throws IllegalArgumentException if the Queue contains invalid elements 73 */ 74 protected PredicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate) { 75 super(queue, predicate); 76 } 77 78 /** 79 * Gets the queue being decorated. 80 * 81 * @return the decorated queue 82 */ 83 @Override 84 protected Queue<E> decorated() { 85 return (Queue<E>) super.decorated(); 86 } 87 88 @Override 89 public E element() { 90 return decorated().element(); 91 } 92 93 /** 94 * Override to validate the object being added to ensure it matches 95 * the predicate. 96 * 97 * @param object the object being added 98 * @return the result of adding to the underlying queue 99 * @throws IllegalArgumentException if the add is invalid 100 */ 101 @Override 102 public boolean offer(final E object) { 103 validate(object); 104 return decorated().offer(object); 105 } 106 107 @Override 108 public E peek() { 109 return decorated().peek(); 110 } 111 112 @Override 113 public E poll() { 114 return decorated().poll(); 115 } 116 117 @Override 118 public E remove() { 119 return decorated().remove(); 120 } 121 122 }