Commit in GeomConverter/src/org/lcsim/geometry/util on MAIN
IDDescriptor.java+125-801.3 -> 1.4
JM: Add warning message if id field is not found.  Return -1 instead of NullPointerException.

GeomConverter/src/org/lcsim/geometry/util
IDDescriptor.java 1.3 -> 1.4
diff -u -r1.3 -r1.4
--- IDDescriptor.java	10 May 2005 18:25:13 -0000	1.3
+++ IDDescriptor.java	21 Jan 2006 02:29:38 -0000	1.4
@@ -5,89 +5,134 @@
 
 /**
  * Takes a string of the form:
- * <pre>layer:7,system:3,barrel:3,theta:32:11,phi:11</pre>
+ * 
+ * <pre>
+ * layer:7,system:3,barrel:3,theta:32:11,phi:11
+ * </pre>
+ * 
  * and generates a parsed description of the fields.
+ * 
  * @author tonyj
  */
 public class IDDescriptor
 {
-   private int[] start;
-   private int[] length;
-   private String[] name;
-   private Map nameMap = new HashMap();
-   private int maxBit;
-   
-   public IDDescriptor(String idDescriptor) throws IDException
-   {
-      try
-      {
-         String[] fields = idDescriptor.split(",");
-         int n = fields.length;
-         start = new int[n];
-         length = new int[n];
-         name = new String[n];
-         
-         int pos = 0;
-         for (int i=0; i<n; i++)
-         {
-            String[] subFields = fields[i].split(":");
-            if (subFields.length<2 || subFields.length>3) throw new RuntimeException("Invalid subfield: "+fields[i]);
-            name[i] = subFields[0].trim();
-            nameMap.put(name[i],new Integer(i));
-            if (subFields.length == 3)
-            {
-               start[i] = Integer.parseInt(subFields[1]);
-               if (start[i] < 0) throw new RuntimeException("Invalid field start position: "+start[i]);
-               length[i] = Integer.parseInt(subFields[2]);
-               if (length[i] == 0) throw new RuntimeException("Invalid field length: "+start[i]);
-            }
-            else
-            {
-               start[i] = pos;
-               length[i] = Integer.parseInt(subFields[1]);
-            }
-            pos = start[i]+Math.abs(length[i]);
-            if (pos > maxBit) maxBit = pos;
-         }
-      }
-      catch (RuntimeException x)
-      {
-         throw new IDException("Invalid id descriptor: "+idDescriptor,x);
-      }
-   }
-   public int fieldCount()
-   {
-      return name.length;
-   }
-   public int indexOf(String name)
-   {
-      return ((Number) nameMap.get(name)).intValue();
-   }
-   public int fieldStart(int index)
-   {
-      return start[index];
-   }
-   public int fieldLength(int index)
-   {
-      return Math.abs(length[index]);
-   }
-   public boolean isSigned(int index)
-   {
-      return length[index] < 0;
-   }
-   public String fieldName(int index)
-   {
-      return name[index];
-   }
-   public int getMaxBit()
-   {
-      return maxBit;
-   }
-   public static class IDException extends Exception
-   {
-      IDException(String message, Throwable cause)
-      {
-         super(message,cause);
-      }
-   }
+	private int[] start;
+
+	private int[] length;
+
+	private String[] name;
+
+	private Map nameMap = new HashMap();
+
+	private int maxBit;
+	
+	private int nfields;
+	
+	private static int INVALID_INDEX = -1;
+
+	public IDDescriptor(String idDescriptor) throws IDException
+	{
+		try
+		{
+			String[] fields = idDescriptor.split(",");
+			//int n = fields.length;
+			nfields = fields.length;
+			start = new int[nfields];
+			length = new int[nfields];
+			name = new String[nfields];
+
+			int pos = 0;
+			for (int i = 0; i < nfields; i++)
+			{
+				String[] subFields = fields[i].split(":");
+				if (subFields.length < 2 || subFields.length > 3)
+					throw new RuntimeException("Invalid subfield: " + fields[i]);
+				name[i] = subFields[0].trim();
+				nameMap.put(name[i], new Integer(i));
+				if (subFields.length == 3)
+				{
+					start[i] = Integer.parseInt(subFields[1]);
+					if (start[i] < 0)
+						throw new RuntimeException(
+								"Invalid field start position: " + start[i]);
+					length[i] = Integer.parseInt(subFields[2]);
+					if (length[i] == 0)
+						throw new RuntimeException("Invalid field length: "
+								+ start[i]);
+				} else
+				{
+					start[i] = pos;
+					length[i] = Integer.parseInt(subFields[1]);
+				}
+				pos = start[i] + Math.abs(length[i]);
+				if (pos > maxBit)
+					maxBit = pos;
+			}
+		} catch (RuntimeException x)
+		{
+			throw new IDException("Invalid id descriptor: " + idDescriptor, x);
+		}
+	}
+
+	public int fieldCount()
+	{
+		return name.length;
+	}
+
+	public int indexOf(String name) 
+	{
+		int idx = INVALID_INDEX;
+		if (nameMap.get(name) != null)
+		{
+			idx = ((Number) nameMap.get(name)).intValue();
+		}
+		else 
+		{
+			System.err.println("WARNING: Requested field " + name + " does not exist in this IDDescriptor");
+		}
+		return idx;
+	}
+
+	public int fieldStart(int index)
+	{
+		return start[index];
+	}
+
+	public int fieldLength(int index)
+	{
+		return Math.abs(length[index]);
+	}
+
+	public boolean isSigned(int index)
+	{
+		return length[index] < 0;
+	}
+
+	public String fieldName(int index)
+	{
+		return name[index];
+	}
+
+	public int getMaxBit()
+	{
+		return maxBit;
+	}
+	
+	public int size()
+	{
+		return nfields;
+	}
+	
+	public int maxIndex()
+	{
+		return nfields - 1;
+	}
+
+	public static class IDException extends Exception
+	{
+		IDException(String message, Throwable cause)
+		{
+			super(message, cause);
+		}
+	}
 }
CVSspam 0.2.8