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