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.Iterator; 020import java.util.NavigableSet; 021 022import org.apache.commons.collections4.Predicate; 023 024/** 025 * Decorates another <code>NavigableSet</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 * NavigableSet set = 035 * PredicatedSortedSet.predicatedNavigableSet(new TreeSet(), 036 * NotNullPredicate.notNullPredicate()); 037 * </pre> 038 * 039 * @param <E> the type of the elements in this set 040 * @since 4.1 041 */ 042public class PredicatedNavigableSet<E> extends PredicatedSortedSet<E> implements NavigableSet<E> { 043 044 /** Serialization version */ 045 private static final long serialVersionUID = 20150528L; 046 047 /** 048 * Factory method to create a predicated (validating) navigable set. 049 * <p> 050 * If there are any elements already in the set being decorated, they 051 * are validated. 052 * 053 * @param <E> the element type 054 * @param set the set to decorate, must not be null 055 * @param predicate the predicate to use for validation, must not be null 056 * @return a new predicated navigable set. 057 * @throws NullPointerException if set or predicate is null 058 * @throws IllegalArgumentException if the set contains invalid elements 059 * @since 4.0 060 */ 061 public static <E> PredicatedNavigableSet<E> predicatedNavigableSet(final NavigableSet<E> set, 062 final Predicate<? super E> predicate) { 063 return new PredicatedNavigableSet<>(set, predicate); 064 } 065 066 //----------------------------------------------------------------------- 067 /** 068 * Constructor that wraps (not copies). 069 * <p> 070 * If there are any elements already in the set being decorated, they 071 * are validated. 072 * 073 * @param set the set to decorate, must not be null 074 * @param predicate the predicate to use for validation, must not be null 075 * @throws NullPointerException if set or predicate is null 076 * @throws IllegalArgumentException if the set contains invalid elements 077 */ 078 protected PredicatedNavigableSet(final NavigableSet<E> set, final Predicate<? super E> predicate) { 079 super(set, predicate); 080 } 081 082 /** 083 * Gets the navigable set being decorated. 084 * 085 * @return the decorated navigable set 086 */ 087 @Override 088 protected NavigableSet<E> decorated() { 089 return (NavigableSet<E>) super.decorated(); 090 } 091 092 //----------------------------------------------------------------------- 093 094 @Override 095 public E lower(final E e) { 096 return decorated().lower(e); 097 } 098 099 @Override 100 public E floor(final E e) { 101 return decorated().floor(e); 102 } 103 104 @Override 105 public E ceiling(final E e) { 106 return decorated().ceiling(e); 107 } 108 109 @Override 110 public E higher(final E e) { 111 return decorated().higher(e); 112 } 113 114 @Override 115 public E pollFirst() { 116 return decorated().pollFirst(); 117 } 118 119 @Override 120 public E pollLast() { 121 return decorated().pollLast(); 122 } 123 124 @Override 125 public NavigableSet<E> descendingSet() { 126 return predicatedNavigableSet(decorated().descendingSet(), predicate); 127 } 128 129 @Override 130 public Iterator<E> descendingIterator() { 131 return decorated().descendingIterator(); 132 } 133 134 @Override 135 public NavigableSet<E> subSet(final E fromElement, final boolean fromInclusive, final E toElement, 136 final boolean toInclusive) { 137 final NavigableSet<E> sub = decorated().subSet(fromElement, fromInclusive, toElement, toInclusive); 138 return predicatedNavigableSet(sub, predicate); 139 } 140 141 @Override 142 public NavigableSet<E> headSet(final E toElement, final boolean inclusive) { 143 final NavigableSet<E> head = decorated().headSet(toElement, inclusive); 144 return predicatedNavigableSet(head, predicate); 145 } 146 147 @Override 148 public NavigableSet<E> tailSet(final E fromElement, final boolean inclusive) { 149 final NavigableSet<E> tail = decorated().tailSet(fromElement, inclusive); 150 return predicatedNavigableSet(tail, predicate); 151 } 152 153}