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      * @param withInheritedField whether inherited fields should be included.
166      * @return Returns all the fields in this class and all super classes.
167      */
168     public abstract List<ModelField> getAllFields( boolean withInheritedField );
169 
170     public abstract ModelField getField( String type, VersionRange versionRange );
171 
172     /**
173      * Returns the list of all fields in this class for a specific version.
174      *
175      * It does not include the fields of super classes.
176      *
177      * @param version the specific version
178      * @return Returns the list of all fields in this class. It does not include the
179      *         fields of super classes.
180      */
181     public List<ModelField> getFields( Version version )
182     {
183         List<ModelField> fieldList = new ArrayList<ModelField>();
184 
185         for ( ModelField currentField : getAllFields() )
186         {
187             if ( version.inside( currentField.getVersionRange() ) )
188             {
189                 fieldList.add( currentField );
190             }
191         }
192 
193         return fieldList;
194     }
195 
196     public List<ModelField> getAllFields( Version version, boolean withInheritedField )
197     {
198         List<ModelField> allFieldsList = new ArrayList<ModelField>();
199 
200         List<ModelField> fieldList = new ArrayList<ModelField>();
201 
202         for ( ModelField currentField : getAllFields( withInheritedField ) )
203         {
204             if ( version.inside( currentField.getVersionRange() ) )
205             {
206                 allFieldsList.add( currentField );
207             }
208         }
209 
210         for ( ModelField currentField : allFieldsList )
211         {
212             if ( version.inside( currentField.getVersionRange() ) )
213             {
214                 fieldList.add( currentField );
215             }
216         }
217 
218         return fieldList;
219     }
220 
221     public boolean hasField( String type, Version version )
222     {
223         try
224         {
225             getField( type, new VersionRange( version ) );
226 
227             return true;
228         }
229         catch ( Exception e )
230         {
231             return false;
232         }
233     }
234 
235     public ModelField getField( String type, Version version )
236     {
237         return getField( type, new VersionRange( version ) );
238     }
239 
240     public List<ModelField> getIdentifierFields( Version version )
241     {
242         List<ModelField> identifierFields = new ArrayList<ModelField>();
243 
244         for ( ModelField field : getFields( version ) )
245         {
246             if ( field.isIdentifier() )
247             {
248                 identifierFields.add( field );
249             }
250         }
251 
252         return identifierFields;
253     }
254 
255     // ----------------------------------------------------------------------
256     //
257     // ----------------------------------------------------------------------
258 
259     public void initialize( Model model )
260     {
261         this.model = model;
262 
263         if ( packageName == null )
264         {
265             packageName = model.getDefaultPackageName( false, null );
266         }
267     }
268 }