MultiMapUtils.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 java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.HashSet;
  21. import java.util.List;
  22. import java.util.Set;

  23. import org.apache.commons.collections4.bag.HashBag;
  24. import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
  25. import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
  26. import org.apache.commons.collections4.multimap.TransformedMultiValuedMap;
  27. import org.apache.commons.collections4.multimap.UnmodifiableMultiValuedMap;

  28. /**
  29.  * Provides utility methods and decorators for {@link MultiValuedMap} instances.
  30.  * <p>
  31.  * It contains various type safe and null safe methods. Additionally, it provides
  32.  * the following decorators:
  33.  * </p>
  34.  * <ul>
  35.  *   <li>{@link #unmodifiableMultiValuedMap(MultiValuedMap)}</li>
  36.  *   <li>{@link #transformedMultiValuedMap(MultiValuedMap, Transformer, Transformer)}</li>
  37.  * </ul>
  38.  *
  39.  * @since 4.1
  40.  */
  41. public class MultiMapUtils {

  42.     /**
  43.      * An empty {@link UnmodifiableMultiValuedMap}.
  44.      */
  45.     @SuppressWarnings({ "rawtypes", "unchecked" })
  46.     public static final MultiValuedMap EMPTY_MULTI_VALUED_MAP =
  47.             UnmodifiableMultiValuedMap.unmodifiableMultiValuedMap(new ArrayListValuedHashMap(0, 0));

  48.     /**
  49.      * Returns an immutable empty {@code MultiValuedMap} if the argument is
  50.      * {@code null}, or the argument itself otherwise.
  51.      *
  52.      * @param <K> the type of key in the map
  53.      * @param <V> the type of value in the map
  54.      * @param map  the map, may be null
  55.      * @return an empty {@link MultiValuedMap} if the argument is null
  56.      */
  57.     @SuppressWarnings("unchecked")
  58.     public static <K, V> MultiValuedMap<K, V> emptyIfNull(final MultiValuedMap<K, V> map) {
  59.         return map == null ? EMPTY_MULTI_VALUED_MAP : map;
  60.     }

  61.     /**
  62.      * Returns immutable EMPTY_MULTI_VALUED_MAP with generic type safety.
  63.      *
  64.      * @param <K> the type of key in the map
  65.      * @param <V> the type of value in the map
  66.      * @return immutable and empty {@code MultiValuedMap}
  67.      */
  68.     @SuppressWarnings("unchecked")
  69.     public static <K, V> MultiValuedMap<K, V> emptyMultiValuedMap() {
  70.         return EMPTY_MULTI_VALUED_MAP;
  71.     }

  72.     // Null safe methods

  73.     /**
  74.      * Gets a Collection from {@code MultiValuedMap} in a null-safe manner.
  75.      *
  76.      * @param <K> the key type
  77.      * @param <V> the value type
  78.      * @param map  the {@link MultiValuedMap} to use
  79.      * @param key  the key to look up
  80.      * @return the Collection in the {@link MultiValuedMap}, or null if input map is null
  81.      */
  82.     public static <K, V> Collection<V> getCollection(final MultiValuedMap<K, V> map, final K key) {
  83.         if (map != null) {
  84.             return map.get(key);
  85.         }
  86.         return null;
  87.     }

  88.     /**
  89.      * Gets a Bag from {@code MultiValuedMap} in a null-safe manner.
  90.      *
  91.      * @param <K> the key type
  92.      * @param <V> the value type
  93.      * @param map  the {@link MultiValuedMap} to use
  94.      * @param key  the key to look up
  95.      * @return the Collection in the {@link MultiValuedMap} as Bag, or null if input map is null
  96.      */
  97.     public static <K, V> Bag<V> getValuesAsBag(final MultiValuedMap<K, V> map, final K key) {
  98.         if (map != null) {
  99.             final Collection<V> col = map.get(key);
  100.             if (col instanceof Bag) {
  101.                 return (Bag<V>) col;
  102.             }
  103.             return new HashBag<>(col);
  104.         }
  105.         return null;
  106.     }

  107.     /**
  108.      * Gets a List from {@code MultiValuedMap} in a null-safe manner.
  109.      *
  110.      * @param <K> the key type
  111.      * @param <V> the value type
  112.      * @param map  the {@link MultiValuedMap} to use
  113.      * @param key  the key to look up
  114.      * @return the Collection in the {@link MultiValuedMap} as List, or null if input map is null
  115.      */
  116.     public static <K, V> List<V> getValuesAsList(final MultiValuedMap<K, V> map, final K key) {
  117.         if (map != null) {
  118.             final Collection<V> col = map.get(key);
  119.             if (col instanceof List) {
  120.                 return (List<V>) col;
  121.             }
  122.             return new ArrayList<>(col);
  123.         }
  124.         return null;
  125.     }

  126.     // TODO: review the getValuesAsXXX methods - depending on the actual MultiValuedMap type, changes
  127.     // to the returned collection might update the backing map. This should be clarified and/or prevented.

  128.     /**
  129.      * Gets a Set from {@code MultiValuedMap} in a null-safe manner.
  130.      *
  131.      * @param <K> the key type
  132.      * @param <V> the value type
  133.      * @param map  the {@link MultiValuedMap} to use
  134.      * @param key  the key to look up
  135.      * @return the Collection in the {@link MultiValuedMap} as Set, or null if input map is null
  136.      */
  137.     public static <K, V> Set<V> getValuesAsSet(final MultiValuedMap<K, V> map, final K key) {
  138.         if (map != null) {
  139.             final Collection<V> col = map.get(key);
  140.             if (col instanceof Set) {
  141.                 return (Set<V>) col;
  142.             }
  143.             return new HashSet<>(col);
  144.         }
  145.         return null;
  146.     }

  147.     /**
  148.      * Null-safe check if the specified {@code MultiValuedMap} is empty.
  149.      * <p>
  150.      * If the provided map is null, returns true.
  151.      * </p>
  152.      *
  153.      * @param map  the map to check, may be null
  154.      * @return true if the map is empty or null
  155.      */
  156.     public static boolean isEmpty(final MultiValuedMap<?, ?> map) {
  157.         return map == null || map.isEmpty();
  158.     }

  159.     /**
  160.      * Creates a {@link ListValuedMap} with an {@link java.util.ArrayList ArrayList} as
  161.      * collection class to store the values mapped to a key.
  162.      *
  163.      * @param <K> the key type
  164.      * @param <V> the value type
  165.      * @return a new {@code ListValuedMap}
  166.      */
  167.     public static <K, V> ListValuedMap<K, V> newListValuedHashMap() {
  168.         return new ArrayListValuedHashMap<>();
  169.     }

  170.     /**
  171.      * Creates a {@link SetValuedMap} with an {@link java.util.HashSet HashSet} as
  172.      * collection class to store the values mapped to a key.
  173.      *
  174.      * @param <K> the key type
  175.      * @param <V> the value type
  176.      * @return a new {@link SetValuedMap}
  177.      */
  178.     public static <K, V> SetValuedMap<K, V> newSetValuedHashMap() {
  179.         return new HashSetValuedHashMap<>();
  180.     }

  181.     /**
  182.      * Returns a {@code TransformedMultiValuedMap} backed by the given map.
  183.      * <p>
  184.      * This method returns a new {@code MultiValuedMap} (decorating the
  185.      * specified map) that will transform any new entries added to it. Existing
  186.      * entries in the specified map will not be transformed. If you want that
  187.      * behavior, see {@link TransformedMultiValuedMap#transformedMap}.
  188.      * </p>
  189.      * <p>
  190.      * Each object is passed through the transformers as it is added to the Map.
  191.      * It is important not to use the original map after invoking this method,
  192.      * as it is a back door for adding untransformed objects.
  193.      * </p>
  194.      * <p>
  195.      * If there are any elements already in the map being decorated, they are
  196.      * NOT transformed.
  197.      * </p>
  198.      *
  199.      * @param <K> the key type
  200.      * @param <V> the value type
  201.      * @param map  the {@link MultiValuedMap} to transform, must not be null, typically empty
  202.      * @param keyTransformer  the transformer for the map keys, null means no transformation
  203.      * @param valueTransformer  the transformer for the map values, null means no transformation
  204.      * @return a transformed {@code MultiValuedMap} backed by the given map
  205.      * @throws NullPointerException if map is null
  206.      */
  207.     public static <K, V> MultiValuedMap<K, V> transformedMultiValuedMap(final MultiValuedMap<K, V> map,
  208.             final Transformer<? super K, ? extends K> keyTransformer,
  209.             final Transformer<? super V, ? extends V> valueTransformer) {
  210.         return TransformedMultiValuedMap.transformingMap(map, keyTransformer, valueTransformer);
  211.     }

  212.     /**
  213.      * Returns an {@code UnmodifiableMultiValuedMap} backed by the given
  214.      * map.
  215.      *
  216.      * @param <K> the key type
  217.      * @param <V> the value type
  218.      * @param map  the {@link MultiValuedMap} to decorate, must not be null
  219.      * @return an unmodifiable {@link MultiValuedMap} backed by the provided map
  220.      * @throws NullPointerException if map is null
  221.      */
  222.     public static <K, V> MultiValuedMap<K, V> unmodifiableMultiValuedMap(
  223.             final MultiValuedMap<? extends K, ? extends V> map) {
  224.         return UnmodifiableMultiValuedMap.<K, V>unmodifiableMultiValuedMap(map);
  225.     }

  226.     /**
  227.      * Don't allow instances.
  228.      */
  229.     private MultiMapUtils() {
  230.         // empty
  231.     }

  232. }