Home | History | Annotate | Download | only in collections
      1 package org.testng.collections;
      2 
      3 import java.util.ArrayList;
      4 import java.util.Collection;
      5 import java.util.HashSet;
      6 import java.util.List;
      7 import java.util.Map;
      8 import java.util.Set;
      9 
     10 public abstract class MultiMap<K, V, C extends Collection<V>> {
     11   protected final Map<K, C> m_objects = Maps.newHashMap();
     12 
     13   protected abstract C createValue();
     14 
     15   public boolean put(K key, V method) {
     16     boolean setExists = true;
     17     C l = m_objects.get(key);
     18     if (l == null) {
     19       setExists = false;
     20       l = createValue();
     21       m_objects.put(key, l);
     22     }
     23     return l.add(method) && setExists;
     24   }
     25 
     26   public C get(K key) {
     27     return m_objects.get(key);
     28   }
     29 
     30   @Deprecated
     31   public List<K> getKeys() {
     32     return new ArrayList<>(keySet());
     33   }
     34 
     35   public Set<K> keySet() {
     36     return new HashSet(m_objects.keySet());
     37   }
     38 
     39   public boolean containsKey(K k) {
     40     return m_objects.containsKey(k);
     41   }
     42 
     43   @Override
     44   public String toString() {
     45     StringBuilder result = new StringBuilder();
     46     Set<K> indices = keySet();
     47     for (K i : indices) {
     48       result.append("\n    ").append(i).append(" <-- ");
     49       for (Object o : m_objects.get(i)) {
     50         result.append(o).append(" ");
     51       }
     52     }
     53     return result.toString();
     54   }
     55 
     56   public boolean isEmpty() {
     57     return m_objects.size() == 0;
     58   }
     59 
     60   @Deprecated
     61   public int getSize() {
     62     return size();
     63   }
     64 
     65   public int size() {
     66     return m_objects.size();
     67   }
     68 
     69   @Deprecated
     70   public C remove(K key) {
     71     return removeAll(key);
     72   }
     73 
     74   public boolean remove(K key, V value) {
     75     C values = get(key);
     76     if (values == null) {
     77       return false;
     78     }
     79     return values.remove(value);
     80   }
     81 
     82   public C removeAll(K key) {
     83     return m_objects.remove(key);
     84   }
     85 
     86   @Deprecated
     87   public Set<Map.Entry<K, C>> getEntrySet() {
     88     return entrySet();
     89   }
     90 
     91   public Set<Map.Entry<K, C>> entrySet() {
     92     return m_objects.entrySet();
     93   }
     94 
     95   @Deprecated
     96   public Collection<C> getValues() {
     97     return values();
     98   }
     99 
    100   public Collection<C> values() {
    101     return m_objects.values();
    102   }
    103 
    104   public boolean putAll(K k, Collection<? extends V> values) {
    105     boolean result = false;
    106     for (V v : values) {
    107       result = put(k, v) || result;
    108     }
    109     return result;
    110   }
    111 }
    112