KickJava   Java API By Example, From Geeks To Geeks.

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


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 list of symbols.
15  *
16  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
17  * @version CVS $Id: SymbolList.java,v 1.7 2003/12/09 19:55:53 benedikta Exp $
18  */

19 public class SymbolList implements SymbolCollection, Serializable JavaDoc, Cloneable JavaDoc
20 {
21   private int capacityIncrement = 100;
22   private int elementCount = 0;
23   private Symbol[] list = new Symbol[10];
24
25   /**
26    * Creates a empty list of symbols.
27    */

28   public SymbolList() {}
29
30   /*
31    * Add a symbol to this list.
32    *
33    * @param symbol Symbol, which should added.
34    *
35    * @return Index of the symbol in this list.
36    */

37   public boolean addSymbol(Symbol symbol)
38   {
39     if (symbol==null)
40       throw new NullPointerException JavaDoc("Symbol is null");
41
42     ensureCapacity(elementCount+1);
43     list[elementCount] = symbol;
44     elementCount++;
45     return true;
46   }
47
48   /*
49    * Add the symbols from another list to this list
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     for (int i = 0; i<collection.getSymbolCount(); i++)
59       addSymbol(collection.getSymbol(i));
60
61     return true;
62   }
63
64   /**
65    * Removes a symbol by an index from this list.
66    *
67    * @param index Index of the symbol.
68    */

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

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

100   public void setSymbol(int index, Symbol symbol)
101   {
102     if (index>=elementCount)
103       throw new ArrayIndexOutOfBoundsException JavaDoc(index);
104
105     if (symbol==null)
106       throw new NullPointerException JavaDoc("Symbol is null");
107
108     list[index] = symbol;
109   }
110
111   /**
112    * Return a symbol giving by an index.
113    *
114    * @param index Index of the symbol.
115    *
116    * @return Symbol.
117    */

118   public Symbol getSymbol(int index)
119   {
120     if ((index<0) && (index>=elementCount))
121       throw new IndexOutOfBoundsException JavaDoc();
122
123     return list[index];
124   }
125
126   /**
127    * Returns a symbol from this list given by the name of the symbol.
128    *
129    * @param name Name of the symbol.
130    *
131    * @return Symbol.
132    */

133   public Symbol getSymbol(String JavaDoc name)
134   {
135     int index = indexOf(name);
136
137     if (index>=0)
138       return list[index];
139
140     return null;
141   }
142
143   /**
144    * Returns the count of symbols in the list.
145    *
146    * @return Count of symbol.
147    */

148   public int getSymbolCount()
149   {
150     return elementCount;
151   }
152
153   /**
154    * If this list is empty.
155    *
156    * @return True, if the list is empty.
157    */

158   public boolean isEmpty()
159   {
160     return elementCount==0;
161   }
162
163   /**
164    * Return the index of a symbol.
165    *
166    * @param symbol Symbol.
167    *
168    * @return Index of this symbol.
169    */

170   public int indexOf(Symbol symbol)
171   {
172     if (symbol==null)
173       throw new NullPointerException JavaDoc("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   /**
183    * Return the index of a symbol, given by the name of the Symbol.
184    *
185    * @param name Name of symbol.
186    *
187    * @return Index of this symbol.
188    */

189   public int indexOf(String JavaDoc name)
190   {
191     if (name==null)
192       throw new NullPointerException JavaDoc("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   /**
202    * If the list contains a symbol.
203    *
204    * @param symbol Symbol
205    *
206    * @return True, if the list contains the symbol
207    */

208   public boolean contains(Symbol symbol)
209   {
210     if (symbol==null)
211       throw new NullPointerException JavaDoc("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   /**
221    * If the list contains a symbol, given by the name of the symbol.
222    *
223    * @param name Name of the symbol.
224    *
225    * @return True, if the list contains the symbol.
226    */

227   public boolean contains(String JavaDoc name)
228   {
229     if (name==null)
230       throw new NullPointerException JavaDoc("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   /**
240    * Removes all symbols from this list.
241    */

242   public void clear()
243   {
244     elementCount = 0;
245   }
246
247   /**
248    * Return all used terminal symbols in this list.
249    *
250    * @return Set of terminal symbols.
251    */

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   /**
266    * Return all used non terminal symbols in this list.
267    *
268    * @return Set of nonterminal symbols.
269    */

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   /**
284    * Compares to another list of symbols.
285    *
286    * @param o Another object.
287    *
288    * @return True, if both lists contains the same symbols.
289    */

290   public boolean equals(Object JavaDoc 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   /**
313    * Return a string representation of the list.
314    *
315    * @return String representation of the list.
316    */

317   public String JavaDoc toString()
318   {
319     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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   /**
332    * Create a clone of this list of symbols
333    *
334    * @return Clone of this list.
335    *
336    * @throws CloneNotSupportedException If a exception occurs during the cloning.
337    */

338   public Object JavaDoc 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   /**
349    * Ensure the capacity for adding values
350    *
351    * @param minCapacity
352    */

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