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