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