    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.set;
    018
    019import java.util.Iterator;
    020import java.util.NavigableSet;
    021
    022import org.apache.commons.collections4.Predicate;
    023
    024/**
    025 * Decorates another {@code NavigableSet} to validate that all additions
    026 * match a specified predicate.
    027 * <p>
    028 * This set exists to provide validation for the decorated set.
    029 * It is normally created to decorate an empty set.
    030 * If an object cannot be added to the set, an IllegalArgumentException is thrown.
    031 * </p>
    032 * <p>
    033 * One usage would be to ensure that no null entries are added to the set.
    034 * </p>
    035 * <pre>
    036 * NavigableSet set =
    037 *   PredicatedSortedSet.predicatedNavigableSet(new TreeSet(),
    038 *                                              NotNullPredicate.notNullPredicate());
    039 * </pre>
    040 *
    041 * @param <E> the type of the elements in this set
    042 * @since 4.1
    043 */
    044public class PredicatedNavigableSet<E> extends PredicatedSortedSet<E> implements NavigableSet<E> {
    045
    046    /** Serialization version */
    047    private static final long serialVersionUID = 20150528L;
    048
    049    /**
    050     * Factory method to create a predicated (validating) navigable set.
    051     * <p>
    052     * If there are any elements already in the set being decorated, they
    053     * are validated.
    054     *
    055     * @param <E> the element type
    056     * @param set  the set to decorate, must not be null
    057     * @param predicate  the predicate to use for validation, must not be null
    058     * @return a new predicated navigable set.
    059     * @throws NullPointerException if set or predicate is null
    060     * @throws IllegalArgumentException if the set contains invalid elements
    061     * @since 4.0
    062     */
    063    public static <E> PredicatedNavigableSet<E> predicatedNavigableSet(final NavigableSet<E> set,
    064                                                                       final Predicate<? super E> predicate) {
    065        return new PredicatedNavigableSet<>(set, predicate);
    066    }
    067
    068    /**
    069     * Constructor that wraps (not copies).
    070     * <p>
    071     * If there are any elements already in the set being decorated, they
    072     * are validated.
    073     *
    074     * @param set  the set to decorate, must not be null
    075     * @param predicate  the predicate to use for validation, must not be null
    076     * @throws NullPointerException if set or predicate is null
    077     * @throws IllegalArgumentException if the set contains invalid elements
    078     */
    079    protected PredicatedNavigableSet(final NavigableSet<E> set, final Predicate<? super E> predicate) {
    080        super(set, predicate);
    081    }
    082
    083    @Override
    084    public E ceiling(final E e) {
    085        return decorated().ceiling(e);
    086    }
    087
    088    /**
    089     * Gets the navigable set being decorated.
    090     *
    091     * @return the decorated navigable set
    092     */
    093    @Override
    094    protected NavigableSet<E> decorated() {
    095        return (NavigableSet<E>) super.decorated();
    096    }
    097
    098    @Override
    099    public Iterator<E> descendingIterator() {
    100        return decorated().descendingIterator();
    101    }
    102
    103    @Override
    104    public NavigableSet<E> descendingSet() {
    105        return predicatedNavigableSet(decorated().descendingSet(), predicate);
    106    }
    107
    108    @Override
    109    public E floor(final E e) {
    110        return decorated().floor(e);
    111    }
    112
    113    @Override
    114    public NavigableSet<E> headSet(final E toElement, final boolean inclusive) {
    115        final NavigableSet<E> head = decorated().headSet(toElement, inclusive);
    116        return predicatedNavigableSet(head, predicate);
    117    }
    118
    119    @Override
    120    public E higher(final E e) {
    121        return decorated().higher(e);
    122    }
    123
    124    @Override
    125    public E lower(final E e) {
    126        return decorated().lower(e);
    127    }
    128
    129    @Override
    130    public E pollFirst() {
    131        return decorated().pollFirst();
    132    }
    133
    134    @Override
    135    public E pollLast() {
    136        return decorated().pollLast();
    137    }
    138
    139    @Override
    140    public NavigableSet<E> subSet(final E fromElement, final boolean fromInclusive, final E toElement,
    141            final boolean toInclusive) {
    142        final NavigableSet<E> sub = decorated().subSet(fromElement, fromInclusive, toElement, toInclusive);
    143        return predicatedNavigableSet(sub, predicate);
    144    }
    145
    146    @Override
    147    public NavigableSet<E> tailSet(final E fromElement, final boolean inclusive) {
    148        final NavigableSet<E> tail = decorated().tailSet(fromElement, inclusive);
    149        return predicatedNavigableSet(tail, predicate);
    150    }
    151
    152}