PredicatedSortedSet.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.set;

  18. import java.util.Comparator;
  19. import java.util.SortedSet;

  20. import org.apache.commons.collections4.Predicate;

  21. /**
  22.  * Decorates another {@code SortedSet} to validate that all additions
  23.  * match a specified predicate.
  24.  * <p>
  25.  * This set exists to provide validation for the decorated set.
  26.  * It is normally created to decorate an empty set.
  27.  * If an object cannot be added to the set, an IllegalArgumentException is thrown.
  28.  * </p>
  29.  * <p>
  30.  * One usage would be to ensure that no null entries are added to the set.
  31.  * </p>
  32.  * <pre>
  33.  * SortedSet set =
  34.  *   PredicatedSortedSet.predicatedSortedSet(new TreeSet(),
  35.  *                                           NotNullPredicate.notNullPredicate());
  36.  * </pre>
  37.  * <p>
  38.  * This class is Serializable from Commons Collections 3.1.
  39.  * </p>
  40.  *
  41.  * @param <E> the type of the elements in this set
  42.  * @since 3.0
  43.  */
  44. public class PredicatedSortedSet<E> extends PredicatedSet<E> implements SortedSet<E> {

  45.     /** Serialization version */
  46.     private static final long serialVersionUID = -9110948148132275052L;

  47.     /**
  48.      * Factory method to create a predicated (validating) sorted set.
  49.      * <p>
  50.      * If there are any elements already in the set being decorated, they
  51.      * are validated.
  52.      *
  53.      * @param <E> the element type
  54.      * @param set  the set to decorate, must not be null
  55.      * @param predicate  the predicate to use for validation, must not be null
  56.      * @return a new predicated sorted set.
  57.      * @throws NullPointerException if set or predicate is null
  58.      * @throws IllegalArgumentException if the set contains invalid elements
  59.      * @since 4.0
  60.      */
  61.     public static <E> PredicatedSortedSet<E> predicatedSortedSet(final SortedSet<E> set,
  62.                                                                  final Predicate<? super E> predicate) {
  63.         return new PredicatedSortedSet<>(set, predicate);
  64.     }

  65.     /**
  66.      * Constructor that wraps (not copies).
  67.      * <p>
  68.      * If there are any elements already in the set being decorated, they
  69.      * are validated.
  70.      *
  71.      * @param set  the set to decorate, must not be null
  72.      * @param predicate  the predicate to use for validation, must not be null
  73.      * @throws NullPointerException if set or predicate is null
  74.      * @throws IllegalArgumentException if the set contains invalid elements
  75.      */
  76.     protected PredicatedSortedSet(final SortedSet<E> set, final Predicate<? super E> predicate) {
  77.         super(set, predicate);
  78.     }

  79.     @Override
  80.     public Comparator<? super E> comparator() {
  81.         return decorated().comparator();
  82.     }

  83.     /**
  84.      * Gets the sorted set being decorated.
  85.      *
  86.      * @return the decorated sorted set
  87.      */
  88.     @Override
  89.     protected SortedSet<E> decorated() {
  90.         return (SortedSet<E>) super.decorated();
  91.     }

  92.     @Override
  93.     public E first() {
  94.         return decorated().first();
  95.     }

  96.     @Override
  97.     public SortedSet<E> headSet(final E toElement) {
  98.         final SortedSet<E> head = decorated().headSet(toElement);
  99.         return new PredicatedSortedSet<>(head, predicate);
  100.     }

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

  105.     @Override
  106.     public SortedSet<E> subSet(final E fromElement, final E toElement) {
  107.         final SortedSet<E> sub = decorated().subSet(fromElement, toElement);
  108.         return new PredicatedSortedSet<>(sub, predicate);
  109.     }

  110.     @Override
  111.     public SortedSet<E> tailSet(final E fromElement) {
  112.         final SortedSet<E> tail = decorated().tailSet(fromElement);
  113.         return new PredicatedSortedSet<>(tail, predicate);
  114.     }

  115. }