    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.Comparator;
    020import java.util.SortedSet;
    021
    022import org.apache.commons.collections4.Predicate;
    023
    024/**
    025 * Decorates another {@code SortedSet} 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 * SortedSet set =
    037 *   PredicatedSortedSet.predicatedSortedSet(new TreeSet(),
    038 *                                           NotNullPredicate.notNullPredicate());
    039 * </pre>
    040 * <p>
    041 * This class is Serializable from Commons Collections 3.1.
    042 * </p>
    043 *
    044 * @param <E> the type of the elements in this set
    045 * @since 3.0
    046 */
    047public class PredicatedSortedSet<E> extends PredicatedSet<E> implements SortedSet<E> {
    048
    049    /** Serialization version */
    050    private static final long serialVersionUID = -9110948148132275052L;
    051
    052    /**
    053     * Factory method to create a predicated (validating) sorted set.
    054     * <p>
    055     * If there are any elements already in the set being decorated, they
    056     * are validated.
    057     *
    058     * @param <E> the element type
    059     * @param set  the set to decorate, must not be null
    060     * @param predicate  the predicate to use for validation, must not be null
    061     * @return a new predicated sorted set.
    062     * @throws NullPointerException if set or predicate is null
    063     * @throws IllegalArgumentException if the set contains invalid elements
    064     * @since 4.0
    065     */
    066    public static <E> PredicatedSortedSet<E> predicatedSortedSet(final SortedSet<E> set,
    067                                                                 final Predicate<? super E> predicate) {
    068        return new PredicatedSortedSet<>(set, predicate);
    069    }
    070
    071    /**
    072     * Constructor that wraps (not copies).
    073     * <p>
    074     * If there are any elements already in the set being decorated, they
    075     * are validated.
    076     *
    077     * @param set  the set to decorate, must not be null
    078     * @param predicate  the predicate to use for validation, must not be null
    079     * @throws NullPointerException if set or predicate is null
    080     * @throws IllegalArgumentException if the set contains invalid elements
    081     */
    082    protected PredicatedSortedSet(final SortedSet<E> set, final Predicate<? super E> predicate) {
    083        super(set, predicate);
    084    }
    085
    086    @Override
    087    public Comparator<? super E> comparator() {
    088        return decorated().comparator();
    089    }
    090
    091    /**
    092     * Gets the sorted set being decorated.
    093     *
    094     * @return the decorated sorted set
    095     */
    096    @Override
    097    protected SortedSet<E> decorated() {
    098        return (SortedSet<E>) super.decorated();
    099    }
    100
    101    @Override
    102    public E first() {
    103        return decorated().first();
    104    }
    105
    106    @Override
    107    public SortedSet<E> headSet(final E toElement) {
    108        final SortedSet<E> head = decorated().headSet(toElement);
    109        return new PredicatedSortedSet<>(head, predicate);
    110    }
    111
    112    @Override
    113    public E last() {
    114        return decorated().last();
    115    }
    116
    117    @Override
    118    public SortedSet<E> subSet(final E fromElement, final E toElement) {
    119        final SortedSet<E> sub = decorated().subSet(fromElement, toElement);
    120        return new PredicatedSortedSet<>(sub, predicate);
    121    }
    122
    123    @Override
    124    public SortedSet<E> tailSet(final E fromElement) {
    125        final SortedSet<E> tail = decorated().tailSet(fromElement);
    126        return new PredicatedSortedSet<>(tail, predicate);
    127    }
    128
    129}