1 8 9 package net.sourceforge.chaperon.model.symbol; 10 11 import java.io.Serializable ; 12 13 20 public class SymbolSet implements SymbolCollection, Serializable , Cloneable 21 { 22 private int capacityIncrement = 100; 23 private int elementCount = 0; 24 private Symbol[] list = new Symbol[10]; 25 26 33 public boolean addSymbol(Symbol symbol) 34 { 35 int index = indexOf(symbol); 36 37 if (index==-1) 38 { 39 ensureCapacity(elementCount+1); 40 list[elementCount] = symbol; 41 elementCount++; 42 return true; 43 } 44 45 return false; 46 } 47 48 53 public boolean addSymbol(SymbolCollection collection) 54 { 55 if (collection==null) 56 throw new NullPointerException ("Symbol collection is null"); 57 58 boolean changed = false; 59 for (int i = 0; i<collection.getSymbolCount(); i++) 60 changed |= addSymbol(collection.getSymbol(i)); 61 62 return changed; 63 } 64 65 70 public void removeSymbol(int index) 71 { 72 if (index>=elementCount) 73 throw new ArrayIndexOutOfBoundsException (index); 74 75 elementCount--; 76 if (index<elementCount) 77 System.arraycopy(list, index+1, list, index, elementCount-index); 78 } 79 80 85 public void removeSymbol(Symbol symbol) 86 { 87 if (symbol==null) 88 throw new NullPointerException ("Symbol is null"); 89 90 for (int i = elementCount-1; i>=0; i--) 91 if (symbol.equals(list[i])) 92 removeSymbol(i); 93 } 94 95 101 public void setSymbol(int index, Symbol symbol) 102 { 103 if (index>=elementCount) 104 throw new ArrayIndexOutOfBoundsException (index); 105 106 if (symbol==null) 107 throw new NullPointerException ("Symbol is null"); 108 109 list[index] = symbol; 110 111 if (contains(symbol) && (!getSymbol(index).equals(symbol))) 112 throw new IllegalArgumentException ("Set already contains the symbol"); 113 114 list[index] = symbol; 115 } 116 117 124 public Symbol getSymbol(int index) 125 { 126 if ((index<0) && (index>=elementCount)) 127 throw new IndexOutOfBoundsException (); 128 129 return list[index]; 130 } 131 132 139 public Symbol getSymbol(String name) 140 { 141 int index = indexOf(name); 142 143 if (index>=0) 144 return list[index]; 145 146 return null; 147 } 148 149 154 public int getSymbolCount() 155 { 156 return elementCount; 157 } 158 159 164 public boolean isEmpty() 165 { 166 return elementCount==0; 167 } 168 169 176 public int indexOf(Symbol symbol) 177 { 178 if (symbol==null) 179 throw new NullPointerException ("Symbol is null"); 180 181 for (int i = 0; i<elementCount; i++) 182 if (list[i].equals(symbol)) 183 return i; 184 185 return -1; 186 } 187 188 195 public int indexOf(String name) 196 { 197 if (name==null) 198 throw new NullPointerException ("Name is null"); 199 200 for (int i = 0; i<elementCount; i++) 201 if (list[i].getName().equals(name)) 202 return i; 203 204 return -1; 205 } 206 207 214 public boolean contains(Symbol symbol) 215 { 216 if (symbol==null) 217 throw new NullPointerException ("Symbol is null"); 218 219 for (int i = 0; i<elementCount; i++) 220 if (list[i].equals(symbol)) 221 return true; 222 223 return false; 224 } 225 226 233 public boolean contains(String name) 234 { 235 if (name==null) 236 throw new NullPointerException ("Name is null"); 237 238 for (int i = 0; i<elementCount; i++) 239 if (list[i].getName().equals(name)) 240 return true; 241 242 return false; 243 } 244 245 public boolean contains(SymbolCollection collection) 246 { 247 if (collection==null) 248 throw new NullPointerException ("Set is null"); 249 250 for (int i = 0; i<collection.getSymbolCount(); i++) 251 if (!contains(collection.getSymbol(i))) 252 return false; 253 254 return true; 255 } 256 257 260 public void clear() 261 { 262 elementCount = 0; 263 } 264 265 270 public SymbolSet getTerminals() 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 Terminal) 278 set.addSymbol(symbol); 279 280 return set; 281 } 282 283 288 public SymbolSet getNonterminals() 289 { 290 SymbolSet set = new SymbolSet(); 291 292 Symbol symbol; 293 294 for (int i = 0; i<elementCount; i++) 295 if ((symbol = this.list[i]) instanceof Nonterminal) 296 set.addSymbol(symbol); 297 298 return set; 299 } 300 301 308 public boolean equals(Object o) 309 { 310 if (o==this) 311 return true; 312 313 if (o instanceof SymbolSet) 314 { 315 SymbolSet set = (SymbolSet)o; 316 317 if (getSymbolCount()!=set.getSymbolCount()) 318 return false; 319 320 for (int i = 0; i<set.getSymbolCount(); i++) 321 if (!contains(set.getSymbol(i))) 322 return false; 323 324 return true; 325 } 326 327 return false; 328 } 329 330 335 public String toString() 336 { 337 StringBuffer buffer = new StringBuffer (); 338 339 for (int i = 0; i<elementCount; i++) 340 { 341 buffer.append(list[i].toString()); 342 if (i<(elementCount-1)) 343 buffer.append(" "); 344 } 345 346 return buffer.toString(); 347 } 348 349 356 public Object clone() 357 { 358 SymbolList clone = new SymbolList(); 359 360 for (int i = 0; i<getSymbolCount(); i++) 361 clone.addSymbol(getSymbol(i)); 362 363 return clone; 364 } 365 366 371 private void ensureCapacity(int minCapacity) 372 { 373 if (list.length>=minCapacity) 374 return; 375 376 int newCapacity = list.length+capacityIncrement; 377 378 if (capacityIncrement<=0) 379 newCapacity = list.length*2; 380 381 Symbol[] newArray = new Symbol[Math.max(newCapacity, minCapacity)]; 382 383 System.arraycopy(list, 0, newArray, 0, list.length); 384 list = newArray; 385 } 386 } 387 | Popular Tags |