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 association between a class or interface description and
28   * a target class or interface description.  An association has a role,
29   * a cardinality, and if the cardinality is <b>Cardinality.ZeroToMany</b> or
30   * <b>Cardinality.OneToMany</b>, a collection description.
31   *
32   * @author  Michael Heuer
33   */
34  public final class Association
35  {
36      /** The lowercase name for this association. */
37      private final String lower;
38  
39      /** The mixed-case name for this association. */
40      private final String mixed;
41  
42      /** The uppercase name for this association. */
43      private final String upper;
44  
45      /** The package name for this association. */
46      private final String packageName;
47  
48      /** The description for this association. */
49      private final String description;
50  
51      /** The role for this association. */
52      private final Role role;
53  
54      /** The cardinality for this association. */
55      private final Cardinality cardinality;
56  
57      /** The collection description for this association. */
58      private final CollectionDescription collectionDescription;
59  
60      /** True if this is a "bound" association. */
61      private final boolean bound;
62  
63  
64      /**
65       * Create a new association with the specified class description, role name,
66       * and cardinality.  The cardinality must be one of <b>Cardinality.ZeroToOne</b>
67       * or <b>Cardinality.StrictlyOne</b>.
68       *
69       * @param cd class description, must not be null
70       * @param roleName role name
71       * @param cardinality cardinality, must not be null and must be one
72       *    of <b>Cardinality.ZeroToOne</b> or <b>Cardinality.StrictlyOne</b>
73       */
74      public Association(final ClassDescription cd,
75                         final String roleName,
76                         final Cardinality cardinality)
77      {
78          if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
79          {
80              throw new IllegalArgumentException("cardinality must be one of {ZeroToOne, StrictlyOne}");
81          }
82  
83          this.lower = cd.getLower();
84          this.mixed = cd.getMixed();
85          this.upper = cd.getUpper();
86          this.description = cd.getDescription();
87          this.packageName = cd.getPackageName();
88  
89          this.role = new Role(roleName);
90  
91          this.cardinality = cardinality;
92          this.collectionDescription = null;
93          this.bound = false;
94      }
95  
96      /**
97       * Create a new association with the specified class description, role name,
98       * cardinality, and bound flag.  The cardinality must be one of <b>Cardinality.ZeroToOne</b>
99       * or <b>Cardinality.StrictlyOne</b>.
100      *
101      * @param cd class description, must not be null
102      * @param roleName role name
103      * @param cardinality cardinality, must not be null and must be one
104      *    of <b>Cardinality.ZeroToOne</b> or <b>Cardinality.StrictlyOne</b>
105      * @param bound true if this is a "bound" association
106      */
107     public Association(final ClassDescription cd,
108                        final String roleName,
109                        final Cardinality cardinality,
110                        final boolean bound)
111     {
112         if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
113         {
114             throw new IllegalArgumentException("cardinality must be one of {ZeroToOne, StrictlyOne}");
115         }
116 
117         this.lower = cd.getLower();
118         this.mixed = cd.getMixed();
119         this.upper = cd.getUpper();
120         this.description = cd.getDescription();
121         this.packageName = cd.getPackageName();
122 
123         this.role = new Role(roleName);
124 
125         this.cardinality = cardinality;
126         this.collectionDescription = null;
127         this.bound = bound;
128     }
129 
130     /**
131      * Create a new association with the specified class description, role name, and
132      * cardinality and choose a collection description that satisfies the
133      * specified boolean parameters.
134      *
135      * @param cd class description, must not be null
136      * @param roleName role name
137      * @param cardinality cardinality, must not be null
138      * @param indexed true if the collection should be indexed
139      * @param unique true if the collection should not allow duplicate elements
140      * @param ordered true if the collection should iterate over elements in <i>insertion-order</i>
141      * @param sorted true if the collection should iterate over elements in ascending element order,
142      *    sorted according to the <i>natural ordering</i> of its elements (see Comparable), or by a Comparator
143      *    provided at creation time
144      */
145     public Association(final ClassDescription cd,
146                        final String roleName,
147                        final Cardinality cardinality,
148                        final boolean indexed,
149                        final boolean unique,
150                        final boolean ordered,
151                        final boolean sorted)
152     {
153         this.lower = cd.getLower();
154         this.mixed = cd.getMixed();
155         this.upper = cd.getUpper();
156         this.description = cd.getDescription();
157         this.packageName = cd.getPackageName();
158 
159         this.role = new Role(roleName);
160 
161         this.cardinality = cardinality;
162         if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
163         {
164             this.collectionDescription = CollectionDescription.choose(indexed, unique, ordered, sorted);
165         }
166         else
167         {
168             this.collectionDescription = null;
169         }
170         this.bound = false;
171     }
172 
173     /**
174      * Create a new association with the specified class description, role name,
175      * cardinality, and bound flag and choose a collection description that satisfies the
176      * specified boolean parameters.
177      *
178      * @param cd class description, must not be null
179      * @param roleName role name
180      * @param cardinality cardinality, must not be null
181      * @param bound true if this is a "bound" association
182      * @param indexed true if the collection should be indexed
183      * @param unique true if the collection should not allow duplicate elements
184      * @param ordered true if the collection should iterate over elements in <i>insertion-order</i>
185      * @param sorted true if the collection should iterate over elements in ascending element order,
186      *    sorted according to the <i>natural ordering</i> of its elements (see Comparable), or by a Comparator
187      *    provided at creation time
188      */
189     public Association(final ClassDescription cd,
190                        final String roleName,
191                        final Cardinality cardinality,
192                        final boolean bound,
193                        final boolean indexed,
194                        final boolean unique,
195                        final boolean ordered,
196                        final boolean sorted)
197     {
198         this.lower = cd.getLower();
199         this.mixed = cd.getMixed();
200         this.upper = cd.getUpper();
201         this.description = cd.getDescription();
202         this.packageName = cd.getPackageName();
203 
204         this.role = new Role(roleName);
205 
206         this.cardinality = cardinality;
207         if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
208         {
209             this.collectionDescription = CollectionDescription.choose(indexed, unique, ordered, sorted);
210         }
211         else
212         {
213             this.collectionDescription = null;
214         }
215         this.bound = bound;
216     }
217 
218     /**
219      * Create a new association with the specified interface description, role name,
220      * and cardinality.  The cardinality must be one of <b>Cardinality.ZeroToOne</b>
221      * or <b>Cardinality.StrictlyOne</b>.
222      *
223      * @param id interface description, must not be null
224      * @param roleName role name
225      * @param cardinality cardinality, must not be null and must be one
226      *    of <b>Cardinality.ZeroToOne</b> or <b>Cardinality.StrictlyOne</b>
227      */
228     public Association(final InterfaceDescription id,
229                        final String roleName,
230                        final Cardinality cardinality)
231     {
232         if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
233         {
234             throw new IllegalArgumentException("cardinality must be one of {ZeroToOne, StrictlyOne}");
235         }
236 
237         this.lower = id.getLower();
238         this.mixed = id.getMixed();
239         this.upper = id.getUpper();
240         this.description = id.getDescription();
241         this.packageName = id.getPackageName();
242 
243         this.role = new Role(roleName);
244 
245         this.cardinality = cardinality;
246         this.collectionDescription = null;
247         this.bound = false;
248     }
249 
250     /**
251      * Create a new association with the specified interface description, role name,
252      * and cardinality.  The cardinality must be one of <b>Cardinality.ZeroToOne</b>
253      * or <b>Cardinality.StrictlyOne</b>.
254      *
255      * @param id interface description, must not be null
256      * @param roleName role name
257      * @param cardinality cardinality, must not be null and must be one
258      *    of <b>Cardinality.ZeroToOne</b> or <b>Cardinality.StrictlyOne</b>
259      * @param bound true if this is a "bound" association
260      */
261     public Association(final InterfaceDescription id,
262                        final String roleName,
263                        final Cardinality cardinality,
264                        final boolean bound)
265     {
266         if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
267         {
268             throw new IllegalArgumentException("cardinality must be one of {ZeroToOne, StrictlyOne}");
269         }
270 
271         this.lower = id.getLower();
272         this.mixed = id.getMixed();
273         this.upper = id.getUpper();
274         this.description = id.getDescription();
275         this.packageName = id.getPackageName();
276 
277         this.role = new Role(roleName);
278 
279         this.cardinality = cardinality;
280         this.collectionDescription = null;
281         this.bound = bound;
282     }
283 
284     /**
285      * Create a new association with the specified interface description, role name, and
286      * cardinality and choose a collection description that satisfies the
287      * specified boolean parameters.
288      *
289      * @param id interface description, must not be null
290      * @param roleName role name
291      * @param cardinality cardinality, must not be null
292      * @param indexed true if the collection should be indexed
293      * @param unique true if the collection should not allow duplicate elements
294      * @param ordered true if the collection should iterate over elements in <i>insertion-order</i>
295      * @param sorted true if the collection should iterate over elements in ascending element order,
296      *    sorted according to the <i>natural ordering</i> of its elements (see Comparable), or by a Comparator
297      *    provided at creation time
298      */
299     public Association(final InterfaceDescription id,
300                        final String roleName,
301                        final Cardinality cardinality,
302                        final boolean indexed,
303                        final boolean unique,
304                        final boolean ordered,
305                        final boolean sorted)
306     {
307         this.lower = id.getLower();
308         this.mixed = id.getMixed();
309         this.upper = id.getUpper();
310         this.description = id.getDescription();
311         this.packageName = id.getPackageName();
312 
313         this.role = new Role(roleName);
314 
315         this.cardinality = cardinality;
316         if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
317         {
318             this.collectionDescription = CollectionDescription.choose(indexed, unique, ordered, sorted);
319         }
320         else
321         {
322             this.collectionDescription = null;
323         }
324         this.bound = false;
325     }
326 
327     /**
328      * Create a new association with the specified interface description, role name, and
329      * cardinality and choose a collection description that satisfies the
330      * specified boolean parameters.
331      *
332      * @param id interface description, must not be null
333      * @param roleName role name
334      * @param cardinality cardinality, must not be null
335      * @param bound true if this is a "bound" association
336      * @param indexed true if the collection should be indexed
337      * @param unique true if the collection should not allow duplicate elements
338      * @param ordered true if the collection should iterate over elements in <i>insertion-order</i>
339      * @param sorted true if the collection should iterate over elements in ascending element order,
340      *    sorted according to the <i>natural ordering</i> of its elements (see Comparable), or by a Comparator
341      *    provided at creation time
342      */
343     public Association(final InterfaceDescription id,
344                        final String roleName,
345                        final Cardinality cardinality,
346                        final boolean bound,
347                        final boolean indexed,
348                        final boolean unique,
349                        final boolean ordered,
350                        final boolean sorted)
351     {
352         this.lower = id.getLower();
353         this.mixed = id.getMixed();
354         this.upper = id.getUpper();
355         this.description = id.getDescription();
356         this.packageName = id.getPackageName();
357 
358         this.role = new Role(roleName);
359 
360         this.cardinality = cardinality;
361         if ((cardinality == Cardinality.ZeroToMany) || (cardinality == Cardinality.OneToMany))
362         {
363             this.collectionDescription = CollectionDescription.choose(indexed, unique, ordered, sorted);
364         }
365         else
366         {
367             this.collectionDescription = null;
368         }
369         this.bound = bound;
370     }
371 
372     /**
373      * Create a new association from the specified parameters.
374      *
375      * @param lower lowercase name for this association
376      * @param mixed mixed-case name for this association
377      * @param upper uppercase name for this association
378      * @param description description for this association
379      * @param role role for this association, must not be null
380      * @param cardinality cardinality for this association, must not be null
381      * @param collectionDescription collection description for this association
382      */
383     public Association(final String lower,
384                        final String mixed,
385                        final String upper,
386                        final String description,
387                        final Role role,
388                        final Cardinality cardinality,
389                        final CollectionDescription collectionDescription)
390     {
391         if (role == null)
392         {
393             throw new IllegalArgumentException("role must not be null");
394         }
395         if (cardinality == null)
396         {
397             throw new IllegalArgumentException("cardinality must not be null");
398         }
399 
400         this.lower = lower;
401         this.mixed = mixed;
402         this.upper = upper;
403         this.description = description;
404         this.packageName = "";
405         this.role = role;
406         this.cardinality = cardinality;
407         this.collectionDescription = collectionDescription;
408         this.bound = false;
409     }
410 
411     /**
412      * Create a new association from the specified parameters.
413      *
414      * @param lower lowercase name for this association
415      * @param mixed mixed-case name for this association
416      * @param upper uppercase name for this association
417      * @param description description for this association
418      * @param role role for this association, must not be null
419      * @param cardinality cardinality for this association, must not be null
420      * @param collectionDescription collection description for this association
421      * @param bound true if this is a "bound" association
422      */
423     public Association(final String lower,
424                        final String mixed,
425                        final String upper,
426                        final String description,
427                        final Role role,
428                        final Cardinality cardinality,
429                        final CollectionDescription collectionDescription,
430                        final boolean bound)
431     {
432         if (role == null)
433         {
434             throw new IllegalArgumentException("role must not be null");
435         }
436         if (cardinality == null)
437         {
438             throw new IllegalArgumentException("cardinality must not be null");
439         }
440 
441         this.lower = lower;
442         this.mixed = mixed;
443         this.upper = upper;
444         this.description = description;
445         this.packageName = "";
446         this.role = role;
447         this.cardinality = cardinality;
448         this.collectionDescription = collectionDescription;
449         this.bound = bound;
450     }
451 
452 
453     /**
454      * Return the lowercase name for this association.
455      *
456      * @return the lowercase name for this association
457      */
458     public String getLower()
459     {
460         return lower;
461     }
462 
463     /**
464      * Return the mixed-case name for this association.
465      *
466      * @return the mixed-case name for this association
467      */
468     public String getMixed()
469     {
470         return mixed;
471     }
472 
473     /**
474      * Return the uppercase name for this association.
475      *
476      * @return the uppercase name for this association
477      */
478     public String getUpper()
479     {
480         return upper;
481     }
482 
483     /**
484      * Return the description for this association.
485      *
486      * @return the description for this association
487      */
488     public String getDescription()
489     {
490         return description;
491     }
492 
493     /**
494      * Return the package name for this association.
495      *
496      * @return the package name for this association
497      */
498     public String getPackageName()
499     {
500         return packageName;
501     }
502 
503     /**
504      * Return the role for this association.  The role will not be null.
505      *
506      * @return the role for this association
507      */
508     public Role getRole()
509     {
510         return role;
511     }
512 
513     /**
514      * Return the cardinality for this association.  The cardinality will not be null.
515      *
516      * @return the cardinality for this association
517      */
518     public Cardinality getCardinality()
519     {
520         return cardinality;
521     }
522 
523     /**
524      * Return the collection description for this association.
525      * The collection description may be null.
526      *
527      * @return the collection description for this association
528      */
529     public CollectionDescription getCollectionDescription()
530     {
531         return collectionDescription;
532     }
533 
534     /**
535      * Return true if this is a "bound" association.
536      *
537      * @return true if this is a "bound" association
538      */
539     public boolean isBound()
540     {
541         return bound;
542     }
543 }