/** * Factory method to create a predicated (validating) queue. * <p> * If there are any elements already in the queue being decorated, they * are validated. * * @param <E> the type of the elements in the queue * @param Queue the queue to decorate, must not be null * @param predicate the predicate to use for validation, must not be null * @return a new predicated queue * @throws NullPointerException if queue or predicate is null * @throws IllegalArgumentException if the queue contains invalid elements */ public static <E> PredicatedQueue<E> predicatedQueue(final Queue<E> Queue, final Predicate<? super E> predicate) { return new PredicatedQueue<>(Queue, predicate); }
/** * Override to validate the object being added to ensure it matches * the predicate. * * @param object the object being added * @return the result of adding to the underlying queue * @throws IllegalArgumentException if the add is invalid */ @Override public boolean offer(final E object) { validate(object); return decorated().offer(object); }
/** * Returns a predicated (validating) queue backed by the given queue. * <p> * Only objects that pass the test in the given predicate can be added to the queue. * Trying to add an invalid object results in an IllegalArgumentException. * It is important not to use the original queue after invoking this method, * as it is a backdoor for adding invalid objects. * * @param <E> the type of the elements in the queue * @param queue the queue to predicate, must not be null * @param predicate the predicate used to evaluate new elements, must not be null * @return a predicated queue * @throws NullPointerException if the queue or predicate is null */ public static <E> Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate) { return PredicatedQueue.predicatedQueue(queue, predicate); }
/** * Decorates the given queue with validating behavior using the predicate. All accepted elements * are appended to the queue. If the queue already contains elements, they are validated. * <p> * The builder is not modified by this method, so it is possible to create more collections * or add more elements afterwards. Further changes will not propagate to the returned queue. * * @param queue the queue to decorate, must not be null * @return the decorated queue. * @throws NullPointerException if queue is null * @throws IllegalArgumentException if queue contains invalid elements */ public Queue<E> createPredicatedQueue(final Queue<E> queue) { if (queue == null) { throw new NullPointerException("queue must not be null"); } final PredicatedQueue<E> predicatedQueue = PredicatedQueue.predicatedQueue(queue, predicate); predicatedQueue.addAll(accepted); return predicatedQueue; }
@Override public E peek() { return decorated().peek(); }
@Override public E remove() { return decorated().remove(); }
@Override public E poll() { return decorated().poll(); }
@Override public E element() { return decorated().element(); }