GeomConverter/src/org/lcsim/detector/identifier
diff -N IIdentifierContext.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ IIdentifierContext.java 13 Oct 2007 00:36:44 -0000 1.1
@@ -0,0 +1,64 @@
+package org.lcsim.detector.identifier;
+
+/**
+ * IdContext defines a set of Identifier field indices that are
+ * applicable in a given context, such as a certain level of the
+ * detector hierarchy. The simplest way to define an IdContext
+ * is with start and end indices. An IdContext can also be defined
+ * as a discrete set index values that do not constitute a range.
+ * If a set of index values is found to constitute a range at construction
+ * time, then it will be treated as such, and {@link #isRange()} will return
+ * <code>true</code>.
+ *
+ * @author Jeremy McCormick
+ * @version $Id: IIdentifierContext.java,v 1.1 2007/10/13 00:36:44 jeremy Exp $
+ */
+
+public interface IIdentifierContext
+{
+ /**
+ * The set of discrete indices in this range.
+ * @return Int array of indices.
+ */
+ int[] getIndices();
+
+ /**
+ * The start index.
+ * @return The start index.
+ */
+ int getStartIndex();
+
+ /**
+ * The end index.
+ * @return The end index.
+ */
+ int getEndIndex();
+
+ /**
+ * Does this IdContext constitution a contiguous block of fields between
+ * the start and end indices?
+ * @return True if this is a range; false if not.
+ */
+ boolean isRange();
+
+ /**
+ * Check if <code>index</code> is within this context.
+ * @param index
+ * @return True if index is within this context; false if not.
+ */
+ boolean isValidIndex(int index);
+
+ /**
+ * The number of indices.
+ * @return The number of indices.
+ */
+ int getNumberOfIndices();
+
+ /**
+ * Get the index at position <code>i</code>.
+ * @param i
+ * @return The index at position <code>i</code>.
+ * @throws IllegalArgumentException if <code>i</code> is out of range.
+ */
+ int getIndex(int i);
+}
\ No newline at end of file
GeomConverter/src/org/lcsim/detector/identifier
diff -N IdentifierContext.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ IdentifierContext.java 13 Oct 2007 00:36:45 -0000 1.1
@@ -0,0 +1,94 @@
+package org.lcsim.detector.identifier;
+
+import java.util.Arrays;
+
+/**
+ * Implementation of {@link IdentifierContext}.
+ *
+ * @author Jeremy McCormick
+ * @version $Id: IdentifierContext.java,v 1.1 2007/10/13 00:36:45 jeremy Exp $
+ */
+
+public class IdentifierContext
+implements IIdentifierContext
+{
+ int[] indices;
+ int startIndex;
+ int endIndex;
+ boolean isRange;
+
+ public IdentifierContext(int startIndex, int endIndex)
+ {
+ if (startIndex > endIndex)
+ throw new IllegalArgumentException("startIndex bigger than endIndex!");
+ isRange = true;
+ this.startIndex = startIndex;
+ this.endIndex = endIndex;
+ indices = new int[endIndex - startIndex + 1];
+ int index = startIndex;
+ for (int i=0; i<indices.length; i++)
+ {
+ indices[i] = index;
+ ++index;
+ }
+ }
+
+ public IdentifierContext(int[] indices)
+ {
+ this.indices = new int[indices.length];
+ System.arraycopy(indices, 0, this.indices, 0, indices.length);
+ Arrays.sort(indices);
+ startIndex = indices[0];
+ endIndex = indices[indices.length - 1];
+ isRange = true;
+ for (int i=0; i<indices.length; i++)
+ {
+ if (i != indices.length - 1)
+ {
+ if (indices[i+1] != indices[i] + 1)
+ {
+ // This means the indices are not a continuous range.
+ isRange = false;
+ break;
+ }
+ }
+ }
+ }
+
+ public int getEndIndex()
+ {
+ return endIndex;
+ }
+
+ public int getStartIndex()
+ {
+ return startIndex;
+ }
+
+ public int[] getIndices()
+ {
+ return indices;
+ }
+
+ public boolean isRange()
+ {
+ return isRange;
+ }
+
+ public boolean isValidIndex(int index)
+ {
+ return Arrays.binarySearch(indices, index) > -1;
+ }
+
+ public int getIndex(int i)
+ {
+ if (i < 0 || i > getNumberOfIndices() - 1)
+ throw new IllegalArgumentException("The index " + i + " is invalid!");
+ return indices[i];
+ }
+
+ public int getNumberOfIndices()
+ {
+ return indices.length;
+ }
+}
\ No newline at end of file
GeomConverter/test/org/lcsim/detector/identifier
diff -u -r1.6 -r1.7
--- IdentifierHelperTest.java 28 Aug 2007 22:26:35 -0000 1.6
+++ IdentifierHelperTest.java 13 Oct 2007 00:36:45 -0000 1.7
@@ -10,7 +10,7 @@
* on {@link IIdentifierHelper}.
*
* @author Jeremy McCormick
- * @version $Id: IdentifierHelperTest.java,v 1.6 2007/08/28 22:26:35 jeremy Exp $
+ * @version $Id: IdentifierHelperTest.java,v 1.7 2007/10/13 00:36:45 jeremy Exp $
*/
public class IdentifierHelperTest
@@ -137,4 +137,50 @@
assertEquals( "Field packed id does not match compact!", id, check);
}
+
+ public void testIdContext()
+ {
+ int i1[] = new int[3];
+ i1[0] = 2;
+ i1[1] = 3;
+ i1[2] = 4;
+
+ IdentifierContext c1 = new IdentifierContext(i1);
+ assertTrue(c1.isRange());
+ assertTrue(c1.getStartIndex() == 2);
+ assertTrue(c1.getEndIndex() == 4);
+ assertTrue(c1.isValidIndex(2));
+ assertTrue(c1.isValidIndex(3));
+ assertTrue(c1.isValidIndex(4));
+ assertTrue(!c1.isValidIndex(0));
+ assertTrue(!c1.isValidIndex(5));
+ assertTrue(c1.getNumberOfIndices() == 3);
+
+ IdentifierContext c2 = new IdentifierContext(2,4);
+ assertTrue(c2.isRange());
+ assertTrue(c2.getStartIndex() == 2);
+ assertTrue(c2.getEndIndex() == 4);
+ assertTrue(c2.isValidIndex(2));
+ assertTrue(c2.isValidIndex(3));
+ assertTrue(c2.isValidIndex(4));
+ assertTrue(!c2.isValidIndex(0));
+ assertTrue(!c2.isValidIndex(5));
+ assertTrue(c2.getNumberOfIndices() == 3);
+
+ int i2[] = new int[3];
+ i2[0] = 0;
+ i2[1] = 2;
+ i2[2] = 4;
+ IdentifierContext c3 = new IdentifierContext(i2);
+ assertTrue(!c3.isRange());
+ assertTrue(c3.getStartIndex() == 0);
+ assertTrue(c3.getEndIndex() == 4);
+ assertTrue(c3.isValidIndex(0));
+ assertTrue(c3.isValidIndex(2));
+ assertTrue(c3.isValidIndex(4));
+ assertTrue(!c3.isValidIndex(5));
+ assertTrue(!c3.isValidIndex(1));
+ assertTrue(!c3.isValidIndex(3));
+ assertTrue(c3.getNumberOfIndices() == 3);
+ }
}
\ No newline at end of file