MultisetUtil.

Originally lived in projectx and had a bunch more interesting stuff.
But the code that used it (gates, minerals, themes) went away because
all that mineral stuff went away.

Kept what's still in use.  In particular, singleton() is quite useful
(really, Google should add it to Multisets or ImmutableMultiset).

The count ordering stuff is perhaps semi-deprecated, since Google has
added Multisets.copyHighestCountFirst().
This commit is contained in:
Ray J. Greenwell
2015-01-29 15:55:13 -08:00
parent f490642f3d
commit c98e7a86ee
@@ -0,0 +1,49 @@
//
// ooo-util - a place for OOO utilities
// Copyright (C) 2011 Three Rings Design, Inc., All Rights Reserved
// http://github.com/threerings/ooo-util/blob/master/LICENSE
package com.threerings.util;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.Multiset;
import com.google.common.collect.Ordering;
import com.google.common.primitives.Ints;
/**
* Convenience functions for working with Multisets.
*/
public class MultisetUtil
{
/** An Ordering that compares Multiset.Entrys by count. */
public static final Ordering<Multiset.Entry<?>> ENTRY_COUNT_ORDERING =
new Ordering<Multiset.Entry<?>>() {
public int compare (Multiset.Entry<?> left, Multiset.Entry<?> right) {
return Ints.compare(left.getCount(), right.getCount());
}
};
/**
* Return a Function that will transform objects to their corresponding count in the
* specified Multiset.
*/
public static Function<Object, Integer> countFunction (final Multiset<?> set)
{
return new Function<Object, Integer>() {
public Integer apply (Object o) {
return set.count(o);
}
};
}
/**
* Create an ImmutableMultiset with a single element with the specified count.
*/
public static <T> ImmutableMultiset<T> singleton (T element, int count)
{
return ImmutableMultiset.<T>builder()
.addCopies(element, count)
.build();
}
}