package common;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

public class Sets {
	
  /**
   * <p>The factor by which the expected size of a {@link java.util.HashSet HashSet} should be
   * multiplied went sent to HashSet's constructor as the initial capacity, if using the default
   * load factor.</p>
   * @see http://docs.oracle.com/javase/tutorial/collections/implementations/set.html
   */
	public static final int JAVA_UTIL_HASHSET_SIZE_FACTOR = 2;
	
  /**
   * <p>Adds {@code c1} to {@code c2} and returns {@code c2}. This is intended to be used as a
   * {@link java.util.stream.Collector#combiner() combiner} for a
   * {@link java.util.stream.Collector Collector}.</p>
   * @param <E> the parameter type of {@code c1} and {@code c2}
   * @param <C> the type of {@code c1} and {@code c2}
   * @param c1 a collection whose contents are to be combined with those of {@code c2}
   * @param c2 a collection whose contents are to be combined with those of {@code c1}
   * @return {@code c2} after the contents of {@code c1} are added to it
   */
	public static <E, C extends Collection<E>> C mergeCollections(C c1, C c2){
		c2.addAll(c1);
		return c2;
	}
	
  /**
   * <p>Unions the collections in {@code collections} and counts how many times each element is
   * present.</p>
   * @param <T> the type of the elements being counted
   * @param collections
   * @return a Map from each element present in the collections in {@code collections} to the
   * number of times each such element occurs among the collections in {@code collections}.
   */
	public static <T> Map<T, Integer> countingUnion(Collection<? extends Collection<T>> collections){
		return countingUnion(collections.stream());
	}
	
	public static <T> Map<T, Integer> countingUnion(Stream<? extends Collection<T>> collections){
		Map<T, Integer> result = new HashMap<>();
		
		collections.forEach((collection) -> {
			for(T t : collection){
				result.put(t, result.containsKey(t) 
						? 1 + result.get(t) 
						: 1);
			}
		});
		
		return result;
	}
}

@@ -1418,17 +1418,68 @@
 lections
+ a stream of collections whose elements are counted
 %0A
-
    * @re
@@ -1618,38 +1618,18 @@
 among th
-e collections in %7B@cod
+os
 e collec
@@ -1637,167 +1637,15 @@
 ions
-%7D.
 %0A   */%0A
-%09public static %3CT%3E Map%3CT, Integer%3E countingUnion(Collection%3C? extends Collection%3CT%3E%3E collections)%7B%0A%09%09return countingUnion(collections.stream());%0A%09%7D%0A%09%0A
 %09pub

remove unused countingUnion method in Sets