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  import org.codehaus.modello.ModelloRuntimeException;
31  import org.codehaus.modello.metadata.Metadata;
32  
33  /**
34   * This is the base class for all elements of the model. The name attribute is immutable because it's used as the key.
35   *
36   * @author <a href="mailto:jason@modello.org">Jason van Zyl</a>
37   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
38   * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
39   */
40  public abstract class BaseElement {
41      private String name;
42  
43      private String description;
44  
45      private String comment;
46  
47      private List<String> annotations = new ArrayList<String>();
48  
49      private VersionRange versionRange = new VersionRange("0.0.0+");
50  
51      private Version deprecatedVersion;
52  
53      private transient Map<String, Metadata> metadata = new HashMap<String, Metadata>();
54  
55      private boolean nameRequired;
56  
57      public abstract void validateElement() throws ModelValidationException;
58  
59      public BaseElement(boolean nameRequired) {
60          this.nameRequired = nameRequired;
61  
62          this.name = null;
63      }
64  
65      public BaseElement(boolean nameRequired, String name) {
66          this.nameRequired = nameRequired;
67  
68          this.name = name;
69      }
70  
71      public String getName() {
72          return name;
73      }
74  
75      public void setName(String name) {
76          this.name = name;
77      }
78  
79      public String getDescription() {
80          return description;
81      }
82  
83      public void setDescription(String description) {
84          this.description = description;
85      }
86  
87      public VersionRange getVersionRange() {
88          return versionRange;
89      }
90  
91      public void setVersionRange(VersionRange versionRange) {
92          this.versionRange = versionRange;
93      }
94  
95      public void setDeprecatedVersion(Version deprecatedVersion) {
96          this.deprecatedVersion = deprecatedVersion;
97      }
98  
99      public Version getDeprecatedVersion() {
100         return deprecatedVersion;
101     }
102 
103     public String getComment() {
104         return comment;
105     }
106 
107     public void setComment(String comment) {
108         this.comment = comment;
109     }
110 
111     public boolean hasMetadata(String key) {
112         return metadata.containsKey(key);
113     }
114 
115     public void addMetadata(Metadata metadata) {
116         this.metadata.put(metadata.getClass().getName(), metadata);
117     }
118 
119     protected <T extends Metadata> T getMetadata(Class<T> type, String key) {
120         Metadata metadata = this.metadata.get(key);
121 
122         if (metadata == null) {
123             throw new ModelloRuntimeException("No such metadata: '" + key + "' for element: '" + getName() + "'.");
124         }
125 
126         if (!type.isInstance(metadata)) {
127             throw new ModelloRuntimeException("The metadata is not of the expected type. Key: '" + key
128                     + "', expected type: '" + type.getName() + "'.");
129         }
130 
131         return type.cast(metadata);
132     }
133 
134     // ----------------------------------------------------------------------
135     // Validation utils
136     // ----------------------------------------------------------------------
137 
138     protected void validateFieldNotEmpty(String objectName, String fieldName, String value)
139             throws ModelValidationException {
140         if (value == null) {
141             throw new ModelValidationException("Missing value '" + fieldName + "' from " + objectName + ".");
142         }
143 
144         if (isEmpty(value)) {
145             throw new ModelValidationException("Empty value '" + fieldName + "' from " + objectName + ".");
146         }
147     }
148 
149     public final void validate() throws ModelValidationException {
150         if (nameRequired) {
151             validateFieldNotEmpty("Element.name", "name", name);
152         }
153 
154         validateElement();
155     }
156 
157     protected boolean isEmpty(String string) {
158         return string == null || string.trim().length() == 0;
159     }
160 
161     // ----------------------------------------------------------------------
162     //
163     // ----------------------------------------------------------------------
164 
165     public boolean equals(Object other) {
166         if (other == null || !(other instanceof BaseElement)) {
167             return false;
168         }
169 
170         // If we don't know how to identify this object it's not equal to any other object
171         if (!nameRequired) {
172             return false;
173         }
174 
175         BaseElement baseElem = (BaseElement) other;
176 
177         return name.equals(baseElem.getName()) && versionRange.equals(baseElem.getVersionRange());
178     }
179 
180     public int hashCode() {
181         if (!nameRequired) {
182             return System.identityHashCode(this);
183         }
184 
185         return name.hashCode() + versionRange.toString().hashCode();
186     }
187 
188     /**
189      * @return the annotations
190      */
191     public List<String> getAnnotations() {
192         return annotations;
193     }
194 
195     /**
196      * @param annotations the annotations to set
197      */
198     public void setAnnotations(List<String> annotations) {
199         this.annotations = annotations;
200     }
201 }