/** * Returns a predicated (validating) collection backed by the given collection. * <p> * Only objects that pass the test in the given predicate can be added to the collection. * Trying to add an invalid object results in an IllegalArgumentException. * It is important not to use the original collection after invoking this method, * as it is a backdoor for adding invalid objects. * * @param collection the collection to predicate, must not be null * @param predicate the predicate for the collection, must not be null * @return a predicated collection backed by the given collection * @throws IllegalArgumentException if the Collection is null */ public static Collection predicatedCollection(Collection collection, Predicate predicate) { return PredicatedCollection.decorate(collection, predicate); }
/** * Factory method to create a predicated (validating) collection. * <p> * If there are any elements already in the collection being decorated, they * are validated. * * @param coll the collection to decorate, must not be null * @param predicate the predicate to use for validation, must not be null * @return a new predicated collection * @throws IllegalArgumentException if collection or predicate is null * @throws IllegalArgumentException if the collection contains invalid elements */ public static Collection decorate(Collection coll, Predicate predicate) { return new PredicatedCollection(coll, predicate); }
/** * Override to validate the object being added to ensure it matches * the predicate. * * @param object the object being added * @return the result of adding to the underlying collection * @throws IllegalArgumentException if the add is invalid */ public boolean add(Object object) { validate(object); return getCollection().add(object); }
/** * Constructor that wraps (not copies). * <p> * If there are any elements already in the collection being decorated, they * are validated. * * @param coll the collection to decorate, must not be null * @param predicate the predicate to use for validation, must not be null * @throws IllegalArgumentException if collection or predicate is null * @throws IllegalArgumentException if the collection contains invalid elements */ protected PredicatedCollection(Collection coll, Predicate predicate) { super(coll); if (predicate == null) { throw new IllegalArgumentException("Predicate must not be null"); } this.predicate = predicate; for (Iterator it = coll.iterator(); it.hasNext(); ) { validate(it.next()); } }
/** * Constructor that wraps (not copies). * <p> * If there are any elements already in the collection being decorated, they * are validated. * * @param coll the collection to decorate, must not be null * @param predicate the predicate to use for validation, must not be null * @throws IllegalArgumentException if collection or predicate is null * @throws IllegalArgumentException if the collection contains invalid elements */ protected PredicatedCollection(Collection coll, Predicate predicate) { super(coll); if (predicate == null) { throw new IllegalArgumentException("Predicate must not be null"); } this.predicate = predicate; for (Iterator it = coll.iterator(); it.hasNext(); ) { validate(it.next()); } }
/** * Override to validate the object being added to ensure it matches * the predicate. * * @param object the object being added * @return the result of adding to the underlying collection * @throws IllegalArgumentException if the add is invalid */ public boolean add(Object object) { validate(object); return getCollection().add(object); }
/** * Returns a predicated (validating) collection backed by the given collection. * <p> * Only objects that pass the test in the given predicate can be added to the collection. * Trying to add an invalid object results in an IllegalArgumentException. * It is important not to use the original collection after invoking this method, * as it is a backdoor for adding invalid objects. * * @param collection the collection to predicate, must not be null * @param predicate the predicate for the collection, must not be null * @return a predicated collection backed by the given collection * @throws IllegalArgumentException if the Collection is null */ public static Collection predicatedCollection(Collection collection, Predicate predicate) { return PredicatedCollection.decorate(collection, predicate); }
/** * Factory method to create a predicated (validating) collection. * <p> * If there are any elements already in the collection being decorated, they * are validated. * * @param coll the collection to decorate, must not be null * @param predicate the predicate to use for validation, must not be null * @return a new predicated collection * @throws IllegalArgumentException if collection or predicate is null * @throws IllegalArgumentException if the collection contains invalid elements */ public static Collection decorate(Collection coll, Predicate predicate) { return new PredicatedCollection(coll, predicate); }
/** * Constructor that wraps (not copies). * <p> * If there are any elements already in the collection being decorated, they * are validated. * * @param coll the collection to decorate, must not be null * @param predicate the predicate to use for validation, must not be null * @throws IllegalArgumentException if collection or predicate is null * @throws IllegalArgumentException if the collection contains invalid elements */ protected PredicatedCollection(Collection coll, Predicate predicate) { super(coll); if (predicate == null) { throw new IllegalArgumentException("Predicate must not be null"); } this.predicate = predicate; for (Iterator it = coll.iterator(); it.hasNext(); ) { validate(it.next()); } }
/** * Override to validate the objects being added to ensure they match * the predicate. If any one fails, no update is made to the underlying * collection. * * @param coll the collection being added * @return the result of adding to the underlying collection * @throws IllegalArgumentException if the add is invalid */ public boolean addAll(Collection coll) { for (Iterator it = coll.iterator(); it.hasNext(); ) { validate(it.next()); } return getCollection().addAll(coll); }
protected Collection decorateCollection(Collection collection, Predicate predicate) { return PredicatedCollection.decorate(collection, predicate); }
/** * Factory method to create a typed collection. * <p> * If there are any elements already in the collection being decorated, they * are validated. * * @param coll the collection to decorate, must not be null * @param type the type to allow into the collection, must not be null * @return a new typed collection * @throws IllegalArgumentException if collection or type is null * @throws IllegalArgumentException if the collection contains invalid elements */ public static Collection decorate(Collection coll, Class type) { return new PredicatedCollection(coll, InstanceofPredicate.getInstance(type)); }
/** * Constructor that wraps (not copies). * <p> * If there are any elements already in the collection being decorated, they * are validated. * * @param coll the collection to decorate, must not be null * @param predicate the predicate to use for validation, must not be null * @throws IllegalArgumentException if collection or predicate is null * @throws IllegalArgumentException if the collection contains invalid elements */ protected PredicatedCollection(Collection coll, Predicate predicate) { super(coll); if (predicate == null) { throw new IllegalArgumentException("Predicate must not be null"); } this.predicate = predicate; for (Iterator it = coll.iterator(); it.hasNext(); ) { validate(it.next()); } }
/** * Override to validate the objects being added to ensure they match * the predicate. If any one fails, no update is made to the underlying * collection. * * @param coll the collection being added * @return the result of adding to the underlying collection * @throws IllegalArgumentException if the add is invalid */ public boolean addAll(Collection coll) { for (Iterator it = coll.iterator(); it.hasNext(); ) { validate(it.next()); } return getCollection().addAll(coll); }
/** * Returns a predicated (validating) collection backed by the given collection. * <p> * Only objects that pass the test in the given predicate can be added to the collection. * Trying to add an invalid object results in an IllegalArgumentException. * It is important not to use the original collection after invoking this method, * as it is a backdoor for adding invalid objects. * * @param collection the collection to predicate, must not be null * @param predicate the predicate for the collection, must not be null * @return a predicated collection backed by the given collection * @throws IllegalArgumentException if the Collection is null */ public static Collection predicatedCollection(Collection collection, Predicate predicate) { return PredicatedCollection.decorate(collection, predicate); }
/** * Factory method to create a typed collection. * <p> * If there are any elements already in the collection being decorated, they * are validated. * * @param coll the collection to decorate, must not be null * @param type the type to allow into the collection, must not be null * @return a new typed collection * @throws IllegalArgumentException if collection or type is null * @throws IllegalArgumentException if the collection contains invalid elements */ public static Collection decorate(Collection coll, Class type) { return new PredicatedCollection(coll, InstanceofPredicate.getInstance(type)); }
/** * Constructor that wraps (not copies). * <p> * If there are any elements already in the collection being decorated, they * are validated. * * @param coll the collection to decorate, must not be null * @param predicate the predicate to use for validation, must not be null * @throws IllegalArgumentException if collection or predicate is null * @throws IllegalArgumentException if the collection contains invalid elements */ protected PredicatedCollection(Collection coll, Predicate predicate) { super(coll); if (predicate == null) { throw new IllegalArgumentException("Predicate must not be null"); } this.predicate = predicate; for (Iterator it = coll.iterator(); it.hasNext(); ) { validate(it.next()); } }
/** * Override to validate the object being added to ensure it matches * the predicate. * * @param object the object being added * @return the result of adding to the underlying collection * @throws IllegalArgumentException if the add is invalid */ public boolean add(Object object) { validate(object); return getCollection().add(object); }
/** * Returns a predicated (validating) collection backed by the given collection. * <p> * Only objects that pass the test in the given predicate can be added to the collection. * Trying to add an invalid object results in an IllegalArgumentException. * It is important not to use the original collection after invoking this method, * as it is a backdoor for adding invalid objects. * * @param collection the collection to predicate, must not be null * @param predicate the predicate for the collection, must not be null * @return a predicated collection backed by the given collection * @throws IllegalArgumentException if the Collection is null */ public static Collection predicatedCollection(Collection collection, Predicate predicate) { return PredicatedCollection.decorate(collection, predicate); }
/** * Factory method to create a predicated (validating) collection. * <p> * If there are any elements already in the collection being decorated, they * are validated. * * @param coll the collection to decorate, must not be null * @param predicate the predicate to use for validation, must not be null * @return a new predicated collection * @throws IllegalArgumentException if collection or predicate is null * @throws IllegalArgumentException if the collection contains invalid elements */ public static Collection decorate(Collection coll, Predicate predicate) { return new PredicatedCollection(coll, predicate); }