MultiMap.java

  1. /**
  2.  *
  3.  * Copyright © 2015 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * 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.jivesoftware.smack.util;

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.LinkedHashMap;
  21. import java.util.LinkedHashSet;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;

  25. /**
  26.  * A lightweight implementation of a MultiMap, that is a Map that is able to hold multiple values for every key.
  27.  * <p>
  28.  * This MultiMap uses a LinkedHashMap together with a LinkedHashSet in order to keep the order of its entries.
  29.  * </p>
  30.  *
  31.  * @param <K> the type of the keys the map uses.
  32.  * @param <V> the type of the values the map uses.
  33.  */
  34. public class MultiMap<K,V> {

  35.     /**
  36.      * The constant value '6'.
  37.      */
  38.     public static final int DEFAULT_MAP_SIZE = 6;

  39.     private static final int ENTRY_SET_SIZE = 3;

  40.     private final Map<K, Set<V>> map;

  41.     /**
  42.      * Constructs a new MultiMap with a initial capacity of {@link #DEFAULT_MAP_SIZE}.
  43.      */
  44.     public MultiMap() {
  45.         this(DEFAULT_MAP_SIZE);
  46.     }

  47.     /**
  48.      * Constructs a new MultiMap.
  49.      *
  50.      * @param size the initial capacity.
  51.      */
  52.     public MultiMap(int size) {
  53.         map = new LinkedHashMap<>(size);
  54.     }

  55.     public int size() {
  56.         int size = 0;
  57.         for (Set<V> set : map.values()) {
  58.             size += set.size();
  59.         }
  60.         return size;
  61.     }

  62.     public boolean isEmpty() {
  63.         return map.isEmpty();
  64.     }

  65.     public boolean containsKey(Object key) {
  66.         return map.containsKey(key);
  67.     }

  68.     public boolean containsValue(Object value) {
  69.         for (Set<V> set : map.values()) {
  70.             if (set.contains(value)) {
  71.                 return true;
  72.             }
  73.         }
  74.         return false;
  75.     }

  76.     /**
  77.      * Get the first value for the given key, or <code>null</code> if there are no entries.
  78.      *
  79.      * @param key
  80.      * @return the first value or null.
  81.      */
  82.     public V getFirst(Object key) {
  83.         Set<V> res = getAll(key);
  84.         if (res.isEmpty()) {
  85.             return null;
  86.         } else {
  87.             return res.iterator().next();
  88.         }
  89.     }

  90.     /**
  91.      * Get all values for the given key. Returns the empty set if there are none.
  92.      * <p>
  93.      * Changes to the returned set will update the underlying MultiMap if the return set is not empty.
  94.      * </p>
  95.      *
  96.      * @param key
  97.      * @return all values for the given key.
  98.      */
  99.     public Set<V> getAll(Object key) {
  100.         Set<V> res = map.get(key);
  101.         if (res == null) {
  102.             res = Collections.emptySet();
  103.         }
  104.         return res;
  105.     }

  106.     public boolean put(K key, V value) {
  107.         boolean keyExisted;
  108.         Set<V> set = map.get(key);
  109.         if (set == null) {
  110.             set = new LinkedHashSet<>(ENTRY_SET_SIZE);
  111.             set.add(value);
  112.             map.put(key, set);
  113.             keyExisted = false;
  114.         } else {
  115.             set.add(value);
  116.             keyExisted = true;
  117.         }
  118.         return keyExisted;
  119.     }

  120.     /**
  121.      * Removes all mappings for the given key and returns the first value if there where mappings or <code>null</code> if not.
  122.      *
  123.      * @param key
  124.      * @return the first value of the given key or null.
  125.      */
  126.     public V remove(Object key) {
  127.         Set<V> res = map.remove(key);
  128.         if (res == null) {
  129.             return null;
  130.         }
  131.         assert(!res.isEmpty());
  132.         return res.iterator().next();
  133.     }

  134.     /**
  135.      * Remove the mapping of the given key to the value.
  136.      * <p>
  137.      * Returns <code>true</code> if the mapping was removed and <code>false</code> if the mapping did not exist.
  138.      * </p>
  139.      *
  140.      * @param key
  141.      * @param value
  142.      * @return true if the mapping was removed, false otherwise.
  143.      */
  144.     public boolean removeOne(Object key, V value) {
  145.         Set<V> set = map.get(key);
  146.         if (set == null) {
  147.             return false;
  148.         }
  149.         boolean res = set.remove(value);
  150.         if (set.isEmpty()) {
  151.             // Remove the key also if the value set is now empty
  152.             map.remove(key);
  153.         }
  154.         return res;
  155.     }

  156.     public void putAll(Map<? extends K, ? extends V> map) {
  157.         for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
  158.             put(entry.getKey(), entry.getValue());
  159.         }
  160.     }

  161.     public void clear() {
  162.         map.clear();
  163.     }

  164.     public Set<K> keySet() {
  165.         return map.keySet();
  166.     }

  167.     /**
  168.      * Returns a new list containing all values of this multi map.
  169.      *
  170.      * @return a new list with all values.
  171.      */
  172.     public List<V> values() {
  173.         List<V> values = new ArrayList<>(size());
  174.         for (Set<V> set : map.values()) {
  175.             values.addAll(set);
  176.         }
  177.         return values;
  178.     }

  179.     public Set<Map.Entry<K, V>> entrySet() {
  180.         Set<Map.Entry<K, V>> entrySet = new LinkedHashSet<>(size());
  181.         for (Map.Entry<K, Set<V>> entries : map.entrySet()) {
  182.             K key = entries.getKey();
  183.             for (V value : entries.getValue()) {
  184.                 entrySet.add(new SimpleMapEntry<>(key, value));
  185.             }
  186.         }
  187.         return entrySet;
  188.     }

  189.     private static class SimpleMapEntry<K, V> implements Map.Entry<K, V> {

  190.         private final K key;
  191.         private V value;

  192.         private SimpleMapEntry(K key, V value) {
  193.             this.key = key;
  194.             this.value = value;
  195.         }

  196.         @Override
  197.         public K getKey() {
  198.             return key;
  199.         }

  200.         @Override
  201.         public V getValue() {
  202.             return value;
  203.         }

  204.         @Override
  205.         public V setValue(V value) {
  206.             V tmp = this.value;
  207.             this.value = value;
  208.             return tmp;
  209.         }
  210.     }

  211. }