View Javadoc

1   package org.andromda.core.uml14;
2   
3   import java.util.Collection;
4   import java.util.Iterator;
5   
6   import org.omg.uml.foundation.core.AssociationEnd;
7   import org.omg.uml.foundation.datatypes.Multiplicity;
8   import org.omg.uml.foundation.datatypes.MultiplicityRange;
9   
10  /***
11   * Implements a set of operations that are useful for querying
12   * an association from the perspective of 
13   * a given association end.
14   * 
15   * <p> Useful for answering question such as: isMany2Many, isOne2Many, ...</p>
16   * 
17   * @author Anthony Mowers
18   */
19  public class DirectionalAssociationEnd
20  {
21      protected AssociationEnd associationEnd;
22  
23      public DirectionalAssociationEnd(
24          AssociationEnd associationEnd)
25      {
26          this.associationEnd = associationEnd;
27      }
28  
29      /***
30       * get the near end of the association
31       */
32      public AssociationEnd getSource()
33      {
34          return associationEnd;
35      }
36  
37      /***
38       * get the far end of the association
39       */
40      public AssociationEnd getTarget()
41      {
42          return getOtherEnd(); 
43      }
44      
45      /***
46       * get the name of the association
47       */
48      public String getName()
49      {
50          return associationEnd.getAssociation().getName();
51      }
52      
53      /***
54       * get a string that can be used to uniquely id this association
55       */ 
56      public String getId()
57      {
58          return associationEnd.getAssociation().refMofId();
59      }
60      
61      public boolean isOne2Many()
62      {
63          return !isMany(associationEnd) && isMany(getOtherEnd());
64      }
65      
66      public boolean isMany2Many()
67      {
68          return isMany(associationEnd) && isMany(getOtherEnd());
69      }
70  
71      public boolean isOne2One()
72      {
73          return !isMany(associationEnd) && !isMany(getOtherEnd());
74      }
75      
76      public boolean isMany2One()
77      {
78          return isMany(associationEnd) && !isMany(getOtherEnd());
79      }
80      
81      static protected boolean isMany(AssociationEnd ae)
82      {
83          Multiplicity multiplicity = ae.getMultiplicity();
84          if (multiplicity == null)
85          {
86              return false;  // no multiplicity means multiplicity==1
87          }
88          Collection ranges = multiplicity.getRange();
89          
90          for (Iterator i = ranges.iterator(); i.hasNext() ; )
91          {
92              MultiplicityRange range = (MultiplicityRange)i.next();
93              if ( range.getUpper() > 1 )
94              {
95                  return true;
96              }
97              
98              int rangeSize = range.getUpper() - range.getLower();
99              if (rangeSize < 0)
100             {
101                 return true;
102             }
103             
104         }
105         
106         return false;
107     }
108     
109     protected AssociationEnd getOtherEnd()
110     {
111         AssociationEnd otherEnd;
112         
113         Collection ends = associationEnd.getAssociation().getConnection();
114         for (Iterator i = ends.iterator(); i.hasNext(); )
115         {
116             AssociationEnd ae = (AssociationEnd)i.next();
117             if (!associationEnd.equals(ae))
118             {
119                 return ae;
120             }
121         }
122         
123         return null;
124     }
125     
126         
127 }