GeomConverter/src/org/lcsim/detector
diff -u -r1.4 -r1.5
--- IReadout.java 7 May 2007 22:32:48 -0000 1.4
+++ IReadout.java 30 Jan 2008 00:44:06 -0000 1.5
@@ -6,16 +6,15 @@
* The {@link IReadout} provides access to hit objects
* from {@link IDetectorElement} objects. Since there
* is no generic class for digits or hits, this class
- * provides access to readout based on template parameters.
+ * provides access to hits based on template parameters.
*
* @author jeremym
- * @version $Id: IReadout.java,v 1.4 2007/05/07 22:32:48 jeremy Exp $
+ * @version $Id: IReadout.java,v 1.5 2008/01/30 00:44:06 jeremy Exp $
*/
public interface IReadout
{
/**
- * Get a reference to the list storing hits of type T.
- *
+ * Get a list of hits matching type T.
* @param klass The class of the list to return.
* @return A {@link List} containing the hits or containing
* nothing if no hits have been added.
@@ -23,14 +22,14 @@
public <T> List<T> getHits(Class<T> klass);
/**
- * Add a hit of type T.
+ * Add a hit.
*
* @param hit The hit to add.
*/
- public <T> void addHit(T hit);
+ public void addHit(Object hit);
/**
- * Clear the state of this {@link IReadout}.
+ * Clear the hits.
*/
public void clear();
}
\ No newline at end of file
GeomConverter/src/org/lcsim/detector
diff -u -r1.4 -r1.5
--- Readout.java 7 May 2007 23:38:34 -0000 1.4
+++ Readout.java 30 Jan 2008 00:44:06 -0000 1.5
@@ -1,52 +1,40 @@
package org.lcsim.detector;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
/**
* Simple implementation that of {@link IReadout}
- * that stores lists of hits by type.
- *
+ * that stores a list of Objects retrievable by
+ * class.
* @see IReadout
- *
*/
public class Readout
implements IReadout
{
- Map<Class,List> hits = new HashMap<Class,List>();
+ List hits = new ArrayList();
- public <T> List<T> getHits(Class<T> klass)
- {
- if ( !hits.containsKey( klass) )
+ /**
+ * Get all hits of Class <code>klass</code>.
+ * @return A new typed List containing matching hits.
+ */
+ public <T> List<T> getHits(Class<T> klass)
+ {
+ List<T> matches = new ArrayList<T>();
+ for (Object hit : hits)
{
- hits.put( klass, new ArrayList<T>() );
+ if (klass.isAssignableFrom(hit.getClass()))
+ {
+ matches.add((T)hit);
+ }
+
}
- return (List<T>)hits.get( klass );
- }
-
- public <T> void addHit(T hit)
- {
- // FIXME: Uses first interface which is liable to break easily!
- // Works okay for SimTrackerHit and CalorimeterHit.
- List addList = getHits(hit.getClass().getInterfaces()[0]);
- addList.add(hit);
+ return matches;
}
- public String toString()
+ public void addHit(Object hit)
{
- StringBuffer str = new StringBuffer();
- for ( Class k : hits.keySet() )
- {
- str.append(k.getCanonicalName() + '\n');
- List val = hits.get(k);
- for ( Object v : val )
- {
- str.append(" " + v.toString() + '\n');
- }
- }
- return str.toString();
+ hits.add(hit);
}
public void clear()
GeomConverter/test/org/lcsim/detector
diff -N ReadoutTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ReadoutTest.java 30 Jan 2008 00:44:06 -0000 1.1
@@ -0,0 +1,28 @@
+package org.lcsim.detector;
+
+import junit.framework.TestCase;
+
+public class ReadoutTest extends TestCase
+{
+ public interface TestInterface
+ {}
+
+ public class TestClass implements TestInterface
+ {}
+
+ public class TestClass2 extends TestClass
+ {}
+
+ public class TestClass3
+ {}
+
+ public void testHits()
+ {
+ Readout readout = new Readout();
+ readout.addHit(new TestClass2());
+ assertTrue(readout.getHits(TestInterface.class).size() == 1);
+ assertTrue(readout.getHits(TestClass.class).size() == 1);
+ assertTrue(readout.getHits(TestClass2.class).size() == 1);
+ assertTrue(readout.getHits(TestClass3.class).size() == 0);
+ }
+}