diff --git a/src/main/java/com/samskivert/depot/util/Tuple2.java b/src/main/java/com/samskivert/depot/util/Tuple2.java index 4a7b6e5..01ceb5f 100644 --- a/src/main/java/com/samskivert/depot/util/Tuple2.java +++ b/src/main/java/com/samskivert/depot/util/Tuple2.java @@ -21,6 +21,8 @@ package com.samskivert.depot.util; import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; import com.google.common.base.Objects; @@ -53,6 +55,35 @@ public class Tuple2 implements Serializable }; } + /** + * Converts the supplied list of tuples {@code [(a, b), ...]} into a map {@code [a -> b]}. + * Useful in situations like so: + *
{@code
+     * Map results =
+     *   Tuple2.toMap(from(table).select(FooRecord.INT_KEY, FooRecord.STRING_VALUE))
+     * }
+ */ + public static Map toMap (Iterable> tuples) + { + return toMap(tuples, new HashMap()); + } + + /** + * Converts the supplied list of tuples {@code [(a, b), ...]} into a map {@code [a -> b]}, + * inserting the tuples into the supplied target map. Useful in situations like so: + *
{@code
+     * Map results =
+     *   Tuple2.toMap(from(table).select(FooRecord.INT_KEY, FooRecord.STRING_VALUE))
+     * }
+ */ + public static Map toMap (Iterable> tuples, Map target) + { + for (Tuple2 tuple : tuples) { + target.put(tuple.a, tuple.b); + } + return target; + } + /** Constructs an initialized two tuple. */ public Tuple2 (A a, B b) {