    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.queue;
    018
    019import java.util.Queue;
    020
    021import org.apache.commons.collections4.Predicate;
    022import org.apache.commons.collections4.collection.PredicatedCollection;
    023
    024/**
    025 * Decorates another {@link Queue} to validate that additions
    026 * match a specified predicate.
    027 * <p>
    028 * This queue exists to provide validation for the decorated queue.
    029 * It is normally created to decorate an empty queue.
    030 * If an object cannot be added to the queue, an IllegalArgumentException is thrown.
    031 * <p>
    032 * One usage would be to ensure that no null entries are added to the queue.
    033 * <pre>Queue queue = PredicatedQueue.predicatedQueue(new UnboundedFifoQueue(), NotNullPredicate.INSTANCE);</pre>
    034 *
    035 * @param <E> the type of elements held in this queue
    036 * @since 4.0
    037 */
    038public class PredicatedQueue<E> extends PredicatedCollection<E> implements Queue<E> {
    039
    040    /** Serialization version */
    041    private static final long serialVersionUID = 2307609000539943581L;
    042
    043    /**
    044     * Factory method to create a predicated (validating) queue.
    045     * <p>
    046     * If there are any elements already in the queue being decorated, they
    047     * are validated.
    048     *
    049     * @param <E> the type of the elements in the queue
    050     * @param Queue  the queue to decorate, must not be null
    051     * @param predicate  the predicate to use for validation, must not be null
    052     * @return a new predicated queue
    053     * @throws NullPointerException if queue or predicate is null
    054     * @throws IllegalArgumentException if the queue contains invalid elements
    055     */
    056    public static <E> PredicatedQueue<E> predicatedQueue(final Queue<E> Queue,
    057                                                          final Predicate<? super E> predicate) {
    058        return new PredicatedQueue<>(Queue, predicate);
    059    }
    060
    061    //-----------------------------------------------------------------------
    062    /**
    063     * Constructor that wraps (not copies).
    064     * <p>
    065     * If there are any elements already in the collection being decorated, they
    066     * are validated.
    067     *
    068     * @param queue  the queue to decorate, must not be null
    069     * @param predicate  the predicate to use for validation, must not be null
    070     * @throws NullPointerException if queue or predicate is null
    071     * @throws IllegalArgumentException if the Queue contains invalid elements
    072     */
    073    protected PredicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate) {
    074        super(queue, predicate);
    075    }
    076
    077    /**
    078     * Gets the queue being decorated.
    079     *
    080     * @return the decorated queue
    081     */
    082    @Override
    083    protected Queue<E> decorated() {
    084        return (Queue<E>) super.decorated();
    085    }
    086
    087    //-----------------------------------------------------------------------
    088
    089    /**
    090     * Override to validate the object being added to ensure it matches
    091     * the predicate.
    092     *
    093     * @param object  the object being added
    094     * @return the result of adding to the underlying queue
    095     * @throws IllegalArgumentException if the add is invalid
    096     */
    097    @Override
    098    public boolean offer(final E object) {
    099        validate(object);
    100        return decorated().offer(object);
    101    }
    102
    103    @Override
    104    public E poll() {
    105        return decorated().poll();
    106    }
    107
    108    @Override
    109    public E peek() {
    110        return decorated().peek();
    111    }
    112
    113    @Override
    114    public E element() {
    115        return decorated().element();
    116    }
    117
    118    @Override
    119    public E remove() {
    120        return decorated().remove();
    121    }
    122
    123}