package org.cyclops.cyclopscore.helper;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Helper methods for collections.
 * @author rubensworks
 */
public final class CollectionHelpers {

    /**
     * Add the given key value pair in a map structure where multiple values for a key can exist.
     * This will take care of making a new list into the map when one did not exist before.
     * @param map The map.
     * @param key The key.
     * @param value The value.
     * @param <K> The key type.
     * @param <V> The value type.
     */
    public static <K, V> void addToMapList(Map<K, List<V>> map, K key, V value) {
        if(!map.containsKey(key)) {
            map.put(key, Lists.<V>newLinkedList());
        }
        map.get(key).add(value);
    }

    /**
     * Add the given key value pair in a map structure where multiple values for a key can exist.
     * This will take care of making a new set into the map when one did not exist before.
     * @param map The map.
     * @param key The key.
     * @param value The value.
     * @param <K> The key type.
     * @param <V> The value type.
     */
    public static <K, V> void addToMapSet(Map<K, Set<V>> map, K key, V value) {
        if(!map.containsKey(key)) {
            map.put(key, Sets.<V>newHashSet());
        }
        map.get(key).add(value);
    }

}

@@ -739,32 +739,72 @@
     
-if(!map.containsKey(key)
+List%3CV%3E collection = map.get(key);%0A        if(collection == null
 ) %7B%0A
@@ -819,30 +819,27 @@
     
-map.put(key,
+collection =
  Lists.
-%3CV%3E
 newL
@@ -849,25 +849,24 @@
 edList()
-)
 ;%0A      
   %7D%0A    
@@ -861,32 +861,68 @@
     
-  %7D%0A
       
-  
 map.
-ge
+pu
 t(key
-)
+, collection);%0A        %7D%0A        collection
 .add
@@ -1385,32 +1385,71 @@
     
-if(!map.containsKey(key)
+Set%3CV%3E collection = map.get(key);%0A        if(collection == null
 ) %7B%0A
@@ -1464,29 +1464,26 @@
     
-map.put(key,
+collection =
  Sets.
-%3CV%3E
 newH
@@ -1490,25 +1490,24 @@
 ashSet()
-)
 ;%0A      
   %7D%0A    
@@ -1502,32 +1502,68 @@
     
-  %7D%0A
       
-  
 map.
-ge
+pu
 t(key
-)
+, collection);%0A        %7D%0A        collection
 .add

Make collection helpers more reliable with null values