/** * Factory method to create a predicated (validating) set. * <p> * If there are any elements already in the set being decorated, they * are validated. * * @param <E> the element type * @param set the set to decorate, must not be null * @param predicate the predicate to use for validation, must not be null * @return a decorated set * @throws NullPointerException if set or predicate is null * @throws IllegalArgumentException if the set contains invalid elements * @since 4.0 */ public static <E> PredicatedSet<E> predicatedSet(final Set<E> set, final Predicate<? super E> predicate) { return new PredicatedSet<>(set, predicate); }
/** * Gets the sorted set being decorated. * * @return the decorated sorted set */ @Override protected SortedSet<E> decorated() { return (SortedSet<E>) super.decorated(); }
/** * 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; }
@Override public boolean equals(final Object object) { return object == this || decorated().equals(object); }
@Override public int hashCode() { return decorated().hashCode(); }