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 extends BaseElement {
36      private String packageName;
37  
38      private List<CodeSegment> codeSegments;
39  
40      private transient Model model;
41  
42      private transient Map<String, CodeSegment> codeSegmentMap = new HashMap<String, CodeSegment>();
43  
44      public ModelType() {
45          super(true);
46      }
47  
48      public ModelType(Model model, String name) {
49          super(true, name);
50  
51          this.model = model;
52      }
53  
54      public String getPackageName() {
55          return getPackageName(false, null);
56      }
57  
58      public String getPackageName(boolean withVersion, Version version) {
59          String p;
60  
61          if (packageName != null) {
62              p = packageName;
63          } else {
64              try {
65                  p = model.getDefault(ModelDefault.PACKAGE).getValue();
66              } catch (Exception e) {
67                  p = ModelDefault.PACKAGE_VALUE;
68              }
69          }
70  
71          if (withVersion) {
72              p += "." + version.toString("v", "_");
73          }
74  
75          return p;
76      }
77  
78      public void setPackageName(String packageName) {
79          this.packageName = packageName;
80      }
81  
82      public Model getModel() {
83          return model;
84      }
85  
86      // ----------------------------------------------------------------------
87      // CodeSegment
88      // ----------------------------------------------------------------------
89  
90      public List<CodeSegment> getAllCodeSegments() {
91          if (codeSegments == null) {
92              codeSegments = new ArrayList<CodeSegment>();
93          }
94  
95          return codeSegments;
96      }
97  
98      public List<CodeSegment> getCodeSegments(Version version) {
99          return getCodeSegments(new VersionRange(version));
100     }
101 
102     public List<CodeSegment> getCodeSegments(VersionRange versionRange) {
103         List<CodeSegment> codeSegments = getAllCodeSegments();
104 
105         List<CodeSegment> codeSegmentsList = new ArrayList<CodeSegment>();
106 
107         if (codeSegments != null) {
108             for (CodeSegment codeSegment : codeSegments) {
109                 if (versionRange.getFromVersion().inside(codeSegment.getVersionRange())
110                         && versionRange.getToVersion().inside(codeSegment.getVersionRange())) {
111                     codeSegmentsList.add(codeSegment);
112                 }
113             }
114         }
115 
116         return codeSegmentsList;
117     }
118 
119     public void addCodeSegment(CodeSegment codeSegment) {
120         getAllCodeSegments().add(codeSegment);
121 
122         codeSegmentMap.put(codeSegment.getName(), codeSegment);
123     }
124 
125     // ----------------------------------------------------------------------
126     // Field
127     // ----------------------------------------------------------------------
128 
129     /**
130      * Returns the list of all fields in this class.
131      *
132      * It does not include the fields of super classes.
133      *
134      * @return Returns the list of all fields in this class. It does not include the
135      *         fields of super classes.
136      */
137     public abstract List<ModelField> getAllFields();
138 
139     /**
140      * Returns all the fields in this class and all super classes if withInheritedField equals to true.
141      *
142      * @param withInheritedField whether inherited fields should be included.
143      * @return Returns all the fields in this class and all super classes.
144      */
145     public abstract List<ModelField> getAllFields(boolean withInheritedField);
146 
147     public abstract ModelField getField(String type, VersionRange versionRange);
148 
149     /**
150      * Returns the list of all fields in this class for a specific version.
151      *
152      * It does not include the fields of super classes.
153      *
154      * @param version the specific version
155      * @return Returns the list of all fields in this class. It does not include the
156      *         fields of super classes.
157      */
158     public List<ModelField> getFields(Version version) {
159         List<ModelField> fieldList = new ArrayList<ModelField>();
160 
161         for (ModelField currentField : getAllFields()) {
162             if (version.inside(currentField.getVersionRange())) {
163                 fieldList.add(currentField);
164             }
165         }
166 
167         return fieldList;
168     }
169 
170     public List<ModelField> getAllFields(Version version, boolean withInheritedField) {
171         List<ModelField> allFieldsList = new ArrayList<ModelField>();
172 
173         List<ModelField> fieldList = new ArrayList<ModelField>();
174 
175         for (ModelField currentField : getAllFields(withInheritedField)) {
176             if (version.inside(currentField.getVersionRange())) {
177                 allFieldsList.add(currentField);
178             }
179         }
180 
181         for (ModelField currentField : allFieldsList) {
182             if (version.inside(currentField.getVersionRange())) {
183                 fieldList.add(currentField);
184             }
185         }
186 
187         return fieldList;
188     }
189 
190     public boolean hasField(String type, Version version) {
191         try {
192             getField(type, new VersionRange(version));
193 
194             return true;
195         } catch (Exception e) {
196             return false;
197         }
198     }
199 
200     public ModelField getField(String type, Version version) {
201         return getField(type, new VersionRange(version));
202     }
203 
204     public List<ModelField> getIdentifierFields(Version version) {
205         List<ModelField> identifierFields = new ArrayList<ModelField>();
206 
207         for (ModelField field : getFields(version)) {
208             if (field.isIdentifier()) {
209                 identifierFields.add(field);
210             }
211         }
212 
213         return identifierFields;
214     }
215 
216     // ----------------------------------------------------------------------
217     //
218     // ----------------------------------------------------------------------
219 
220     public void initialize(Model model) {
221         this.model = model;
222 
223         if (packageName == null) {
224             packageName = model.getDefaultPackageName(false, null);
225         }
226     }
227 }