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 19 import java.util.Comparator; 20 import java.util.SortedSet; 21 22 import org.apache.commons.collections4.Predicate; 23 24 /** 25 * Decorates another {@code SortedSet} to validate that all additions 26 * match a specified predicate. 27 * <p> 28 * This set exists to provide validation for the decorated set. 29 * It is normally created to decorate an empty set. 30 * If an object cannot be added to the set, an IllegalArgumentException is thrown. 31 * </p> 32 * <p> 33 * One usage would be to ensure that no null entries are added to the set. 34 * </p> 35 * <pre> 36 * SortedSet set = 37 * PredicatedSortedSet.predicatedSortedSet(new TreeSet(), 38 * NotNullPredicate.notNullPredicate()); 39 * </pre> 40 * <p> 41 * This class is Serializable from Commons Collections 3.1. 42 * </p> 43 * 44 * @param <E> the type of the elements in this set 45 * @since 3.0 46 */ 47 public class PredicatedSortedSet<E> extends PredicatedSet<E> implements SortedSet<E> { 48 49 /** Serialization version */ 50 private static final long serialVersionUID = -9110948148132275052L; 51 52 /** 53 * Factory method to create a predicated (validating) sorted set. 54 * <p> 55 * If there are any elements already in the set being decorated, they 56 * are validated. 57 * 58 * @param <E> the element type 59 * @param set the set to decorate, must not be null 60 * @param predicate the predicate to use for validation, must not be null 61 * @return a new predicated sorted set. 62 * @throws NullPointerException if set or predicate is null 63 * @throws IllegalArgumentException if the set contains invalid elements 64 * @since 4.0 65 */ 66 public static <E> PredicatedSortedSet<E> predicatedSortedSet(final SortedSet<E> set, 67 final Predicate<? super E> predicate) { 68 return new PredicatedSortedSet<>(set, predicate); 69 } 70 71 /** 72 * Constructor that wraps (not copies). 73 * <p> 74 * If there are any elements already in the set being decorated, they 75 * are validated. 76 * 77 * @param set the set to decorate, must not be null 78 * @param predicate the predicate to use for validation, must not be null 79 * @throws NullPointerException if set or predicate is null 80 * @throws IllegalArgumentException if the set contains invalid elements 81 */ 82 protected PredicatedSortedSet(final SortedSet<E> set, final Predicate<? super E> predicate) { 83 super(set, predicate); 84 } 85 86 @Override 87 public Comparator<? super E> comparator() { 88 return decorated().comparator(); 89 } 90 91 /** 92 * Gets the sorted set being decorated. 93 * 94 * @return the decorated sorted set 95 */ 96 @Override 97 protected SortedSet<E> decorated() { 98 return (SortedSet<E>) super.decorated(); 99 } 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 }