KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.eclipse.emf.examples.jet.article2.model;
2
3
4 import java.util.Iterator JavaDoc;
5 import java.util.Properties JavaDoc;
6
7
8 /**
9  * Class modelling an Singleton instance of an enumeration. An instance has a
10  * name, a set of attribute values, and knows whether it is the default instance
11  * of the enumeration.
12  *
13  * @author Remko Popma
14  * @version $Revision: 1.1 $ ($Date: 2004/05/31 21:35:35 $)
15  */

16 public class Instance
17 {
18
19   private TypesafeEnum mType = null;
20
21   private String JavaDoc mName = "";
22
23   private Properties JavaDoc mValues = new Properties JavaDoc();
24
25   /**
26    * Creates an uninitialized <code>Instance</code>.
27    */

28   public Instance()
29   {
30   }
31
32   /**
33    * Creates an <code>Instance</code> with the specified name.
34    *
35    * @param name
36    * the name of this instance
37    */

38   public Instance(String JavaDoc name)
39   {
40     mName = name;
41   }
42
43   /**
44    * Creates an <code>Instance</code> with the specified name and attribute
45    * values.
46    *
47    * @param name
48    * the name of this instance
49    * @param values
50    * a <code>Properties</code> object mapping attribute names to the
51    * attribute values for this instance
52    */

53   public Instance(String JavaDoc name, Properties JavaDoc values)
54   {
55     super();
56     mName = name;
57     mValues = values;
58   }
59
60   /**
61    * Convenience method that returns a description of the key attribute names
62    * and values.
63    *
64    * @return a comma-separated list of key attribute names and values, formatted
65    * like
66    * <code>attrib1-name = attrib1-value, attrib2-name = attrib2-value (, ...)</code>
67    * @throws IllegalStateException
68    * if this instance has not been added to a type
69    */

70   public String JavaDoc keyDescription()
71   {
72     assertTypeNotNull();
73
74     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
75     for (Iterator JavaDoc i = mType.keyAttributes(); i.hasNext();)
76     {
77       Attribute attribute = (Attribute)i.next();
78       result.append(attribute.getCappedName());
79       result.append(" = ");
80       result.append(getAttributeValue(attribute));
81       if (i.hasNext())
82       {
83         result.append(", ");
84       }
85     }
86     return result.toString();
87   }
88
89   /**
90    * Convenience method that returns the attribute values of this instance, in
91    * the order expected by the constructor of this instance.
92    *
93    * @return a comma-separated list of all attribute values of this instance,
94    * formatted like <code>attrib1-value, attrib2-value (, ...)</code>
95    * @throws IllegalStateException
96    * if this instance has not been added to a type
97    */

98   public String JavaDoc constructorValues()
99   {
100     assertTypeNotNull(); // check we've been added to a TypesafeEnum
101

102     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
103     for (Iterator JavaDoc i = getType().attributes(); i.hasNext();)
104     {
105       Attribute attribute = (Attribute)i.next();
106       result.append(getAttributeValue(attribute));
107       if (i.hasNext())
108       {
109         result.append(", ");
110       }
111     }
112     return result.toString();
113   }
114
115   /**
116    * Returns the name of this instance converted to a java class name. For
117    * example, a constant name like <code>THIS_IS_A_CONSTANT</code> is
118    * converted to <code>ThisIsAConstant</code>.
119    *
120    * @return the name of this instance converted to a java class name
121    */

122   public String JavaDoc getCappedName()
123   {
124     return NameUtil.constantToJavaClassName(getName());
125   }
126
127   /**
128    * Returns the name of this instance.
129    *
130    * @return the name of this instance
131    */

132   public String JavaDoc getName()
133   {
134     return mName;
135   }
136
137   /**
138    * Returns the <code>Properties</code> object mapping attribute names to the
139    * attribute values for this instance
140    *
141    * @return the <code>Properties</code> object mapping attribute names to the
142    * attribute values for this instance
143    */

144   public Properties JavaDoc getValues()
145   {
146     return mValues;
147   }
148
149   /**
150    * Returns the value of the specified attribute for this instance.
151    *
152    * @param attribute
153    * the attribute whose value to return
154    * @return the value of the specified attribute for this instance
155    */

156   public String JavaDoc getAttributeValue(Attribute attribute)
157   {
158     return getAttributeValue(attribute.getName());
159   }
160
161   /**
162    * Returns the value of the specified attribute for this instance.
163    *
164    * @param attributeName
165    * the name of the attribute whose value to return
166    * @return the value of the specified attribute for this instance
167    */

168   public String JavaDoc getAttributeValue(String JavaDoc attributeName)
169   {
170     return mValues.getProperty(attributeName);
171   }
172
173   /**
174    * Sets the name of this instance.
175    *
176    * @param name
177    * the name of this instance
178    */

179   public void setName(String JavaDoc name)
180   {
181     mName = name;
182   }
183
184   /**
185    * Sets the <code>Properties</code> object mapping attribute names to the
186    * attribute values for this instance.
187    *
188    * @param values
189    * the <code>Properties</code> object mapping attribute names to
190    * the attribute values for this instance
191    */

192   public void setValues(Properties JavaDoc values)
193   {
194     mValues = values;
195   }
196
197   /**
198    * Returns the <code>TypesafeEnum</code> parent for this instance.
199    *
200    * @return the <code>TypesafeEnum</code> parent for this instance
201    */

202   public TypesafeEnum getType()
203   {
204     return mType;
205   }
206
207   /**
208    * Sets the <code>TypesafeEnum</code> parent for this instance.
209    *
210    * @param type
211    * the <code>TypesafeEnum</code> parent for this instance
212    */

213   /* package */void setType(TypesafeEnum type)
214   {
215     mType = type;
216   }
217
218   /**
219    * Returns whether this instance is the default instance of the parent
220    * <code>TypesafeEnum</code>.
221    *
222    * @return whether this instance is the default instance of the parent
223    * <code>TypesafeEnum</code>
224    * @throws IllegalStateException
225    * if this instance has not been added to a type
226    */

227   public boolean isDefault()
228   {
229     assertTypeNotNull();
230     return getType().getDefaultInstance() == this;
231   }
232
233   /**
234    * Sets whether this instance is the default instance of the parent
235    * <code>TypesafeEnum</code>.
236    *
237    * @param isDefault
238    * whether this instance is the default instance of the parent
239    * <code>TypesafeEnum</code>
240    * @throws IllegalStateException
241    * if this instance has not been added to a type
242    */

243   public void setDefault()
244   {
245     assertTypeNotNull();
246     getType().setDefaultInstance(this);
247   }
248
249   private void assertTypeNotNull()
250   {
251     if (getType() == null)
252     {
253       throw new IllegalStateException JavaDoc("Type is null. This instance has not been added to a type yet.");
254     }
255   }
256 }
Popular Tags