KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > examples > jet > article2 > model > TypesafeEnum


1 package org.eclipse.emf.examples.jet.article2.model;
2
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7
8 /**
9  * Class modelling a Java typesafe enumeration class.
10  *
11  * @author Remko Popma
12  * @version $Revision: 1.1 $ ($Date: 2004/05/31 21:35:35 $)
13  */

14 public class TypesafeEnum
15 {
16
17   private String JavaDoc mPackageName = "";
18
19   private String JavaDoc mClassName = "";
20
21   private String JavaDoc mAuthor = "";
22
23   private String JavaDoc mVersion = "";
24
25   private ArrayList JavaDoc mInstances = new ArrayList JavaDoc();
26
27   private ArrayList JavaDoc mAttributes = new ArrayList JavaDoc();
28
29   private Instance mDefaultInstance = null;
30
31   /**
32    * Constructs an uninitialized <code>TypesafeEnum</code>.
33    */

34   public TypesafeEnum()
35   {
36     super();
37   }
38
39   /**
40    * Convenience method that returns a comma-separated list of attribute types
41    * and names. The result of this method can be used as the parameter list for
42    * the constructor of an instance.
43    *
44    * @return a comma-separated list of attributes, formatted like
45    * <code>attrib1-type attrib1-name, attrib2-type attrib2-name (, ...)</code>
46    */

47   public String JavaDoc constructorParameterDescription()
48   {
49     return parameterDescription(attributes());
50   }
51
52   /**
53    * Convenience method that returns a comma-separated list of the key attribute
54    * types and names. The result of this method can be used as the parameter
55    * list for the lookup method.
56    *
57    * @return a comma-separated list of key attributes, formatted like
58    * <code>key-attrib1-type key-attrib1-name, key-attrib2-type key-attrib2-name (, ...)</code>
59    */

60   public String JavaDoc keyParameterDescription()
61   {
62     return parameterDescription(keyAttributes());
63   }
64
65   private String JavaDoc parameterDescription(Iterator JavaDoc attributes)
66   {
67     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
68
69     for (Iterator JavaDoc i = attributes; i.hasNext();)
70     {
71       Attribute attrib = (Attribute)i.next();
72       result.append(attrib.getType()).append(' ');
73       result.append(attrib.getUncappedName());
74
75       if (i.hasNext())
76       {
77         result.append(", ");
78       }
79     }
80     return result.toString();
81   }
82
83   /**
84    * Returns whether this <code>TypesafeEnum</code> has a default instance.
85    *
86    * @return whether this <code>TypesafeEnum</code> has a default instance
87    */

88   public boolean hasDefaultInstance()
89   {
90     return getDefaultInstance() != null;
91   }
92
93   /**
94    * Returns the default instance of this <code>TypesafeEnum</code>, or
95    * <code>null</code> if this type does not have a default instance.
96    *
97    * @return the default instance or <code>null</code>
98    */

99   public Instance getDefaultInstance()
100   {
101     return mDefaultInstance;
102   }
103
104   /**
105    * Sets the default instance of this <code>TypesafeEnum</code>.
106    *
107    * @param newDefault
108    * the new default instance
109    */

110   public void setDefaultInstance(Instance newDefault)
111   {
112     mDefaultInstance = newDefault;
113   }
114
115   /**
116    * Returns the author to use in the type comment of this type
117    *
118    * @return the author to use in the type comment of this type
119    */

120   public String JavaDoc getAuthor()
121   {
122     return mAuthor;
123   }
124
125   /**
126    * Returns the class name of this type.
127    *
128    * @return the class name of this type
129    */

130   public String JavaDoc getClassName()
131   {
132     return mClassName;
133   }
134
135   /**
136    * Returns the package name of this type.
137    *
138    * @return the package name of this type
139    */

140   public String JavaDoc getPackageName()
141   {
142     return mPackageName;
143   }
144
145   /**
146    * Returns the version string to use in the type comment of this type.
147    *
148    * @return the version string to use in the type comment of this type
149    */

150   public String JavaDoc getVersion()
151   {
152     return mVersion;
153   }
154
155   /**
156    * Sets the author to use in the type comment of this type.
157    *
158    * @param author
159    * the author to use in the type comment of this type
160    */

161   public void setAuthor(String JavaDoc author)
162   {
163     mAuthor = author;
164   }
165
166   /**
167    * Sets the class name of this type.
168    *
169    * @param className
170    * the class name of this type
171    */

172   public void setClassName(String JavaDoc className)
173   {
174     mClassName = className;
175   }
176
177   /**
178    * Sets the package name of this type.
179    *
180    * @param packageName
181    * the package name of this type
182    */

183   public void setPackageName(String JavaDoc packageName)
184   {
185     mPackageName = packageName;
186   }
187
188   /**
189    * Sets the version string to use in the type comment of this type.
190    *
191    * @param version
192    * the version string to use in the type comment of this type
193    */

194   public void setVersion(String JavaDoc version)
195   {
196     mVersion = version;
197   }
198
199   /**
200    * Returns an <code>Iterator</code> over the instances of this type.
201    *
202    * @return an <code>Iterator</code> over the instances of this type
203    */

204   public Iterator JavaDoc instances()
205   {
206     return mInstances.iterator();
207   }
208
209   /**
210    * Returns an <code>Iterator</code> over the attributes of this type.
211    *
212    * @return an <code>Iterator</code> over the attributes of this type
213    */

214   public Iterator JavaDoc attributes()
215   {
216     return mAttributes.iterator();
217   }
218
219   /**
220    * Returns an <code>Iterator</code> over the attributes of this type that
221    * have been marked as key attributes (uniquely identifying instances).
222    *
223    * @return an <code>Iterator</code> over the key attributes
224    */

225   public Iterator JavaDoc keyAttributes()
226   {
227     ArrayList JavaDoc result = new ArrayList JavaDoc();
228     for (Iterator JavaDoc i = attributes(); i.hasNext();)
229     {
230       Attribute attrib = (Attribute)i.next();
231       if (attrib.isKey())
232       {
233         result.add(attrib);
234       }
235     }
236     return result.iterator();
237   }
238
239   /**
240    * Adds an instance to this <code>TypesafeEnum</code>, and sets the type of
241    * the specified <code>Instance</code> to this object.
242    *
243    * @param instance
244    * the instance to add
245    */

246   public void addInstance(Instance instance)
247   {
248     mInstances.add(instance);
249     instance.setType(this);
250   }
251
252   /**
253    * Removes the specified <code>Instance</code> from this
254    * <code>TypesafeEnum</code>, and sets the type of the specified instance
255    * to <code>null</code>.
256    *
257    * @param instance
258    * the instance to remove
259    * @return whether the instance was in this type
260    */

261   public boolean removeInstance(Instance instance)
262   {
263     boolean result = mInstances.remove(instance);
264     if (result)
265     {
266       instance.setType(null);
267     }
268     return result;
269   }
270
271   /**
272    * Returns the number of instances in this type.
273    *
274    * @return the number of instances in this type
275    */

276   public int instanceCount()
277   {
278     return mInstances.size();
279   }
280
281   /**
282    * Adds the specified <code>Attribute</code> to this
283    * <code>TypesafeEnum</code>.
284    *
285    * @param attribute
286    * the attribute to add
287    */

288   public void addAttribute(Attribute attribute)
289   {
290     mAttributes.add(attribute);
291   }
292
293   /**
294    * Removes the specified <code>Attribute</code> from this
295    * <code>TypesafeEnum</code>.
296    *
297    * @param attribute
298    * the attribute to remove
299    * @return whether the specified attribute existed in this type
300    */

301   public boolean removeAttribute(Attribute attribute)
302   {
303     return mAttributes.remove(attribute);
304   }
305
306   /**
307    * Returns the number of attributes in this type.
308    *
309    * @return the number of attributes in this type
310    */

311   public int attributeCount()
312   {
313     return mAttributes.size();
314   }
315
316   /**
317    * Returns whether this is the last attribute in the list.
318    *
319    * @param attribute
320    * @return
321    */

322   public boolean isLast(Attribute attribute)
323   {
324     return mAttributes.indexOf(attribute) == attributeCount() - 1;
325   }
326
327   /**
328    * Returns whether this is the first attribute in the list.
329    *
330    * @param attribute
331    * @return
332    */

333   public boolean isFirst(Attribute attribute)
334   {
335     return mAttributes.indexOf(attribute) == 0;
336   }
337
338   /**
339    * Returns whether this is the last instance in the list.
340    *
341    * @param instance
342    * @return
343    */

344   public boolean isLast(Instance instance)
345   {
346     return mInstances.indexOf(instance) == instanceCount() - 1;
347   }
348
349   /**
350    * Returns whether this is the first instance in the list.
351    *
352    * @param instance
353    * @return
354    */

355   public boolean isFirst(Instance instance)
356   {
357     return mInstances.indexOf(instance) == 0;
358   }
359
360   /**
361    * @param property
362    * @return
363    */

364   public boolean hasAttribute(String JavaDoc property)
365   {
366     for (Iterator JavaDoc i = attributes(); i.hasNext();)
367     {
368       if (((Attribute)i.next()).getName().equals(property))
369       {
370         return true;
371       }
372     }
373     return false;
374   }
375
376   /**
377    * Returns the attributes in this typesafe enum as an array.
378    *
379    * @return the attributes in this typesafe enum as an array
380    */

381   public Attribute[] attributeArray()
382   {
383     return (Attribute[])mAttributes.toArray(new Attribute [mAttributes.size()]);
384   }
385 }
Popular Tags