PredicatedQueue.java

  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. import java.util.Queue;

  19. import org.apache.commons.collections4.Predicate;
  20. import org.apache.commons.collections4.collection.PredicatedCollection;

  21. /**
  22.  * Decorates another {@link Queue} to validate that additions
  23.  * match a specified predicate.
  24.  * <p>
  25.  * This queue exists to provide validation for the decorated queue.
  26.  * It is normally created to decorate an empty queue.
  27.  * If an object cannot be added to the queue, an IllegalArgumentException is thrown.
  28.  * </p>
  29.  * <p>
  30.  * One usage would be to ensure that no null entries are added to the queue.
  31.  * </p>
  32.  * <pre>Queue queue = PredicatedQueue.predicatedQueue(new UnboundedFifoQueue(), NotNullPredicate.INSTANCE);</pre>
  33.  *
  34.  * @param <E> the type of elements held in this queue
  35.  * @since 4.0
  36.  */
  37. public class PredicatedQueue<E> extends PredicatedCollection<E> implements Queue<E> {

  38.     /** Serialization version */
  39.     private static final long serialVersionUID = 2307609000539943581L;

  40.     /**
  41.      * Factory method to create a predicated (validating) queue.
  42.      * <p>
  43.      * If there are any elements already in the queue being decorated, they
  44.      * are validated.
  45.      *
  46.      * @param <E> the type of the elements in the queue
  47.      * @param Queue  the queue to decorate, must not be null
  48.      * @param predicate  the predicate to use for validation, must not be null
  49.      * @return a new predicated queue
  50.      * @throws NullPointerException if queue or predicate is null
  51.      * @throws IllegalArgumentException if the queue contains invalid elements
  52.      */
  53.     public static <E> PredicatedQueue<E> predicatedQueue(final Queue<E> Queue,
  54.                                                           final Predicate<? super E> predicate) {
  55.         return new PredicatedQueue<>(Queue, predicate);
  56.     }

  57.     /**
  58.      * Constructor that wraps (not copies).
  59.      * <p>
  60.      * If there are any elements already in the collection being decorated, they
  61.      * are validated.
  62.      *
  63.      * @param queue  the queue to decorate, must not be null
  64.      * @param predicate  the predicate to use for validation, must not be null
  65.      * @throws NullPointerException if queue or predicate is null
  66.      * @throws IllegalArgumentException if the Queue contains invalid elements
  67.      */
  68.     protected PredicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate) {
  69.         super(queue, predicate);
  70.     }

  71.     /**
  72.      * Gets the queue being decorated.
  73.      *
  74.      * @return the decorated queue
  75.      */
  76.     @Override
  77.     protected Queue<E> decorated() {
  78.         return (Queue<E>) super.decorated();
  79.     }

  80.     @Override
  81.     public E element() {
  82.         return decorated().element();
  83.     }

  84.     /**
  85.      * Override to validate the object being added to ensure it matches
  86.      * the predicate.
  87.      *
  88.      * @param object  the object being added
  89.      * @return the result of adding to the underlying queue
  90.      * @throws IllegalArgumentException if the add is invalid
  91.      */
  92.     @Override
  93.     public boolean offer(final E object) {
  94.         validate(object);
  95.         return decorated().offer(object);
  96.     }

  97.     @Override
  98.     public E peek() {
  99.         return decorated().peek();
  100.     }

  101.     @Override
  102.     public E poll() {
  103.         return decorated().poll();
  104.     }

  105.     @Override
  106.     public E remove() {
  107.         return decorated().remove();
  108.     }

  109. }