MultiSetUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4;

  18. import org.apache.commons.collections4.multiset.HashMultiSet;
  19. import org.apache.commons.collections4.multiset.PredicatedMultiSet;
  20. import org.apache.commons.collections4.multiset.SynchronizedMultiSet;
  21. import org.apache.commons.collections4.multiset.UnmodifiableMultiSet;

  22. /**
  23.  * Provides utility methods and decorators for {@link MultiSet} instances.
  24.  *
  25.  * @since 4.1
  26.  */
  27. public class MultiSetUtils {

  28.     /**
  29.      * An empty unmodifiable multiset.
  30.      */
  31.     @SuppressWarnings("rawtypes") // OK, empty multiset is compatible with any type
  32.     public static final MultiSet EMPTY_MULTISET =
  33.         UnmodifiableMultiSet.unmodifiableMultiSet(new HashMultiSet<>());

  34.     /**
  35.      * Gets an empty {@code MultiSet}.
  36.      *
  37.      * @param <E> the element type
  38.      * @return an empty MultiSet
  39.      */
  40.     @SuppressWarnings("unchecked") // OK, empty multiset is compatible with any type
  41.     public static <E> MultiSet<E> emptyMultiSet() {
  42.         return EMPTY_MULTISET;
  43.     }

  44.     /**
  45.      * Returns a predicated (validating) multiset backed by the given multiset.
  46.      * <p>
  47.      * Only objects that pass the test in the given predicate can be added to
  48.      * the multiset. Trying to add an invalid object results in an
  49.      * IllegalArgumentException. It is important not to use the original multiset
  50.      * after invoking this method, as it is a backdoor for adding invalid
  51.      * objects.
  52.      * </p>
  53.      *
  54.      * @param <E> the element type
  55.      * @param multiset the multiset to predicate, must not be null
  56.      * @param predicate the predicate for the multiset, must not be null
  57.      * @return a predicated multiset backed by the given multiset
  58.      * @throws NullPointerException if the MultiSet or Predicate is null
  59.      */
  60.     public static <E> MultiSet<E> predicatedMultiSet(final MultiSet<E> multiset,
  61.             final Predicate<? super E> predicate) {
  62.         return PredicatedMultiSet.predicatedMultiSet(multiset, predicate);
  63.     }

  64.     /**
  65.      * Returns a synchronized (thread-safe) multiset backed by the given multiset.
  66.      * In order to guarantee serial access, it is critical that all access to the
  67.      * backing multiset is accomplished through the returned multiset.
  68.      * <p>
  69.      * It is imperative that the user manually synchronize on the returned multiset
  70.      * when iterating over it:
  71.      * </p>
  72.      * <pre>
  73.      * MultiSet multiset = MultiSetUtils.synchronizedMultiSet(new HashMultiSet());
  74.      * ...
  75.      * synchronized(multiset) {
  76.      *     Iterator i = multiset.iterator(); // Must be in synchronized block
  77.      *     while (i.hasNext())
  78.      *         foo(i.next());
  79.      *     }
  80.      * }
  81.      * </pre>
  82.      *
  83.      * Failure to follow this advice may result in non-deterministic behavior.
  84.      *
  85.      * @param <E> the element type
  86.      * @param multiset the multiset to synchronize, must not be null
  87.      * @return a synchronized multiset backed by that multiset
  88.      * @throws NullPointerException if the MultiSet is null
  89.      */
  90.     public static <E> MultiSet<E> synchronizedMultiSet(final MultiSet<E> multiset) {
  91.         return SynchronizedMultiSet.synchronizedMultiSet(multiset);
  92.     }

  93.     /**
  94.      * Returns an unmodifiable view of the given multiset. Any modification attempts
  95.      * to the returned multiset will raise an {@link UnsupportedOperationException}.
  96.      *
  97.      * @param <E> the element type
  98.      * @param multiset the multiset whose unmodifiable view is to be returned, must not be null
  99.      * @return an unmodifiable view of that multiset
  100.      * @throws NullPointerException if the MultiSet is null
  101.      */
  102.     public static <E> MultiSet<E> unmodifiableMultiSet(final MultiSet<? extends E> multiset) {
  103.         return UnmodifiableMultiSet.unmodifiableMultiSet(multiset);
  104.     }

  105.     /**
  106.      * Don't allow instances.
  107.      */
  108.     private MultiSetUtils() {
  109.         // empty
  110.     }

  111. }