package org.arp.javautil.collections;

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

/**
 * Extra utilities for collections.
 * 
 * @author Andrew Post
 */
public class Collections {

    private Collections() {
    }

    /**
     * Puts a value into a map of key -> list of values. If the specified key
     * is new, it creates an {@link ArrayList} as its value.
     * @param map a {@link Map}.
     * @param key a key.
     * @param valueElt a value.
     */
    public static <K, V> void putList(Map<K, List<V>> map, K key, V valueElt) {
        if (map.containsKey(key)) {
            List<V> l = map.get(key);
            l.add(valueElt);
        } else {
            List<V> l = new ArrayList<V>();
            l.add(valueElt);
            map.put(key, l);
        }
    }

    /**
     * Puts a collection of values into a map of key -> list of values.
     * If the specified key is new, it creates an {@link ArrayList} as its
     * value.
     * @param map a {@link Map}.
     * @param key a key.
     * @param values a {@link Collection<? extends V>} of values.
     */
    public static <K, V> void putListAll(Map<K, List<V>> map, K key,
            Collection<? extends V> values) {
        for (V value : values)
            putList(map, key, value);
    }

    /**
     * Checks if any of a collection's elements are also in the provided set.
     *
     * @param aSet a {@link Set}.
     * @param aColl a {@link Collection}.
     * @return <code>true</code> or <code>false</code>.
     */
    public static <K> boolean containsAny(Set<K> aSet, Collection<K> aColl) {
        for (K obj : aColl) {
            if (aSet.contains(obj)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Checks whether any of an arrys's elements are also in the provided set.
     *
     * @param aSet a {@link Set}.
     * @param arr an array.
     * @return <code>true</code> or <code>false</code>.
     */
    public static <K> boolean containsAny(Set<K> aSet, K[] arr) {
        for (K obj : arr) {
            if (aSet.contains(obj)) {
                return true;
            }
        }
        return false;
    }
}

@@ -1301,16 +1301,18 @@
  values)
+ %7B
 %0A       
@@ -1342,16 +1342,26 @@
 value);%0A
+        %7D%0A
     %7D%0A%0A 
@@ -1381,18 +1381,23 @@
  Checks 
-if
+whether
  any of 
@@ -1397,27 +1397,23 @@
 any of a
- collection
+n arrys
 's eleme
@@ -1506,33 +1506,19 @@
 am a
-Coll a %7B@link Collection%7D
+rr an array
 .%0A  
@@ -1638,27 +1638,15 @@
 et, 
-Collection%3CK%3E aColl
+K%5B%5D arr
 ) %7B%0A
@@ -1667,20 +1667,18 @@
  obj : a
-Coll
+rr
 ) %7B%0A    
@@ -1812,86 +1812,122 @@
   * 
-Checks whether any of an arrys's elements are also in the provided set
+Sequentially adds the contents of the specified %3Ccode%3Ecollections%3C/code%3E%0A     * to %3Ccode%3Ecollection%3C/code%3E
 .%0A     *
 %0A   
@@ -1914,32 +1914,33 @@
 n%3C/code%3E.%0A     *
+ 
 %0A     * @param a
@@ -1942,62 +1942,141 @@
 ram 
-aSet a %7B@link Set%7D.%0A     * @param arr an array
+%3CK%3E%0A     * @param collection a %7B@link Collection%7D.%0A     * @param collections the %7B@link Collection%7Ds whose contents to insert
 .%0A     *
  @re
@@ -2075,35 +2075,27 @@
    *
- @return
  %3Ccode%3E
-true
+Null
 %3C/code%3E 
 or %3C
@@ -2090,37 +2090,43 @@
 %3C/code%3E 
-or %3Ccode%3Efalse%3C/code%3E
+collections will be skipped
 .%0A     *
@@ -2153,76 +2153,152 @@
 %3CK%3E 
-boolean containsAny(Set%3CK%3E aSet, K%5B%5D arr) %7B%0A        for (K obj : arr
+void addAll(Collection%3CK%3E collection,%0A            Collection%3C? extends K%3E... collections) %7B%0A        for (Collection%3C? extends K%3E c : collections
 ) %7B%0A
@@ -2313,34 +2313,25 @@
     if (
-aSet.contains(obj)
+c != null
 ) %7B%0A    
@@ -2338,35 +2338,44 @@
             
-return true
+collection.addAll(c)
 ;%0A          
@@ -2388,38 +2388,16 @@
       %7D%0A
-        return false;%0A
     %7D%0A%7D%0A

Prepare for introduction of disk caching.