PredicatedNavigableSet.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.Iterator;
  19. import java.util.NavigableSet;

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

  21. /**
  22.  * Decorates another {@code NavigableSet} 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.  * NavigableSet set =
  34.  *   PredicatedSortedSet.predicatedNavigableSet(new TreeSet(),
  35.  *                                              NotNullPredicate.notNullPredicate());
  36.  * </pre>
  37.  *
  38.  * @param <E> the type of the elements in this set
  39.  * @since 4.1
  40.  */
  41. public class PredicatedNavigableSet<E> extends PredicatedSortedSet<E> implements NavigableSet<E> {

  42.     /** Serialization version */
  43.     private static final long serialVersionUID = 20150528L;

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

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

  76.     @Override
  77.     public E ceiling(final E e) {
  78.         return decorated().ceiling(e);
  79.     }

  80.     /**
  81.      * Gets the navigable set being decorated.
  82.      *
  83.      * @return the decorated navigable set
  84.      */
  85.     @Override
  86.     protected NavigableSet<E> decorated() {
  87.         return (NavigableSet<E>) super.decorated();
  88.     }

  89.     @Override
  90.     public Iterator<E> descendingIterator() {
  91.         return decorated().descendingIterator();
  92.     }

  93.     @Override
  94.     public NavigableSet<E> descendingSet() {
  95.         return predicatedNavigableSet(decorated().descendingSet(), predicate);
  96.     }

  97.     @Override
  98.     public E floor(final E e) {
  99.         return decorated().floor(e);
  100.     }

  101.     @Override
  102.     public NavigableSet<E> headSet(final E toElement, final boolean inclusive) {
  103.         final NavigableSet<E> head = decorated().headSet(toElement, inclusive);
  104.         return predicatedNavigableSet(head, predicate);
  105.     }

  106.     @Override
  107.     public E higher(final E e) {
  108.         return decorated().higher(e);
  109.     }

  110.     @Override
  111.     public E lower(final E e) {
  112.         return decorated().lower(e);
  113.     }

  114.     @Override
  115.     public E pollFirst() {
  116.         return decorated().pollFirst();
  117.     }

  118.     @Override
  119.     public E pollLast() {
  120.         return decorated().pollLast();
  121.     }

  122.     @Override
  123.     public NavigableSet<E> subSet(final E fromElement, final boolean fromInclusive, final E toElement,
  124.             final boolean toInclusive) {
  125.         final NavigableSet<E> sub = decorated().subSet(fromElement, fromInclusive, toElement, toInclusive);
  126.         return predicatedNavigableSet(sub, predicate);
  127.     }

  128.     @Override
  129.     public NavigableSet<E> tailSet(final E fromElement, final boolean inclusive) {
  130.         final NavigableSet<E> tail = decorated().tailSet(fromElement, inclusive);
  131.         return predicatedNavigableSet(tail, predicate);
  132.     }

  133. }