Print

Print


Commit in lcsim/src/org/lcsim/util on MAIN
ListAddDriver.java+33-31.1 -> 1.2
MJC: Take into account unexpected interaction between metadata handling in generic routines vs BaseLCSimEvent.put()

lcsim/src/org/lcsim/util
ListAddDriver.java 1.1 -> 1.2
diff -u -r1.1 -r1.2
--- ListAddDriver.java	17 Feb 2006 01:57:03 -0000	1.1
+++ ListAddDriver.java	20 Apr 2007 22:44:56 -0000	1.2
@@ -9,16 +9,37 @@
 /**
  * Add a number of named lists together and write the combined list to the event.
  *
- * @version $Id: ListAddDriver.java,v 1.1 2006/02/17 01:57:03 mcharles Exp $
+ * @version $Id: ListAddDriver.java,v 1.2 2007/04/20 22:44:56 mcharles Exp $
  */
 
 public class ListAddDriver<T> extends Driver
 {
-    /** Simple constructor */
+    /** Simple constructor.
+     *
+     * WARNING: If you use this constructor and you try to merge lists of
+     * different subtypes (e.g. org.lcsim.recon.cluster.mipfinder.MIPCluster
+     * and org.lcsim.recon.cluster.nn.NearestNeighborCluster) then type
+     * information will be lost when the merged collection is uploaded to
+     * the event. This is due to the way metadata is handled in Event.
+     * To keep all type information, use the ListAddDriver<T>(Class<T> containedClass)
+     * constructor.
+     */
     public ListAddDriver () {
         super();
     }
 
+    /** More sophisticated constructor, with added type-safety.
+     *
+     * Here is an example of how to call this constructor, for
+     * a list of Cluster objects:
+     *    ListAddDriver<Cluster> = new ListAddDriver<Cluster>(Cluster.class); 
+     */	
+    Class<T> m_containedClass = null;
+    public ListAddDriver (Class<T> containedClass) {
+	super();
+	m_containedClass = containedClass;
+    }
+
     /** Process one event, performing merge. */
     public void process(EventHeader event)
     {
@@ -27,7 +48,16 @@
         List<T> currentList = (List<T>) (event.get(inputName));
         outputList.addAll(currentList);
       }
-      event.put(m_outputName, outputList);
+      if (m_containedClass == null) {
+	  // This is a little risky because event.put() is not fully
+	  // type-safe. It tries to infer the type from the first
+	  // elements of the list, and can get confused if there are
+	  // multiple subtypes present.
+	  event.put(m_outputName, outputList);
+      } else {
+	  // Now we know the intended class, this is fully type-safe.
+	  event.put(m_outputName, outputList, m_containedClass, 0);
+      }
     }
 
     /** Add one input list */
CVSspam 0.2.8