1 package org.codehaus.modello.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
35
36
37
38
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
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
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
190
191 public List<String> getAnnotations() {
192 return annotations;
193 }
194
195
196
197
198 public void setAnnotations(List<String> annotations) {
199 this.annotations = annotations;
200 }
201 }