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