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 org.codehaus.modello.ModelloRuntimeException;
26  import org.codehaus.modello.metadata.Metadata;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  import java.util.ArrayList;
31  import java.util.List;
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  {
42      private String name;
43  
44      private String description;
45  
46      private String comment;
47  
48      private List<String> annotations = new ArrayList<String>();
49  
50      private VersionRange versionRange = new VersionRange( "0.0.0+" );
51  
52      private Version deprecatedVersion;
53  
54      private transient Map<String, Metadata> metadata = new HashMap<String, Metadata>();
55  
56      private boolean nameRequired;
57  
58      public abstract void validateElement()
59          throws ModelValidationException;
60  
61      public BaseElement( boolean nameRequired )
62      {
63          this.nameRequired = nameRequired;
64  
65          this.name = null;
66      }
67  
68      public BaseElement( boolean nameRequired, String name )
69      {
70          this.nameRequired = nameRequired;
71  
72          this.name = name;
73      }
74  
75      public String getName()
76      {
77          return name;
78      }
79  
80      public void setName( String name )
81      {
82          this.name = name;
83      }
84  
85      public String getDescription()
86      {
87          return description;
88      }
89  
90      public void setDescription( String description )
91      {
92          this.description = description;
93      }
94  
95      public VersionRange getVersionRange()
96      {
97          return versionRange;
98      }
99  
100     public void setVersionRange( VersionRange versionRange )
101     {
102         this.versionRange = versionRange;
103     }
104 
105     public void setDeprecatedVersion( Version deprecatedVersion )
106     {
107         this.deprecatedVersion = deprecatedVersion;
108     }
109 
110     public Version getDeprecatedVersion()
111     {
112         return deprecatedVersion;
113     }
114 
115     public String getComment()
116     {
117         return comment;
118     }
119 
120     public void setComment( String comment )
121     {
122         this.comment = comment;
123     }
124 
125     public boolean hasMetadata( String key )
126     {
127         return metadata.containsKey( key );
128     }
129 
130     public void addMetadata( Metadata metadata )
131     {
132         this.metadata.put( metadata.getClass().getName(), metadata );
133     }
134 
135     protected <T extends Metadata> T getMetadata( Class<T> type, String key )
136     {
137         Metadata metadata = this.metadata.get( key );
138 
139         if ( metadata == null )
140         {
141             throw new ModelloRuntimeException( "No such metadata: '" + key + "' for element: '" + getName() + "'." );
142         }
143 
144         if ( !type.isInstance( metadata ) )
145         {
146             throw new ModelloRuntimeException( "The metadata is not of the expected type. Key: '" + key
147                 + "', expected type: '" + type.getName() + "'." );
148         }
149 
150         return type.cast( metadata );
151     }
152 
153     // ----------------------------------------------------------------------
154     // Validation utils
155     // ----------------------------------------------------------------------
156 
157     protected void validateFieldNotEmpty( String objectName, String fieldName, String value )
158         throws ModelValidationException
159     {
160         if ( value == null )
161         {
162             throw new ModelValidationException( "Missing value '" + fieldName + "' from " + objectName + "." );
163         }
164 
165         if ( isEmpty( value ) )
166         {
167             throw new ModelValidationException( "Empty value '" + fieldName + "' from " + objectName + "." );
168         }
169     }
170 
171     public final void validate()
172         throws ModelValidationException
173     {
174         if ( nameRequired )
175         {
176             validateFieldNotEmpty( "Element.name", "name", name );
177         }
178 
179         validateElement();
180     }
181 
182     protected boolean isEmpty( String string )
183     {
184         return string == null || string.trim().length() == 0;
185     }
186 
187     // ----------------------------------------------------------------------
188     //
189     // ----------------------------------------------------------------------
190 
191     public boolean equals( Object other )
192     {
193         if ( other == null || !( other instanceof BaseElement ) )
194         {
195             return false;
196         }
197 
198         // If we don't know how to identify this object it's not equal to any other object
199         if ( !nameRequired )
200         {
201             return false;
202         }
203 
204         BaseElement baseElem = (BaseElement) other;
205 
206         return name.equals( baseElem.getName() ) && versionRange.equals( baseElem.getVersionRange() );
207     }
208 
209     public int hashCode()
210     {
211         if ( !nameRequired )
212         {
213             return System.identityHashCode( this );
214         }
215 
216         return name.hashCode() + versionRange.toString().hashCode();
217     }
218 
219     /**
220      * @return the annotations
221      */
222     public List<String> getAnnotations()
223     {
224         return annotations;
225     }
226 
227     /**
228      * @param annotations the annotations to set
229      */
230     public void setAnnotations( List<String> annotations )
231     {
232         this.annotations = annotations;
233     }
234 }