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     * </p>
058     *
059     * @param <E> the element type
060     * @param multiset the multiset to predicate, must not be null
061     * @param predicate the predicate for the multiset, must not be null
062     * @return a predicated multiset backed by the given multiset
063     * @throws NullPointerException if the MultiSet or Predicate is null
064     */
065    public static <E> MultiSet<E> predicatedMultiSet(final MultiSet<E> multiset,
066            final Predicate<? super E> predicate) {
067        return PredicatedMultiSet.predicatedMultiSet(multiset, predicate);
068    }
069
070    /**
071     * Returns a synchronized (thread-safe) multiset backed by the given multiset.
072     * In order to guarantee serial access, it is critical that all access to the
073     * backing multiset is accomplished through the returned multiset.
074     * <p>
075     * It is imperative that the user manually synchronize on the returned multiset
076     * when iterating over it:
077     * </p>
078     * <pre>
079     * MultiSet multiset = MultiSetUtils.synchronizedMultiSet(new HashMultiSet());
080     * ...
081     * synchronized(multiset) {
082     *     Iterator i = multiset.iterator(); // Must be in synchronized block
083     *     while (i.hasNext())
084     *         foo(i.next());
085     *     }
086     * }
087     * </pre>
088     *
089     * Failure to follow this advice may result in non-deterministic behavior.
090     *
091     * @param <E> the element type
092     * @param multiset the multiset to synchronize, must not be null
093     * @return a synchronized multiset backed by that multiset
094     * @throws NullPointerException if the MultiSet is null
095     */
096    public static <E> MultiSet<E> synchronizedMultiSet(final MultiSet<E> multiset) {
097        return SynchronizedMultiSet.synchronizedMultiSet(multiset);
098    }
099
100    /**
101     * Returns an unmodifiable view of the given multiset. Any modification attempts
102     * to the returned multiset will raise an {@link UnsupportedOperationException}.
103     *
104     * @param <E> the element type
105     * @param multiset the multiset whose unmodifiable view is to be returned, must not be null
106     * @return an unmodifiable view of that multiset
107     * @throws NullPointerException if the MultiSet is null
108     */
109    public static <E> MultiSet<E> unmodifiableMultiSet(final MultiSet<? extends E> multiset) {
110        return UnmodifiableMultiSet.unmodifiableMultiSet(multiset);
111    }
112
113    /**
114     * Don't allow instances.
115     */
116    private MultiSetUtils() {
117        // empty
118    }
119
120}