1 8 9 package net.sourceforge.chaperon.model.symbol; 10 11 import java.io.Serializable ; 12 13 19 public class SymbolList implements SymbolCollection, Serializable , Cloneable 20 { 21 private int capacityIncrement = 100; 22 private int elementCount = 0; 23 private Symbol[] list = new Symbol[10]; 24 25 28 public SymbolList() {} 29 30 37 public boolean addSymbol(Symbol symbol) 38 { 39 if (symbol==null) 40 throw new NullPointerException ("Symbol is null"); 41 42 ensureCapacity(elementCount+1); 43 list[elementCount] = symbol; 44 elementCount++; 45 return true; 46 } 47 48 53 public boolean addSymbol(SymbolCollection collection) 54 { 55 if (collection==null) 56 throw new NullPointerException ("Symbol collection is null"); 57 58 for (int i = 0; i<collection.getSymbolCount(); i++) 59 addSymbol(collection.getSymbol(i)); 60 61 return true; 62 } 63 64 69 public void removeSymbol(int index) 70 { 71 if (index>=elementCount) 72 throw new ArrayIndexOutOfBoundsException (index); 73 74 elementCount--; 75 if (index<elementCount) 76 System.arraycopy(list, index+1, list, index, elementCount-index); 77 } 78 79 84 public void removeSymbol(Symbol symbol) 85 { 86 if (symbol==null) 87 throw new NullPointerException ("Symbol is null"); 88 89 for (int i = elementCount-1; i>=0; i--) 90 if (symbol.equals(list[i])) 91 removeSymbol(i); 92 } 93 94 100 public void setSymbol(int index, Symbol symbol) 101 { 102 if (index>=elementCount) 103 throw new ArrayIndexOutOfBoundsException (index); 104 105 if (symbol==null) 106 throw new NullPointerException ("Symbol is null"); 107 108 list[index] = symbol; 109 } 110 111 118 public Symbol getSymbol(int index) 119 { 120 if ((index<0) && (index>=elementCount)) 121 throw new IndexOutOfBoundsException (); 122 123 return list[index]; 124 } 125 126 133 public Symbol getSymbol(String name) 134 { 135 int index = indexOf(name); 136 137 if (index>=0) 138 return list[index]; 139 140 return null; 141 } 142 143 148 public int getSymbolCount() 149 { 150 return elementCount; 151 } 152 153 158 public boolean isEmpty() 159 { 160 return elementCount==0; 161 } 162 163 170 public int indexOf(Symbol symbol) 171 { 172 if (symbol==null) 173 throw new NullPointerException ("Symbol is null"); 174 175 for (int i = 0; i<elementCount; i++) 176 if (list[i].equals(symbol)) 177 return i; 178 179 return -1; 180 } 181 182 189 public int indexOf(String name) 190 { 191 if (name==null) 192 throw new NullPointerException ("Name is null"); 193 194 for (int i = 0; i<elementCount; i++) 195 if (list[i].getName().equals(name)) 196 return i; 197 198 return -1; 199 } 200 201 208 public boolean contains(Symbol symbol) 209 { 210 if (symbol==null) 211 throw new NullPointerException ("Symbol is null"); 212 213 for (int i = 0; i<elementCount; i++) 214 if (list[i].equals(symbol)) 215 return true; 216 217 return false; 218 } 219 220 227 public boolean contains(String name) 228 { 229 if (name==null) 230 throw new NullPointerException ("Name is null"); 231 232 for (int i = 0; i<elementCount; i++) 233 if (list[i].getName().equals(name)) 234 return true; 235 236 return false; 237 } 238 239 242 public void clear() 243 { 244 elementCount = 0; 245 } 246 247 252 public SymbolSet getTerminals() 253 { 254 SymbolSet set = new SymbolSet(); 255 256 Symbol symbol; 257 258 for (int i = 0; i<elementCount; i++) 259 if ((symbol = this.list[i]) instanceof Terminal) 260 set.addSymbol(symbol); 261 262 return set; 263 } 264 265 270 public SymbolSet getNonterminals() 271 { 272 SymbolSet set = new SymbolSet(); 273 274 Symbol symbol; 275 276 for (int i = 0; i<elementCount; i++) 277 if ((symbol = this.list[i]) instanceof Nonterminal) 278 set.addSymbol(symbol); 279 280 return set; 281 } 282 283 290 public boolean equals(Object o) 291 { 292 if (o==this) 293 return true; 294 295 if (o instanceof SymbolList) 296 { 297 SymbolList list = (SymbolList)o; 298 299 if (elementCount!=list.getSymbolCount()) 300 return false; 301 302 for (int i = 0; i<list.getSymbolCount(); i++) 303 if (!getSymbol(i).equals(list.getSymbol(i))) 304 return false; 305 306 return true; 307 } 308 309 return false; 310 } 311 312 317 public String toString() 318 { 319 StringBuffer buffer = new StringBuffer (); 320 321 for (int i = 0; i<elementCount; i++) 322 { 323 buffer.append(list[i].toString()); 324 if (i<(elementCount-1)) 325 buffer.append(" "); 326 } 327 328 return buffer.toString(); 329 } 330 331 338 public Object clone() 339 { 340 SymbolList clone = new SymbolList(); 341 342 for (int i = 0; i<getSymbolCount(); i++) 343 clone.addSymbol(getSymbol(i)); 344 345 return clone; 346 } 347 348 353 private void ensureCapacity(int minCapacity) 354 { 355 if (list.length>=minCapacity) 356 return; 357 358 int newCapacity = list.length+capacityIncrement; 359 360 if (capacityIncrement<=0) 361 newCapacity = list.length*2; 362 363 Symbol[] newArray = new Symbol[Math.max(newCapacity, minCapacity)]; 364 365 System.arraycopy(list, 0, newArray, 0, list.length); 366 list = newArray; 367 } 368 } 369 | Popular Tags |