View Javadoc

1   package org.lcsim.geometry.util;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import static org.lcsim.geometry.IDDecoder.INVALID_INDEX;
7   
8   /**
9    * Takes a string of the form:
10   *
11   * <pre>
12   * layer:7,system:3,barrel:3,theta:32:11,phi:11
13   * </pre>
14   *
15   * and generates a parsed description of the fields.
16   *
17   * @author tonyj
18   */
19  public class IDDescriptor
20  {
21      private int[] start;
22      private int[] length;
23      private String[] name;
24      private Map<String,Integer> nameMap = new HashMap<String,Integer>();
25      private int maxBit;
26      private int nfields;
27      private String description;
28  
29      public IDDescriptor(String idDescriptor) throws IDException
30      {
31          this.description = idDescriptor;
32          try
33          {
34              String[] fields = idDescriptor.split(",");
35              //int n = fields.length;
36              nfields = fields.length;
37              start = new int[nfields];
38              length = new int[nfields];
39              name = new String[nfields];
40  
41              int pos = 0;
42              for (int i = 0; i < nfields; i++)
43              {
44                  String[] subFields = fields[i].split(":");
45                  if (subFields.length < 2 || subFields.length > 3)
46                      throw new RuntimeException("Invalid subfield: " + fields[i]);
47                  name[i] = subFields[0].trim();
48                  nameMap.put(name[i],i);
49                  if (subFields.length == 3)
50                  {
51                      start[i] = Integer.parseInt(subFields[1]);
52                      if (start[i] < 0)
53                          throw new RuntimeException(
54                                  "Invalid field start position: " + start[i]);
55                      length[i] = Integer.parseInt(subFields[2]);
56                      if (length[i] == 0)
57                          throw new RuntimeException("Invalid field length: "
58                                  + start[i]);
59                  }
60                  else
61                  {
62                      start[i] = pos;
63                      length[i] = Integer.parseInt(subFields[1]);
64                  }
65                  pos = start[i] + Math.abs(length[i]);
66                  if (pos > maxBit)
67                      maxBit = pos;
68              }
69          }
70          catch (RuntimeException x)
71          {
72              throw new IDException("Invalid id descriptor: " + idDescriptor, x);
73          }
74      }
75  
76      public int fieldCount()
77      {
78          return name.length;
79      }
80      /**
81       * Returns the index of the specified field.
82       * @throws IllegalArgumentException If the requested field does not exist.
83       */
84      public int indexOf(String name) throws IllegalArgumentException
85      {
86          if (nameMap.get(name) != null)
87          {
88              return nameMap.get(name);
89          }
90          else
91          {
92              throw new IllegalArgumentException("Invalid field name: "+name);
93          }
94      }
95  
96      public int fieldStart(int index)
97      {
98          return start[index];
99      }
100 
101     public int fieldLength(int index)
102     {
103         return Math.abs(length[index]);
104     }
105 
106     public boolean isSigned(int index)
107     {
108         return length[index] < 0;
109     }
110 
111     public String fieldName(int index)
112     {
113         return name[index];
114     }
115 
116     public int getMaxBit()
117     {
118         return maxBit;
119     }
120 
121     public int size()
122     {
123         return nfields;
124     }
125 
126     public int maxIndex()
127     {
128         return nfields - 1;
129     }
130 
131     public static class IDException extends Exception
132     {
133         IDException(String message, Throwable cause)
134         {
135             super(message, cause);
136         }
137     }
138     
139     public String toString()
140     {
141         return description;
142     }
143 }