View Javadoc

1   package org.lcsim.geometry.util;
2   
3   /**
4    *
5    * @author tonyj
6    */
7   public class IDDecoder
8   {
9       private IDDescriptor desc;
10      private long id;
11      
12      /** Creates a new instance of IIDecoder */
13      public IDDecoder(IDDescriptor desc)
14      {
15          this.desc = desc;
16      }
17      
18      public void setID(long id)
19      {
20          this.id = id;
21      }
22      
23      public long getID()
24      {
25      	return this.id;
26      }
27          
28      public int getFieldIndex(String name)
29      {
30          return desc.indexOf(name);
31      }
32      
33      public int getValue(String name)
34      {
35          return getValue(desc.indexOf(name));
36      }
37      
38      public int getValue(int index)
39      {
40          int start = desc.fieldStart(index);
41          int length = desc.fieldLength(index);
42          int mask = (1<<length) - 1;
43          
44          int result = (int) ((id >> start) & mask);
45          if (desc.isSigned(index))
46          {
47              int signBit = 1<<(length-1);
48              if ((result & signBit) != 0) result -= (1<<length);
49          }
50          return result;
51      }
52      
53      public int[] getValues(int[] buffer)
54      {
55          if (buffer.length != desc.fieldCount()) throw new IllegalArgumentException("Invalid buffer length");
56          for (int i=0; i<buffer.length; i++)
57          {
58              buffer[i] = getValue(i);
59          }
60          return buffer;
61      }
62      
63      public int getFieldCount()
64      {
65          return desc.fieldCount();
66      }
67      
68      public String getFieldName(int index)
69      {
70          return desc.fieldName(index);
71      }
72      
73      public String toString()
74      {
75          StringBuffer sb = new StringBuffer();
76          for (int i=0; i<desc.fieldCount();)
77          {
78              sb.append(desc.fieldName(i));
79              sb.append(':');
80              sb.append(getValue(i));
81              if (++i >= desc.fieldCount()) break;
82              sb.append(',');
83          }
84          return sb.toString();
85      }
86  }