/**
 * 
 */
package codemining.util;

import java.util.Set;
import java.util.SortedSet;

import com.google.common.collect.Multiset;
import com.google.common.collect.Multiset.Entry;
import com.google.common.collect.Multisets;
import com.google.common.collect.Sets;

/**
 * A utility class containing collection-related utilities.
 * 
 * @author Miltos Allamanis <m.allamanis@ed.ac.uk>
 * 
 */
public class CollectionUtil {

	/**
	 * Return the elements that have been seen at least nSeen times.
	 * 
	 * @param nSeen
	 * @param baseMultiset
	 * @return
	 */
	public static <T> Set<T> getElementsUpToCount(final int nSeen,
			final Multiset<T> baseMultiset) {
		final Set<T> toKeep = Sets.newHashSet();
		for (final Entry<T> entry : Multisets.copyHighestCountFirst(
				baseMultiset).entrySet()) {
			if (entry.getCount() < nSeen) {
				break;
			}
			toKeep.add(entry.getElement());
		}
		return toKeep;
	}

	/**
	 * Return the top elements of a sorted set.
	 * 
	 * @param originalSet
	 * @param nTopElements
	 * @return
	 */
	public static <T extends Comparable<T>> SortedSet<T> getTopElements(
			final SortedSet<T> originalSet, final int nTopElements) {
		final SortedSet<T> filteredElements = Sets.newTreeSet();
		int i = 0;
		for (final T element : originalSet) {
			if (i > nTopElements) {
				break;
			}
			filteredElements.add(element);
			i++;
		}
		return filteredElements;
	}

}

@@ -31,16 +31,38 @@
 .util;%0A%0A
+import java.util.Map;%0A
 import j
@@ -71,24 +71,24 @@
 a.util.Set;%0A
-
 import java.
@@ -104,16 +104,110 @@
 edSet;%0A%0A
+import com.google.common.base.Functions;%0Aimport com.google.common.collect.ImmutableSortedMap;%0A
 import c
@@ -290,16 +290,16 @@
 .Entry;%0A
-
 import c
@@ -334,16 +334,59 @@
 tisets;%0A
+import com.google.common.collect.Ordering;%0A
 import c
@@ -1541,10 +1541,790 @@
 ts;%0A%09%7D%0A%0A
+%09/** Sort map by value (lowest first) */%0A%09public static %3CK extends Comparable%3CK%3E, V extends Comparable%3CV%3E%3E Map%3CK, V%3E sortMapByValueAscending(%0A%09%09%09final Map%3CK, V%3E map) %7B%0A%09%09final Ordering%3CK%3E valueThenKeyComparator = Ordering.natural()%0A%09%09%09%09.onResultOf(Functions.forMap(map))%0A%09%09%09%09.compound(Ordering.%3CK%3E natural());%0A%09%09return ImmutableSortedMap.copyOf(map, valueThenKeyComparator);%0A%09%7D%0A%0A%09/** Sort map by value (highest first) */%0A%09public static %3CK extends Comparable%3CK%3E, V extends Comparable%3CV%3E%3E Map%3CK, V%3E sortMapByValueDescending(%0A%09%09%09final Map%3CK, V%3E map) %7B%0A%09%09final Ordering%3CK%3E valueThenKeyComparator = Ordering.natural().reverse()%0A%09%09%09%09.onResultOf(Functions.forMap(map))%0A%09%09%09%09.compound(Ordering.%3CK%3E natural().reverse());%0A%09%09return ImmutableSortedMap.copyOf(map, valueThenKeyComparator);%0A%09%7D%0A%0A
 %7D%0A

Add generic map sorting functions