    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.bag;
    018
    019import java.util.Set;
    020
    021import org.apache.commons.collections4.Bag;
    022import org.apache.commons.collections4.Predicate;
    023import org.apache.commons.collections4.collection.PredicatedCollection;
    024
    025/**
    026 * Decorates another {@link Bag} to validate that additions
    027 * match a specified predicate.
    028 * <p>
    029 * This bag exists to provide validation for the decorated bag.
    030 * It is normally created to decorate an empty bag.
    031 * If an object cannot be added to the bag, an {@link IllegalArgumentException} is thrown.
    032 * </p>
    033 * <p>
    034 * One usage would be to ensure that no null entries are added to the bag.
    035 * </p>
    036 * <pre>
    037 * Bag bag = PredicatedBag.predicatedBag(new HashBag(), NotNullPredicate.INSTANCE);
    038 * </pre>
    039 * <p>
    040 * This class is Serializable from Commons Collections 3.1.
    041 * </p>
    042 *
    043 * @param <E> the type of elements in this bag
    044 * @since 3.0
    045 */
    046public class PredicatedBag<E> extends PredicatedCollection<E> implements Bag<E> {
    047
    048    /** Serialization version */
    049    private static final long serialVersionUID = -2575833140344736876L;
    050
    051    /**
    052     * Factory method to create a predicated (validating) bag.
    053     * <p>
    054     * If there are any elements already in the bag being decorated, they
    055     * are validated.
    056     *
    057     * @param <E> the type of the elements in the bag
    058     * @param bag  the bag to decorate, must not be null
    059     * @param predicate  the predicate to use for validation, must not be null
    060     * @return a new predicated Bag
    061     * @throws NullPointerException if bag or predicate is null
    062     * @throws IllegalArgumentException if the bag contains invalid elements
    063     * @since 4.0
    064     */
    065    public static <E> PredicatedBag<E> predicatedBag(final Bag<E> bag, final Predicate<? super E> predicate) {
    066        return new PredicatedBag<>(bag, predicate);
    067    }
    068
    069    /**
    070     * Constructor that wraps (not copies).
    071     * <p>
    072     * If there are any elements already in the bag being decorated, they
    073     * are validated.
    074     *
    075     * @param bag  the bag to decorate, must not be null
    076     * @param predicate  the predicate to use for validation, must not be null
    077     * @throws NullPointerException if bag or predicate is null
    078     * @throws IllegalArgumentException if the bag contains invalid elements
    079     */
    080    protected PredicatedBag(final Bag<E> bag, final Predicate<? super E> predicate) {
    081        super(bag, predicate);
    082    }
    083
    084    @Override
    085    public boolean add(final E object, final int count) {
    086        validate(object);
    087        return decorated().add(object, count);
    088    }
    089
    090    /**
    091     * Gets the decorated bag.
    092     *
    093     * @return the decorated bag
    094     */
    095    @Override
    096    protected Bag<E> decorated() {
    097        return (Bag<E>) super.decorated();
    098    }
    099
    100    @Override
    101    public boolean equals(final Object object) {
    102        return object == this || decorated().equals(object);
    103    }
    104
    105    @Override
    106    public int getCount(final Object object) {
    107        return decorated().getCount(object);
    108    }
    109
    110    @Override
    111    public int hashCode() {
    112        return decorated().hashCode();
    113    }
    114
    115    @Override
    116    public boolean remove(final Object object, final int count) {
    117        return decorated().remove(object, count);
    118    }
    119
    120    @Override
    121    public Set<E> uniqueSet() {
    122        return decorated().uniqueSet();
    123    }
    124
    125}