View Javadoc

1   package org.codehaus.modello.model;
2   
3   /*
4    * Copyright (c) 2004, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  /**
31   * Either a model class or interface.
32   *
33   * @author <a href="mailto:hboutemy@codehaus.org">Hervé Boutemy</a>
34   */
35  public abstract class ModelType
36      extends BaseElement
37  {
38      private String packageName;
39  
40      private List<CodeSegment> codeSegments;
41  
42      private transient Model model;
43  
44      private transient Map<String, CodeSegment> codeSegmentMap = new HashMap<String, CodeSegment>();
45  
46      public ModelType()
47      {
48          super( true );
49      }
50  
51      public ModelType( Model model, String name )
52      {
53          super( true, name );
54  
55          this.model = model;
56      }
57  
58      public String getPackageName()
59      {
60          return getPackageName( false, null );
61      }
62  
63      public String getPackageName( boolean withVersion, Version version )
64      {
65          String p;
66  
67          if ( packageName != null )
68          {
69              p = packageName;
70          }
71          else
72          {
73              try
74              {
75                  p = model.getDefault( ModelDefault.PACKAGE ).getValue();
76              }
77              catch ( Exception e )
78              {
79                  p = ModelDefault.PACKAGE_VALUE;
80              }
81          }
82  
83          if ( withVersion )
84          {
85              p += "." + version.toString( "v", "_" );
86          }
87  
88          return p;
89      }
90  
91      public void setPackageName( String packageName )
92      {
93          this.packageName = packageName;
94      }
95  
96      public Model getModel()
97      {
98          return model;
99      }
100 
101     // ----------------------------------------------------------------------
102     // CodeSegment
103     // ----------------------------------------------------------------------
104 
105     public List<CodeSegment> getAllCodeSegments()
106     {
107         if ( codeSegments == null )
108         {
109             codeSegments = new ArrayList<CodeSegment>();
110         }
111 
112         return codeSegments;
113     }
114 
115     public List<CodeSegment> getCodeSegments( Version version )
116     {
117         return getCodeSegments( new VersionRange( version ) );
118     }
119 
120     public List<CodeSegment> getCodeSegments( VersionRange versionRange )
121     {
122         List<CodeSegment> codeSegments = getAllCodeSegments();
123 
124         List<CodeSegment> codeSegmentsList = new ArrayList<CodeSegment>();
125 
126         if ( codeSegments != null )
127         {
128             for ( CodeSegment codeSegment : codeSegments )
129             {
130                 if ( versionRange.getFromVersion().inside( codeSegment.getVersionRange() )
131                     && versionRange.getToVersion().inside( codeSegment.getVersionRange() ) )
132                 {
133                     codeSegmentsList.add( codeSegment );
134                 }
135             }
136         }
137 
138         return codeSegmentsList;
139     }
140 
141     public void addCodeSegment( CodeSegment codeSegment )
142     {
143         getAllCodeSegments().add( codeSegment );
144 
145         codeSegmentMap.put( codeSegment.getName(), codeSegment );
146     }
147 
148     // ----------------------------------------------------------------------
149     // Field
150     // ----------------------------------------------------------------------
151 
152     /**
153      * Returns the list of all fields in this class.
154      *
155      * It does not include the fields of super classes.
156      *
157      * @return Returns the list of all fields in this class. It does not include the
158      *         fields of super classes.
159      */
160     public abstract List<ModelField> getAllFields();
161 
162     /**
163      * Returns all the fields in this class and all super classes if withInheritedField equals to true.
164      *
165      * @return Returns all the fields in this class and all super classes.
166      */
167     public abstract List<ModelField> getAllFields( boolean withInheritedField );
168 
169     public abstract ModelField getField( String type, VersionRange versionRange );
170 
171     /**
172      * Returns the list of all fields in this class for a specific version.
173      *
174      * It does not include the fields of super classes.
175      *
176      * @return Returns the list of all fields in this class. It does not include the
177      *         fields of super classes.
178      */
179     public List<ModelField> getFields( Version version )
180     {
181         List<ModelField> fieldList = new ArrayList<ModelField>();
182 
183         for ( ModelField currentField : getAllFields() )
184         {
185             if ( version.inside( currentField.getVersionRange() ) )
186             {
187                 fieldList.add( currentField );
188             }
189         }
190 
191         return fieldList;
192     }
193 
194     public List<ModelField> getAllFields( Version version, boolean withInheritedField )
195     {
196         List<ModelField> allFieldsList = new ArrayList<ModelField>();
197 
198         List<ModelField> fieldList = new ArrayList<ModelField>();
199 
200         for ( ModelField currentField : getAllFields( withInheritedField ) )
201         {
202             if ( version.inside( currentField.getVersionRange() ) )
203             {
204                 allFieldsList.add( currentField );
205             }
206         }
207 
208         for ( ModelField currentField : allFieldsList )
209         {
210             if ( version.inside( currentField.getVersionRange() ) )
211             {
212                 fieldList.add( currentField );
213             }
214         }
215 
216         return fieldList;
217     }
218 
219     public boolean hasField( String type, Version version )
220     {
221         try
222         {
223             getField( type, new VersionRange( version ) );
224 
225             return true;
226         }
227         catch ( Exception e )
228         {
229             return false;
230         }
231     }
232 
233     public ModelField getField( String type, Version version )
234     {
235         return getField( type, new VersionRange( version ) );
236     }
237 
238     public List<ModelField> getIdentifierFields( Version version )
239     {
240         List<ModelField> identifierFields = new ArrayList<ModelField>();
241 
242         for ( ModelField field : getFields( version ) )
243         {
244             if ( field.isIdentifier() )
245             {
246                 identifierFields.add( field );
247             }
248         }
249 
250         return identifierFields;
251     }
252 
253     // ----------------------------------------------------------------------
254     //
255     // ----------------------------------------------------------------------
256 
257     public void initialize( Model model )
258     {
259         this.model = model;
260 
261         if ( packageName == null )
262         {
263             packageName = model.getDefaultPackageName( false, null );
264         }
265     }
266 }