1 package org.lcsim.detector.identifier;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6
7
8
9
10
11
12
13 public class ExpandedIdentifier
14 implements IExpandedIdentifier
15 {
16 List<Integer> values = new ArrayList<Integer>();
17
18 public ExpandedIdentifier()
19 {}
20
21 public ExpandedIdentifier(IExpandedIdentifier id)
22 {
23 values.addAll(id.getValues());
24 }
25
26 public ExpandedIdentifier(ExpandedIdentifier id)
27 {
28 values.addAll(id.getValues());
29 }
30
31 public ExpandedIdentifier copy()
32 {
33 return new ExpandedIdentifier(this);
34 }
35
36 public ExpandedIdentifier( int reserve )
37 {
38 if (reserve > 64)
39 {
40 throw new RuntimeException("cannot reserve more than 64 fields");
41 }
42
43 if (reserve > 0)
44 {
45 for (int i=0; i<reserve; i++)
46 {
47 addValue(0);
48 }
49 }
50 }
51
52 public ExpandedIdentifier( int[] values )
53 {
54 for ( int value : values )
55 {
56 this.values.add( value );
57 }
58 }
59
60 public ExpandedIdentifier( int[] values, int start )
61 {
62 if ( start >= values.length )
63 {
64 throw new IllegalArgumentException("Start index <" + start + "> is invalid.");
65 }
66
67 for ( int i=start; i<values.length; i++ )
68 {
69 this.values.add( values[start] );
70 }
71 }
72
73 public ExpandedIdentifier( List<Integer> values )
74 {
75 this.values.addAll( values );
76 }
77
78 public ExpandedIdentifier( List<Integer> values, int start )
79 {
80 if ( start >= values.size() )
81 {
82 throw new IllegalArgumentException("Start index<" + start + "> is invalid.");
83 }
84
85 for ( int i=start; i<values.size(); i++ )
86 {
87 this.values.add( values.get( i ) );
88 }
89 }
90
91 public ExpandedIdentifier( String values )
92 {
93 String[] buffer = values.split("/");
94 for ( String value : buffer )
95 {
96 if ( !value.equals(""))
97 {
98 addValue( Integer.parseInt( value ) );
99 }
100 }
101 }
102
103 public void addValue( int value)
104 {
105 values.add(value);
106 }
107
108 public void setValue(int idx, int value)
109 {
110 values.set(idx,value);
111 }
112
113 public void clear()
114 {
115 values.clear();
116 }
117
118 public int size()
119 {
120 return values.size();
121 }
122
123 public int getValue( int index )
124 {
125 return values.get( index );
126 }
127
128 public List<Integer> getValues()
129 {
130 return values;
131 }
132
133 public boolean isValid()
134 {
135 return values.size() != 0;
136 }
137
138 public String toString()
139 {
140 StringBuffer buff = new StringBuffer();
141 for ( Integer value : values )
142 {
143 buff.append("/" + value);
144 }
145 return buff.toString();
146 }
147
148 public boolean equals(IExpandedIdentifier id)
149 {
150 if ( id.size() != this.size() )
151 {
152 return false;
153 }
154
155 for ( int i=0; i<this.size(); i++)
156 {
157 if ( id.getValue(i) != this.getValue(i))
158 {
159 return false;
160 }
161 }
162 return true;
163 }
164
165 public int hashCode()
166 {
167 return values.hashCode();
168 }
169
170 public boolean equals(Object object)
171 {
172 if (object instanceof IExpandedIdentifier)
173 {
174 return equals((IExpandedIdentifier)object);
175 }
176 else
177 {
178 return false;
179 }
180 }
181
182 public int match( IExpandedIdentifier id )
183 {
184 int maxSize = ( size() > id.size() ? id.size() : size() );
185 for ( int i=0; i<maxSize; i++ )
186 {
187 int compare = Integer.valueOf( getValue( i ) ).compareTo( id.getValue( i ) );
188 if ( compare != 0 )
189 {
190 return compare;
191 }
192 }
193 return 0;
194 }
195
196 public int getMaxIndex()
197 {
198 return size() - 1;
199 }
200
201 public boolean isValidIndex(int i)
202 {
203 return i <= getMaxIndex();
204 }
205
206 public int compareField( IExpandedIdentifier id, int idx )
207 {
208 if ( idx > getMaxIndex() || idx > id.getMaxIndex() )
209 {
210 throw new IllegalArgumentException( "The index argument <" + idx + "> is out of range.");
211 }
212 return Integer.valueOf( getValue( idx ) ).compareTo( id.getValue( idx ) );
213 }
214 }