    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;
    018
    019import org.apache.commons.collections4.multiset.HashMultiSet;
    020import org.apache.commons.collections4.multiset.PredicatedMultiSet;
    021import org.apache.commons.collections4.multiset.SynchronizedMultiSet;
    022import org.apache.commons.collections4.multiset.UnmodifiableMultiSet;
    023
    024/**
    025 * Provides utility methods and decorators for {@link MultiSet} instances.
    026 *
    027 * @since 4.1
    028 */
    029public class MultiSetUtils {
    030
    031    /**
    032     * An empty unmodifiable multiset.
    033     */
    034    @SuppressWarnings("rawtypes") // OK, empty multiset is compatible with any type
    035    public static final MultiSet EMPTY_MULTISET =
    036        UnmodifiableMultiSet.unmodifiableMultiSet(new HashMultiSet<>());
    037
    038    /**
    039     * Gets an empty {@code MultiSet}.
    040     *
    041     * @param <E> the element type
    042     * @return an empty MultiSet
    043     */
    044    @SuppressWarnings("unchecked") // OK, empty multiset is compatible with any type
    045    public static <E> MultiSet<E> emptyMultiSet() {
    046        return EMPTY_MULTISET;
    047    }
    048
    049    /**
    050     * Returns a predicated (validating) multiset backed by the given multiset.
    051     * <p>
    052     * Only objects that pass the test in the given predicate can be added to
    053     * the multiset. Trying to add an invalid object results in an
    054     * IllegalArgumentException. It is important not to use the original multiset
    055     * after invoking this method, as it is a backdoor for adding invalid
    056     * objects.
    057     *
    058     * @param <E> the element type
    059     * @param multiset the multiset to predicate, must not be null
    060     * @param predicate the predicate for the multiset, must not be null
    061     * @return a predicated multiset backed by the given multiset
    062     * @throws NullPointerException if the MultiSet or Predicate is null
    063     */
    064    public static <E> MultiSet<E> predicatedMultiSet(final MultiSet<E> multiset,
    065            final Predicate<? super E> predicate) {
    066        return PredicatedMultiSet.predicatedMultiSet(multiset, predicate);
    067    }
    068
    069    /**
    070     * Returns a synchronized (thread-safe) multiset backed by the given multiset.
    071     * In order to guarantee serial access, it is critical that all access to the
    072     * backing multiset is accomplished through the returned multiset.
    073     * <p>
    074     * It is imperative that the user manually synchronize on the returned multiset
    075     * when iterating over it:
    076     *
    077     * <pre>
    078     * MultiSet multiset = MultiSetUtils.synchronizedMultiSet(new HashMultiSet());
    079     * ...
    080     * synchronized(multiset) {
    081     *     Iterator i = multiset.iterator(); // Must be in synchronized block
    082     *     while (i.hasNext())
    083     *         foo(i.next());
    084     *     }
    085     * }
    086     * </pre>
    087     *
    088     * Failure to follow this advice may result in non-deterministic behavior.
    089     *
    090     * @param <E> the element type
    091     * @param multiset the multiset to synchronize, must not be null
    092     * @return a synchronized multiset backed by that multiset
    093     * @throws NullPointerException if the MultiSet is null
    094     */
    095    public static <E> MultiSet<E> synchronizedMultiSet(final MultiSet<E> multiset) {
    096        return SynchronizedMultiSet.synchronizedMultiSet(multiset);
    097    }
    098
    099    /**
    100     * Returns an unmodifiable view of the given multiset. Any modification attempts
    101     * to the returned multiset will raise an {@link UnsupportedOperationException}.
    102     *
    103     * @param <E> the element type
    104     * @param multiset the multiset whose unmodifiable view is to be returned, must not be null
    105     * @return an unmodifiable view of that multiset
    106     * @throws NullPointerException if the MultiSet is null
    107     */
    108    public static <E> MultiSet<E> unmodifiableMultiSet(final MultiSet<? extends E> multiset) {
    109        return UnmodifiableMultiSet.unmodifiableMultiSet(multiset);
    110    }
    111
    112    /**
    113     * Don't allow instances.
    114     */
    115    private MultiSetUtils() {
    116        // empty
    117    }
    118
    119}