package nyc.angus.huffman.distribution;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import javax.annotation.Nonnull;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;

/**
 * Utility for calculating the frequency distribution of characters in a string.
 */
public class FrequencyDistribution {

	public FrequencyDistribution() {
		// Default constructor.
	}

	public Map<Character, Double> calculateFrequency(final String string) {
		if (string == null || string.isEmpty()) {
			return Collections.emptyMap();
		}

		final Multiset<Character> occurrences = countOccurencesFromString(string);

		return calculateFrequency(string, occurrences);
	}

	private Multiset<Character> countOccurencesFromString(@Nonnull final String string) {
		return countOccurencesFromStream(string.chars());
	}

	private Multiset<Character> countOccurencesFromStream(final IntStream charStream) {
		final Multiset<Character> occurrences = HashMultiset.<Character> create();

		charStream.sequential()//
				.forEach(c -> occurrences.add((char) c));

		return occurrences;
	}

	private Map<Character, Double> calculateFrequency(final String string, final Multiset<Character> occurrences) {
		final Map<Character, Double> distribution = new HashMap<>();

		final Stream<Character> stream = occurrences.stream();
		stream.//
		forEach(//
		c -> distribution.put(c, occurrences.count(c) / Double.valueOf(string.length())) //
		);

		return distribution;
	}

}

@@ -150,74 +150,8 @@
 eam;
-%0Aimport java.util.stream.Stream;%0A%0Aimport javax.annotation.Nonnull;
 %0A%0Aim
@@ -654,18 +654,26 @@
 mStr
-ing
+eam
 (string
+.chars()
 );%0A%0A
@@ -730,151 +730,8 @@
 %09%7D%0A%0A
-%09private Multiset%3CCharacter%3E countOccurencesFromString(@Nonnull final String string) %7B%0A%09%09return countOccurencesFromStream(string.chars());%0A%09%7D%0A%0A
 %09pri
@@ -1173,41 +1173,8 @@
 %0A%0A%09%09
-final Stream%3CCharacter%3E stream = 
 occu
@@ -1193,32 +1193,24 @@
 am()
-;
+//
 %0A%09%09
-stream.//%0A
 %09%09
+.
 forEach(
 //%0A%09
@@ -1205,21 +1205,16 @@
 forEach(
-//%0A%09%09
 c -%3E dis
@@ -1289,14 +1289,8 @@
 ()))
- //%0A%09%09
 );%0A%0A

remove un-necessary method