/** * Returns a predicated (validating) set backed by the given set. * <p> * Only objects that pass the test in the given predicate can be added to the set. * Trying to add an invalid object results in an IllegalArgumentException. * It is important not to use the original set after invoking this method, * as it is a backdoor for adding invalid objects. * * @param <E> the element type * @param set the set to predicate, must not be null * @param predicate the predicate for the set, must not be null * @return a predicated set backed by the given set * @throws NullPointerException if the set or predicate is null */ public static <E> Set<E> predicatedSet(final Set<E> set, final Predicate<? super E> predicate) { return PredicatedSet.predicatedSet(set, predicate); }
/** * Decorates the given list with validating behavior using the predicate. All accepted elements * are appended to the set. If the set already contains elements, they are validated. * <p> * The builder is not modified by this method, so it is possible to create more collections * or add more elements afterwards. Further changes will not propagate to the returned set. * * @param set the set to decorate, must not be null * @return the decorated set. * @throws NullPointerException if set is null * @throws IllegalArgumentException if set contains invalid elements */ public Set<E> createPredicatedSet(final Set<E> set) { if (set == null) { throw new NullPointerException("Set must not be null."); } final PredicatedSet<E> predicatedSet = PredicatedSet.predicatedSet(set, predicate); predicatedSet.addAll(accepted); return predicatedSet; }