If we could build upon guava I could make a version of pick() that

took a Function for computing weight. That would be more useful to me, usually.
The Map version could be reimplemented fairly easily with that...


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2921 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray.j.greenwell
2010-10-15 02:25:33 +00:00
parent b77b1c72a6
commit 34f0042956
@@ -211,6 +211,30 @@ public class Randoms
return pick;
}
// public <T> T pick (
// Iterable<? extends T> iterable, Function<? super T, ? extends Number> weightFunction,
// T ifEmpty)
// {
// T pick = ifEmpty;
// double total = 0.0;
// for (T element : iterable) {
// double weight = weightFunction.apply(element).doubleValue();
// // The rest is like the current pick(Map)
// }
// return pick;
// }
//
// public <T> T pick (Map<? extends T, ? extends Number> weightMap, T ifEmpty)
// {
// Map.Entry<? extends T, ? extends Number> pick = pick(weightMap.entrySet(),
// new Function<Map.Entry<?, ? extends Number>, Number>() {
// public Number apply (Map.Entry<?, ? extends Number> entry) {
// return entry.getValue();
// }
// }, null);
// return (pick != null) ? pick.getKey() : ifEmpty;
// }
/**
* Pluck (remove) a random element from the specified Iterable, or return <code>ifEmpty</code>
* if it is empty.