Add toMap to Access interface to simplify fast lookups

Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/src/com/google/gwtorm/client/Access.java b/src/com/google/gwtorm/client/Access.java
index ff8058e..98776ee 100644
--- a/src/com/google/gwtorm/client/Access.java
+++ b/src/com/google/gwtorm/client/Access.java
@@ -14,6 +14,8 @@
 
 package com.google.gwtorm.client;
 
+import java.util.Map;
+
 /**
  * Data access interface for an entity type.
  * <p>
@@ -55,6 +57,14 @@
   K primaryKey(T entity);
 
   /**
+   * Convert a collection of objects into a map, keyed by their primary key.
+   * 
+   * @param c the collection
+   * @return a map of the objects, indexed by their primary key.
+   */
+  Map<K, T> toMap(Iterable<T> c);
+
+  /**
    * Lookup a single entity via its primary key.
    * <p>
    * This method is only implemented if the entity's primary key is defined to
diff --git a/src/com/google/gwtorm/client/impl/AbstractAccess.java b/src/com/google/gwtorm/client/impl/AbstractAccess.java
index 237feef..9103773 100644
--- a/src/com/google/gwtorm/client/impl/AbstractAccess.java
+++ b/src/com/google/gwtorm/client/impl/AbstractAccess.java
@@ -21,6 +21,8 @@
 import com.google.gwtorm.client.Transaction;
 
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
 
 public abstract class AbstractAccess<E, K extends Key<?>, T extends AbstractTransaction>
     implements Access<E, K> {
@@ -35,6 +37,20 @@
     return new ListResultSet<E>(r);
   }
 
+  public Map<K, E> toMap(final Iterable<E> c) {
+    try {
+      final HashMap<K, E> r = new HashMap<K, E>();
+      for (final E e : c) {
+        r.put(primaryKey(e), e);
+      }
+      return r;
+    } finally {
+      if (c instanceof ResultSet) {
+        ((ResultSet<?>) c).close();
+      }
+    }
+  }
+
   public final void insert(final Iterable<E> instances) throws OrmException {
     doInsert(instances, null);
   }