1 /*
2
3 dsh-codegen Source code generation suite.
4 Copyright (c) 2004-2011 held jointly by the individual authors.
5
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 3 of the License, or (at
9 your option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19
20 > http://www.fsf.org/licensing/licenses/lgpl.html
21 > http://www.opensource.org/licenses/lgpl-license.php
22
23 */
24 package org.dishevelled.codegen;
25
26 /**
27 * An attribute on a class or interface description. An attribute has a role,
28 * a cardinality, and if the cardinality is <b>Cardinality.ZeroToMany</b> or
29 * <b>Cardinality.OneToMany</b>, a collection description.
30 *
31 * @author Michael Heuer
32 * @version $Revision: 972 $ $Date: 2011-01-04 21:34:39 -0600 (Tue, 04 Jan 2011) $
33 */
34 public final class Attribute
35 {
36 /** The lowercase name for this attribute. */
37 private final String lower;
38
39 /** The mixed-case name for this attribute. */
40 private final String mixed;
41
42 /** The uppercase name for this attribute. */
43 private final String upper;
44
45 /** The description for this attribute. */
46 private final String description;
47
48 /** The role for this attribute. */
49 private final Role role;
50
51 /** The cardinality for this attribute. */
52 private final Cardinality cardinality;
53
54 /** The collection description for this attribute. */
55 private final CollectionDescription collectionDescription;
56
57 /** True if this is a "bound" attribute. */
58 private final boolean bound;
59
60
61 /**
62 * Create a new attribute with the specified name, role name,
63 * and cardinality. The cardinality must be one of <b>Cardinality.ZeroToOne</b>
64 * or <b>Cardinality.StrictlyOne</b>.
65 *
66 * @param name attribute name
67 * @param roleName role name
68 * @param cardinality cardinality, must not be null and must be one
69 * of <b>Cardinality.ZeroToOne</b> or <b>Cardinality.StrictlyOne</b>
70 */
71 public Attribute(final String name,
72 final String roleName,
73 final Cardinality cardinality)
74 {
75 if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
76 {
77 throw new IllegalArgumentException("cardinality must be one of {ZeroToOne, StrictlyOne}");
78 }
79
80 this.lower = CodegenUtils.makeLowercase(name);
81 this.mixed = CodegenUtils.makeMixedCase(name);
82 this.upper = CodegenUtils.makeUppercase(name);
83 this.description = CodegenUtils.makeDescription(name);
84
85 this.role = new Role(roleName);
86
87 this.cardinality = cardinality;
88 this.collectionDescription = null;
89 this.bound = false;
90 }
91
92 /**
93 * Create a new attribute with the specified name, role name,
94 * cardinality, and bound flag. The cardinality must be one of <b>Cardinality.ZeroToOne</b>
95 * or <b>Cardinality.StrictlyOne</b>.
96 *
97 * @param name attribute name
98 * @param roleName role name
99 * @param cardinality cardinality, must not be null and must be one
100 * of <b>Cardinality.ZeroToOne</b> or <b>Cardinality.StrictlyOne</b>
101 * @param bound true if this is a "bound" attribute
102 */
103 public Attribute(final String name,
104 final String roleName,
105 final Cardinality cardinality,
106 final boolean bound)
107 {
108 if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
109 {
110 throw new IllegalArgumentException("cardinality must be one of {ZeroToOne, StrictlyOne}");
111 }
112
113 this.lower = CodegenUtils.makeLowercase(name);
114 this.mixed = CodegenUtils.makeMixedCase(name);
115 this.upper = CodegenUtils.makeUppercase(name);
116 this.description = CodegenUtils.makeDescription(name);
117
118 this.role = new Role(roleName);
119
120 this.cardinality = cardinality;
121 this.collectionDescription = null;
122 this.bound = bound;
123 }
124
125 /**
126 * Create a new attribute with the specified name, role name, and
127 * cardinality and choose a collection description that satisfies the
128 * specified boolean parameters.
129 *
130 * @param name attribute name
131 * @param roleName role name
132 * @param cardinality cardinality, must not be null
133 * @param indexed true if the collection should be indexed
134 * @param unique true if the collection should not allow duplicate elements
135 * @param ordered true if the collection should iterate over elements in <i>insertion-order</i>
136 * @param sorted true if the collection should iterate over elements in ascending element order,
137 * sorted according to the <i>natural ordering</i> of its elements (see Comparable), or by a Comparator
138 * provided at creation time
139 */
140 public Attribute(final String name,
141 final String roleName,
142 final Cardinality cardinality,
143 final boolean indexed,
144 final boolean unique,
145 final boolean ordered,
146 final boolean sorted)
147 {
148 this.lower = CodegenUtils.makeLowercase(name);
149 this.mixed = CodegenUtils.makeMixedCase(name);
150 this.upper = CodegenUtils.makeUppercase(name);
151 this.description = CodegenUtils.makeDescription(name);
152
153 this.role = new Role(roleName);
154
155 this.cardinality = cardinality;
156 if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
157 {
158 this.collectionDescription = CollectionDescription.choose(indexed, unique, ordered, sorted);
159 }
160 else
161 {
162 this.collectionDescription = null;
163 }
164 this.bound = false;
165 }
166
167 /**
168 * Create a new attribute with the specified name, role name,
169 * cardinality, and bound flag and choose a collection description that satisfies the
170 * specified boolean parameters.
171 *
172 * @param name attribute name
173 * @param roleName role name
174 * @param cardinality cardinality, must not be null
175 * @param bound true if this is a "bound" attribute
176 * @param indexed true if the collection should be indexed
177 * @param unique true if the collection should not allow duplicate elements
178 * @param ordered true if the collection should iterate over elements in <i>insertion-order</i>
179 * @param sorted true if the collection should iterate over elements in ascending element order,
180 * sorted according to the <i>natural ordering</i> of its elements (see Comparable), or by a Comparator
181 * provided at creation time
182 */
183 public Attribute(final String name,
184 final String roleName,
185 final Cardinality cardinality,
186 final boolean bound,
187 final boolean indexed,
188 final boolean unique,
189 final boolean ordered,
190 final boolean sorted)
191 {
192 this.lower = CodegenUtils.makeLowercase(name);
193 this.mixed = CodegenUtils.makeMixedCase(name);
194 this.upper = CodegenUtils.makeUppercase(name);
195 this.description = CodegenUtils.makeDescription(name);
196
197 this.role = new Role(roleName);
198
199 this.cardinality = cardinality;
200 if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
201 {
202 this.collectionDescription = CollectionDescription.choose(indexed, unique, ordered, sorted);
203 }
204 else
205 {
206 this.collectionDescription = null;
207 }
208 this.bound = bound;
209 }
210
211 /**
212 * Create a new attribute from the specified parameters.
213 *
214 * @param lower lowercase name for this attribute
215 * @param mixed mixed-case name for this attribute
216 * @param upper uppercase name for this attribute
217 * @param description description for this attribute
218 * @param role role for this attribute, must not be null
219 * @param cardinality cardinality for this attribute, must not be null
220 * @param collectionDescription collection description for this attribute
221 */
222 public Attribute(final String lower,
223 final String mixed,
224 final String upper,
225 final String description,
226 final Role role,
227 final Cardinality cardinality,
228 final CollectionDescription collectionDescription)
229 {
230 if (role == null)
231 {
232 throw new IllegalArgumentException("role must not be null");
233 }
234 if (cardinality == null)
235 {
236 throw new IllegalArgumentException("cardinality must not be null");
237 }
238
239 this.lower = lower;
240 this.mixed = mixed;
241 this.upper = upper;
242 this.description = description;
243 this.role = role;
244 this.cardinality = cardinality;
245 this.collectionDescription = collectionDescription;
246 this.bound = false;
247 }
248
249 /**
250 * Create a new attribute from the specified parameters.
251 *
252 * @param lower lowercase name for this attribute
253 * @param mixed mixed-case name for this attribute
254 * @param upper uppercase name for this attribute
255 * @param description description for this attribute
256 * @param role role for this attribute, must not be null
257 * @param cardinality cardinality for this attribute, must not be null
258 * @param collectionDescription collection description for this attribute
259 * @param bound true if this is a "bound" attribute
260 */
261 public Attribute(final String lower,
262 final String mixed,
263 final String upper,
264 final String description,
265 final Role role,
266 final Cardinality cardinality,
267 final CollectionDescription collectionDescription,
268 final boolean bound)
269 {
270 if (role == null)
271 {
272 throw new IllegalArgumentException("role must not be null");
273 }
274 if (cardinality == null)
275 {
276 throw new IllegalArgumentException("cardinality must not be null");
277 }
278
279 this.lower = lower;
280 this.mixed = mixed;
281 this.upper = upper;
282 this.description = description;
283 this.role = role;
284 this.cardinality = cardinality;
285 this.collectionDescription = collectionDescription;
286 this.bound = bound;
287 }
288
289
290 /**
291 * Return the lowercase name for this attribute.
292 *
293 * @return the lowercase name for this attribute
294 */
295 public String getLower()
296 {
297 return lower;
298 }
299
300 /**
301 * Return the mixed-case name for this attribute.
302 *
303 * @return the mixed-case name for this attribute
304 */
305 public String getMixed()
306 {
307 return mixed;
308 }
309
310 /**
311 * Return the uppercase name for this attribute.
312 *
313 * @return the uppercase name for this attribute
314 */
315 public String getUpper()
316 {
317 return upper;
318 }
319
320 /**
321 * Return the description for this attribute.
322 *
323 * @return the description for this attribute
324 */
325 public String getDescription()
326 {
327 return description;
328 }
329
330 /**
331 * Return the role for this attribute. The role will not be null.
332 *
333 * @return the role for this attribute
334 */
335 public Role getRole()
336 {
337 return role;
338 }
339
340 /**
341 * Return the cardinality for this attribute. The cardinality will not be null.
342 *
343 * @return the cardinality for this attribute
344 */
345 public Cardinality getCardinality()
346 {
347 return cardinality;
348 }
349
350 /**
351 * Return the collection description for this attribute.
352 * The collection description may be null.
353 *
354 * @return the collection description for this attribute
355 */
356 public CollectionDescription getCollectionDescription()
357 {
358 return collectionDescription;
359 }
360
361 /**
362 * Return true if this is a "bound" attribute.
363 *
364 * @return true if this is a "bound" attribute
365 */
366 public boolean isBound()
367 {
368 return bound;
369 }
370 }