lcsim/src/org/lcsim/recon/cluster/util
diff -N UnwrappableObjectException.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ UnwrappableObjectException.java 2 Jan 2006 21:17:50 -0000 1.1
@@ -0,0 +1,14 @@
+package org.lcsim.recon.cluster.util;
+
+import java.lang.String;
+
+/**
+ * An exception thrown if we don't know how to wrap an object into a Cluster.
+ *
+ * @version $Id: UnwrappableObjectException.java,v 1.1 2006/01/02 21:17:50 mcharles Exp $
+ */
+
+public class UnwrappableObjectException extends java.lang.Exception {
+ public UnwrappableObjectException(String m) { super(m); }
+}
+
lcsim/src/org/lcsim/recon/cluster/util
diff -N ObjectToClusterWrapper.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ObjectToClusterWrapper.java 2 Jan 2006 21:17:50 -0000 1.1
@@ -0,0 +1,34 @@
+package org.lcsim.recon.cluster.util;
+
+import java.util.List;
+import org.lcsim.event.Cluster;
+
+/**
+ * Interface describing a wrapper that takes an arbitrary object and
+ * turns it into a cluster (or takes a list of objects and turns it
+ * into a list of clusters). If it can't handle an object, it throws
+ * an UnwrappableObjectException.
+ *
+ * @version $Id: ObjectToClusterWrapper.java,v 1.1 2006/01/02 21:17:50 mcharles Exp $
+ */
+
+public interface ObjectToClusterWrapper
+{
+ /**
+ * Wrap one object into a cluster
+ */
+ public Cluster wrapObject(Object obj) throws UnwrappableObjectException;
+
+ /**
+ * Wrap a list of objects into a list of clusters.
+ */
+ public List<Cluster> wrapListOfObjects(List inputList) throws UnwrappableObjectException;
+
+ /**
+ * Wrap a list of objects into a list of clusters, allowing user to
+ * specify the empty list (outputList) which will hold the clusters.
+ */
+ public List<Cluster> wrapListOfObjects(List inputList, List<Cluster> outputList) throws UnwrappableObjectException;
+
+}
+
lcsim/src/org/lcsim/recon/cluster/util
diff -N BasicObjectToClusterWrapper.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ BasicObjectToClusterWrapper.java 2 Jan 2006 21:17:50 -0000 1.1
@@ -0,0 +1,83 @@
+package org.lcsim.recon.cluster.util;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import org.lcsim.recon.cluster.util.BasicCluster;
+import org.lcsim.event.CalorimeterHit;
+import org.lcsim.event.Cluster;
+
+/**
+ * Interface describing a class that takes an object and turns it
+ * into a cluster (or takes a list of objects and turns it
+ * into a list of clusters). If it can't handle an object, it throws
+ * an UnwrappableObjectException. In this implementation, the
+ * input objects must be instances of Cluster or CalorimeterHit.
+ *
+ * @version $Id: BasicObjectToClusterWrapper.java,v 1.1 2006/01/02 21:17:50 mcharles Exp $
+ */
+
+public class BasicObjectToClusterWrapper implements ObjectToClusterWrapper
+{
+ /**
+ * Simple constructor
+ */
+ public BasicObjectToClusterWrapper() {}
+
+ /**
+ * Wrap one object (Cluster or CalorimeterHit) into a Cluster
+ */
+ public Cluster wrapObject(Object obj) throws UnwrappableObjectException
+ {
+ if (obj instanceof Cluster) {
+ // Already a cluster
+ Cluster clus = (Cluster) obj;
+ return clus;
+ } else if (obj instanceof CalorimeterHit) {
+ CalorimeterHit hit = (CalorimeterHit) obj;
+ return wrapCalorimeterHit(hit);
+ } else {
+ throw new UnwrappableObjectException("Don't knowhow to wrap class "+obj.getClass().getName());
+ }
+ }
+
+ /**
+ * Wrap a list of objects (Cluster or CalorimeterHit) into a newly
+ * created list of Clusters.
+ */
+ public List<Cluster> wrapListOfObjects(List inputList) throws UnwrappableObjectException {
+ List<Cluster> emptyList = new ArrayList<Cluster>();
+ try {
+ return wrapListOfObjects(inputList, emptyList);
+ } catch (UnwrappableObjectException x) {
+ throw x;
+ }
+ }
+
+ /**
+ * Wrap a list of objects into a list of clusters, allowing user to
+ * specify the empty list (outputList) which will hold the clusters.
+ */
+ public List<Cluster> wrapListOfObjects(List inputList, List<Cluster> outputList) throws UnwrappableObjectException {
+ outputList.clear();
+ if (!outputList.isEmpty()) { throw new AssertionError("Non-empty output list at start of method."); }
+ for (Object obj : inputList) {
+ try {
+ Cluster clus = wrapObject(obj);
+ outputList.add(clus);
+ } catch (UnwrappableObjectException x) {
+ throw x;
+ }
+ }
+ return outputList;
+ }
+
+ /**
+ * Internal utility routine to wrap a CalorimeterHit into a Cluster
+ */
+ protected Cluster wrapCalorimeterHit(CalorimeterHit hit) {
+ BasicCluster clus = new BasicCluster();
+ clus.addHit(hit);
+ return clus;
+ }
+}