View Javadoc

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