KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > model > symbol > SymbolSet


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.model.symbol;
10
11 import java.io.Serializable JavaDoc;
12
13 /**
14  * This class represents a set of symbols, which means that this set dosn't contains duplett of
15  * symbols.
16  *
17  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
18  * @version CVS $Id: SymbolSet.java,v 1.8 2003/12/09 19:55:53 benedikta Exp $
19  */

20 public class SymbolSet implements SymbolCollection, Serializable JavaDoc, Cloneable JavaDoc
21 {
22   private int capacityIncrement = 100;
23   private int elementCount = 0;
24   private Symbol[] list = new Symbol[10];
25
26   /*
27    * Add a symbol to this set.
28    *
29    * @param symbol Symbol, which should added.
30    *
31    * @return Index of the symbol in this set.
32    */

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   /*
49    * Add the symbols from an other list to this set
50    *
51    * @param collection Collection of symbols.
52    */

53   public boolean addSymbol(SymbolCollection collection)
54   {
55     if (collection==null)
56       throw new NullPointerException JavaDoc("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   /**
66    * Removes a symbol by an index from this set.
67    *
68    * @param index Index of the symbol.
69    */

70   public void removeSymbol(int index)
71   {
72     if (index>=elementCount)
73       throw new ArrayIndexOutOfBoundsException JavaDoc(index);
74
75     elementCount--;
76     if (index<elementCount)
77       System.arraycopy(list, index+1, list, index, elementCount-index);
78   }
79
80   /**
81    * Removes a symbol from this set.
82    *
83    * @param symbol Symbol, which should be removed.
84    */

85   public void removeSymbol(Symbol symbol)
86   {
87     if (symbol==null)
88       throw new NullPointerException JavaDoc("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   /**
96    * Replace a symbol given by an index.
97    *
98    * @param index The index, at which the symbol be inserted.
99    * @param symbol Symbol.
100    */

101   public void setSymbol(int index, Symbol symbol)
102   {
103     if (index>=elementCount)
104       throw new ArrayIndexOutOfBoundsException JavaDoc(index);
105
106     if (symbol==null)
107       throw new NullPointerException JavaDoc("Symbol is null");
108
109     list[index] = symbol;
110
111     if (contains(symbol) && (!getSymbol(index).equals(symbol)))
112       throw new IllegalArgumentException JavaDoc("Set already contains the symbol");
113
114     list[index] = symbol;
115   }
116
117   /**
118    * Return a symbol giving by an index.
119    *
120    * @param index Index of the symbol.
121    *
122    * @return Symbol.
123    */

124   public Symbol getSymbol(int index)
125   {
126     if ((index<0) && (index>=elementCount))
127       throw new IndexOutOfBoundsException JavaDoc();
128
129     return list[index];
130   }
131
132   /**
133    * Returns a symbol from this set given by the name of the symbol.
134    *
135    * @param name Name of the symbol.
136    *
137    * @return Symbol.
138    */

139   public Symbol getSymbol(String JavaDoc name)
140   {
141     int index = indexOf(name);
142
143     if (index>=0)
144       return list[index];
145
146     return null;
147   }
148
149   /**
150    * Returns the count of symbols in this set
151    *
152    * @return Count of symbols.
153    */

154   public int getSymbolCount()
155   {
156     return elementCount;
157   }
158
159   /**
160    * If this set is empty.
161    *
162    * @return True, if the set is empty.
163    */

164   public boolean isEmpty()
165   {
166     return elementCount==0;
167   }
168
169   /**
170    * Return the index of a symbol.
171    *
172    * @param symbol Symbol.
173    *
174    * @return Index of this symbol.
175    */

176   public int indexOf(Symbol symbol)
177   {
178     if (symbol==null)
179       throw new NullPointerException JavaDoc("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   /**
189    * Return the index of a symbol, given by the name of the Symbol.
190    *
191    * @param name Name of symbol.
192    *
193    * @return Index of this symbol.
194    */

195   public int indexOf(String JavaDoc name)
196   {
197     if (name==null)
198       throw new NullPointerException JavaDoc("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   /**
208    * If the list contains a symbol
209    *
210    * @param symbol Symbol
211    *
212    * @return True, if the list contains the symbol
213    */

214   public boolean contains(Symbol symbol)
215   {
216     if (symbol==null)
217       throw new NullPointerException JavaDoc("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   /**
227    * If the list contains a symbol, given by the name of the symbol.
228    *
229    * @param name Name of the symbol.
230    *
231    * @return True, if the list contains the symbol.
232    */

233   public boolean contains(String JavaDoc name)
234   {
235     if (name==null)
236       throw new NullPointerException JavaDoc("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 JavaDoc("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   /**
258    * Removes all symbols from this set.
259    */

260   public void clear()
261   {
262     elementCount = 0;
263   }
264
265   /**
266    * Return all used terminal symbols in this set.
267    *
268    * @return List of terminal symbols.
269    */

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   /**
284    * Return all used nonterminal symbols in this set.
285    *
286    * @return List of nonterminal symbols.
287    */

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   /**
302    * Compares to another set of symbols.
303    *
304    * @param o Another object.
305    *
306    * @return True, if both sets contains the same symbols.
307    */

308   public boolean equals(Object JavaDoc 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   /**
331    * Return a string representation of the set.
332    *
333    * @return String representation of the set.
334    */

335   public String JavaDoc toString()
336   {
337     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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   /**
350    * Create a clone of this set of symbols
351    *
352    * @return Clone of this set.
353    *
354    * @throws CloneNotSupportedException If a exception occurs during the cloning.
355    */

356   public Object JavaDoc 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   /**
367    * Ensure the capacity for adding values
368    *
369    * @param minCapacity
370    */

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