From 34f00429563ae70acd49458f8538e8846d4c0ce9 Mon Sep 17 00:00:00 2001 From: "ray.j.greenwell" Date: Fri, 15 Oct 2010 02:25:33 +0000 Subject: [PATCH] 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 --- .../java/com/samskivert/util/Randoms.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/com/samskivert/util/Randoms.java b/src/main/java/com/samskivert/util/Randoms.java index c82886f9..c19cbb27 100644 --- a/src/main/java/com/samskivert/util/Randoms.java +++ b/src/main/java/com/samskivert/util/Randoms.java @@ -211,6 +211,30 @@ public class Randoms return pick; } +// public T pick ( +// Iterable iterable, Function 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 pick (Map weightMap, T ifEmpty) +// { +// Map.Entry pick = pick(weightMap.entrySet(), +// new Function, Number>() { +// public Number apply (Map.Entry entry) { +// return entry.getValue(); +// } +// }, null); +// return (pick != null) ? pick.getKey() : ifEmpty; +// } + /** * Pluck (remove) a random element from the specified Iterable, or return ifEmpty * if it is empty.