View Javadoc

1   /*
2   
3       dsh-codegen  Source code generation suite.
4       Copyright (c) 2004-2013 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   */
33  public final class Attribute
34  {
35      /** The lowercase name for this attribute. */
36      private final String lower;
37  
38      /** The mixed-case name for this attribute. */
39      private final String mixed;
40  
41      /** The uppercase name for this attribute. */
42      private final String upper;
43  
44      /** The description for this attribute. */
45      private final String description;
46  
47      /** The role for this attribute. */
48      private final Role role;
49  
50      /** The cardinality for this attribute. */
51      private final Cardinality cardinality;
52  
53      /** The collection description for this attribute. */
54      private final CollectionDescription collectionDescription;
55  
56      /** True if this is a "bound" attribute. */
57      private final boolean bound;
58  
59  
60      /**
61       * Create a new attribute with the specified name, role name,
62       * and cardinality.  The cardinality must be one of <b>Cardinality.ZeroToOne</b>
63       * or <b>Cardinality.StrictlyOne</b>.
64       *
65       * @param name attribute name
66       * @param roleName role name
67       * @param cardinality cardinality, must not be null and must be one
68       *    of <b>Cardinality.ZeroToOne</b> or <b>Cardinality.StrictlyOne</b>
69       */
70      public Attribute(final String name,
71                       final String roleName,
72                       final Cardinality cardinality)
73      {
74          if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
75          {
76              throw new IllegalArgumentException("cardinality must be one of {ZeroToOne, StrictlyOne}");
77          }
78  
79          this.lower = CodegenUtils.makeLowercase(name);
80          this.mixed = CodegenUtils.makeMixedCase(name);
81          this.upper = CodegenUtils.makeUppercase(name);
82          this.description = CodegenUtils.makeDescription(name);
83  
84          this.role = new Role(roleName);
85  
86          this.cardinality = cardinality;
87          this.collectionDescription = null;
88          this.bound = false;
89      }
90  
91      /**
92       * Create a new attribute with the specified name, role name,
93       * cardinality, and bound flag.  The cardinality must be one of <b>Cardinality.ZeroToOne</b>
94       * or <b>Cardinality.StrictlyOne</b>.
95       *
96       * @param name attribute name
97       * @param roleName role name
98       * @param cardinality cardinality, must not be null and must be one
99       *    of <b>Cardinality.ZeroToOne</b> or <b>Cardinality.StrictlyOne</b>
100      * @param bound true if this is a "bound" attribute
101      */
102     public Attribute(final String name,
103                      final String roleName,
104                      final Cardinality cardinality,
105                      final boolean bound)
106     {
107         if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
108         {
109             throw new IllegalArgumentException("cardinality must be one of {ZeroToOne, StrictlyOne}");
110         }
111 
112         this.lower = CodegenUtils.makeLowercase(name);
113         this.mixed = CodegenUtils.makeMixedCase(name);
114         this.upper = CodegenUtils.makeUppercase(name);
115         this.description = CodegenUtils.makeDescription(name);
116 
117         this.role = new Role(roleName);
118 
119         this.cardinality = cardinality;
120         this.collectionDescription = null;
121         this.bound = bound;
122     }
123 
124     /**
125      * Create a new attribute with the specified name, role name, and
126      * cardinality and choose a collection description that satisfies the
127      * specified boolean parameters.
128      *
129      * @param name attribute name
130      * @param roleName role name
131      * @param cardinality cardinality, must not be null
132      * @param indexed true if the collection should be indexed
133      * @param unique true if the collection should not allow duplicate elements
134      * @param ordered true if the collection should iterate over elements in <i>insertion-order</i>
135      * @param sorted true if the collection should iterate over elements in ascending element order,
136      *    sorted according to the <i>natural ordering</i> of its elements (see Comparable), or by a Comparator
137      *    provided at creation time
138      */
139     public Attribute(final String name,
140                      final String roleName,
141                      final Cardinality cardinality,
142                      final boolean indexed,
143                      final boolean unique,
144                      final boolean ordered,
145                      final boolean sorted)
146     {
147         this.lower = CodegenUtils.makeLowercase(name);
148         this.mixed = CodegenUtils.makeMixedCase(name);
149         this.upper = CodegenUtils.makeUppercase(name);
150         this.description = CodegenUtils.makeDescription(name);
151 
152         this.role = new Role(roleName);
153 
154         this.cardinality = cardinality;
155         if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
156         {
157             this.collectionDescription = CollectionDescription.choose(indexed, unique, ordered, sorted);
158         }
159         else
160         {
161             this.collectionDescription = null;
162         }
163         this.bound = false;
164     }
165 
166     /**
167      * Create a new attribute with the specified name, role name,
168      * cardinality, and bound flag and choose a collection description that satisfies the
169      * specified boolean parameters.
170      *
171      * @param name attribute name
172      * @param roleName role name
173      * @param cardinality cardinality, must not be null
174      * @param bound true if this is a "bound" attribute
175      * @param indexed true if the collection should be indexed
176      * @param unique true if the collection should not allow duplicate elements
177      * @param ordered true if the collection should iterate over elements in <i>insertion-order</i>
178      * @param sorted true if the collection should iterate over elements in ascending element order,
179      *    sorted according to the <i>natural ordering</i> of its elements (see Comparable), or by a Comparator
180      *    provided at creation time
181      */
182     public Attribute(final String name,
183                      final String roleName,
184                      final Cardinality cardinality,
185                      final boolean bound,
186                      final boolean indexed,
187                      final boolean unique,
188                      final boolean ordered,
189                      final boolean sorted)
190     {
191         this.lower = CodegenUtils.makeLowercase(name);
192         this.mixed = CodegenUtils.makeMixedCase(name);
193         this.upper = CodegenUtils.makeUppercase(name);
194         this.description = CodegenUtils.makeDescription(name);
195 
196         this.role = new Role(roleName);
197 
198         this.cardinality = cardinality;
199         if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
200         {
201             this.collectionDescription = CollectionDescription.choose(indexed, unique, ordered, sorted);
202         }
203         else
204         {
205             this.collectionDescription = null;
206         }
207         this.bound = bound;
208     }
209 
210     /**
211      * Create a new attribute from the specified parameters.
212      *
213      * @param lower lowercase name for this attribute
214      * @param mixed mixed-case name for this attribute
215      * @param upper uppercase name for this attribute
216      * @param description description for this attribute
217      * @param role role for this attribute, must not be null
218      * @param cardinality cardinality for this attribute, must not be null
219      * @param collectionDescription collection description for this attribute
220      */
221     public Attribute(final String lower,
222                      final String mixed,
223                      final String upper,
224                      final String description,
225                      final Role role,
226                      final Cardinality cardinality,
227                      final CollectionDescription collectionDescription)
228     {
229         if (role == null)
230         {
231             throw new IllegalArgumentException("role must not be null");
232         }
233         if (cardinality == null)
234         {
235             throw new IllegalArgumentException("cardinality must not be null");
236         }
237 
238         this.lower = lower;
239         this.mixed = mixed;
240         this.upper = upper;
241         this.description = description;
242         this.role = role;
243         this.cardinality = cardinality;
244         this.collectionDescription = collectionDescription;
245         this.bound = false;
246     }
247 
248     /**
249      * Create a new attribute from the specified parameters.
250      *
251      * @param lower lowercase name for this attribute
252      * @param mixed mixed-case name for this attribute
253      * @param upper uppercase name for this attribute
254      * @param description description for this attribute
255      * @param role role for this attribute, must not be null
256      * @param cardinality cardinality for this attribute, must not be null
257      * @param collectionDescription collection description for this attribute
258      * @param bound true if this is a "bound" attribute
259      */
260     public Attribute(final String lower,
261                      final String mixed,
262                      final String upper,
263                      final String description,
264                      final Role role,
265                      final Cardinality cardinality,
266                      final CollectionDescription collectionDescription,
267                      final boolean bound)
268     {
269         if (role == null)
270         {
271             throw new IllegalArgumentException("role must not be null");
272         }
273         if (cardinality == null)
274         {
275             throw new IllegalArgumentException("cardinality must not be null");
276         }
277 
278         this.lower = lower;
279         this.mixed = mixed;
280         this.upper = upper;
281         this.description = description;
282         this.role = role;
283         this.cardinality = cardinality;
284         this.collectionDescription = collectionDescription;
285         this.bound = bound;
286     }
287 
288 
289     /**
290      * Return the lowercase name for this attribute.
291      *
292      * @return the lowercase name for this attribute
293      */
294     public String getLower()
295     {
296         return lower;
297     }
298 
299     /**
300      * Return the mixed-case name for this attribute.
301      *
302      * @return the mixed-case name for this attribute
303      */
304     public String getMixed()
305     {
306         return mixed;
307     }
308 
309     /**
310      * Return the uppercase name for this attribute.
311      *
312      * @return the uppercase name for this attribute
313      */
314     public String getUpper()
315     {
316         return upper;
317     }
318 
319     /**
320      * Return the description for this attribute.
321      *
322      * @return the description for this attribute
323      */
324     public String getDescription()
325     {
326         return description;
327     }
328 
329     /**
330      * Return the role for this attribute.  The role will not be null.
331      *
332      * @return the role for this attribute
333      */
334     public Role getRole()
335     {
336         return role;
337     }
338 
339     /**
340      * Return the cardinality for this attribute.  The cardinality will not be null.
341      *
342      * @return the cardinality for this attribute
343      */
344     public Cardinality getCardinality()
345     {
346         return cardinality;
347     }
348 
349     /**
350      * Return the collection description for this attribute.
351      * The collection description may be null.
352      *
353      * @return the collection description for this attribute
354      */
355     public CollectionDescription getCollectionDescription()
356     {
357         return collectionDescription;
358     }
359 
360     /**
361      * Return true if this is a "bound" attribute.
362      *
363      * @return true if this is a "bound" attribute
364      */
365     public boolean isBound()
366     {
367         return bound;
368     }
369 }