1 package org.opentrafficsim.base.parameters;
2
3 import java.io.Serializable;
4
5 import org.opentrafficsim.base.Identifiable;
6 import org.opentrafficsim.base.Type;
7 import org.opentrafficsim.base.parameters.constraint.Constraint;
8
9 import nl.tudelft.simulation.language.Throw;
10
11
12
13
14
15
16
17
18
19
20
21
22 public class ParameterType<T> extends Type<ParameterType<T>> implements Serializable, Identifiable
23 {
24
25
26 private static final long serialVersionUID = 20160400L;
27
28
29 private final String id;
30
31
32 private final String description;
33
34
35 private final Constraint<? super T> constraint;
36
37
38 private final Class<T> valueClass;
39
40
41 @SuppressWarnings("checkstyle:visibilitymodifier")
42 protected final T defaultValue;
43
44
45
46
47
48
49
50 public ParameterType(final String id, final String description, final Class<T> valueClass)
51 {
52 this(id, description, valueClass, null, null, false);
53 }
54
55
56
57
58
59
60
61
62 public ParameterType(final String id, final String description, final Class<T> valueClass,
63 final Constraint<? super T> constraint)
64 {
65 this(id, description, valueClass, null, constraint, false);
66 }
67
68
69
70
71
72
73
74
75 public ParameterType(final String id, final String description, final Class<T> valueClass, final T defaultValue)
76 {
77 this(id, description, valueClass, defaultValue, null, true);
78 }
79
80
81
82
83
84
85
86
87
88 public ParameterType(final String id, final String description, final Class<T> valueClass, final T defaultValue,
89 final Constraint<? super T> constraint)
90 {
91 this(id, description, valueClass, defaultValue, constraint, true);
92 }
93
94
95
96
97
98
99
100
101
102
103
104 private ParameterType(final String id, final String description, final Class<T> valueClass, final T defaultValue,
105 final Constraint<? super T> constraint, final boolean hasDefaultValue)
106 {
107 Throw.whenNull(id, "Id may not be null.");
108 Throw.whenNull(description, "Description may not be null.");
109 Throw.whenNull(valueClass, "Value class may not be null.");
110 if (hasDefaultValue)
111 {
112 Throw.whenNull(defaultValue, "Default values of parameter types may not be null.");
113 }
114 this.id = id;
115 this.description = description;
116 this.valueClass = valueClass;
117 this.defaultValue = defaultValue;
118 this.constraint = constraint;
119 if (this.defaultValue != null)
120 {
121 try
122 {
123 checkConstraint(this.defaultValue);
124 }
125 catch (ParameterException pe)
126 {
127 throw new RuntimeException(
128 "Default value of parameter '" + this.id + "' does not comply with default constraints.", pe);
129 }
130 try
131 {
132
133
134 check(defaultValue, new ParameterSet());
135 }
136 catch (ParameterException pe)
137 {
138 throw new RuntimeException(
139 "Default value of parameter '" + getId() + "' does not comply with custom constraints.", pe);
140 }
141 }
142 }
143
144
145
146
147
148 @Override
149 public final String getId()
150 {
151 return this.id;
152 }
153
154
155
156
157
158 public final String getDescription()
159 {
160 return this.description;
161 }
162
163
164
165
166
167 public final Class<T> getValueClass()
168 {
169 return this.valueClass;
170 }
171
172
173
174
175
176 public final boolean hasDefaultValue()
177 {
178 return this.defaultValue != null;
179 }
180
181
182
183
184
185
186 public final T getDefaultValue() throws ParameterException
187 {
188 Throw.when(null == this.defaultValue, ParameterException.class, "No default value was set for '%s'.", getId());
189 return this.defaultValue;
190 }
191
192
193
194
195
196
197 public final void checkConstraint(final T value) throws ParameterException
198 {
199 if (this.constraint == null)
200 {
201 return;
202 }
203 Throw.when(!this.constraint.accept(value), ParameterException.class, this.constraint.failMessage(), this.getId());
204 }
205
206
207
208
209
210
211
212 public void check(final T value, final Parameters params) throws ParameterException
213 {
214
215 }
216
217
218
219
220
221
222
223
224 public String printValue(final Parameters parameters) throws ParameterException
225 {
226 return parameters.getParameter(this).toString();
227 }
228
229
230 @Override
231 public final int hashCode()
232 {
233 final int prime = 31;
234 int result = 1;
235 result = prime * result + ((this.defaultValue == null) ? 0 : this.defaultValue.hashCode());
236 result = prime * result + this.description.hashCode();
237 result = prime * result + this.id.hashCode();
238 result = prime * result + this.valueClass.hashCode();
239 return result;
240 }
241
242
243 @Override
244 public final boolean equals(final Object obj)
245 {
246 if (this == obj)
247 {
248 return true;
249 }
250 if (obj == null)
251 {
252 return false;
253 }
254 if (getClass() != obj.getClass())
255 {
256 return false;
257 }
258 ParameterType<?> other = (ParameterType<?>) obj;
259 if (!this.id.equals(other.id))
260 {
261 return false;
262 }
263 if (!this.description.equals(other.description))
264 {
265 return false;
266 }
267 if (!this.valueClass.equals(other.valueClass))
268 {
269 return false;
270 }
271 if (this.defaultValue == null)
272 {
273 if (other.defaultValue != null)
274 {
275 return false;
276 }
277 }
278 else if (!this.defaultValue.equals(other.defaultValue))
279 {
280 return false;
281 }
282 return true;
283 }
284
285
286
287
288
289 public final Constraint<? super T> getConstraint()
290 {
291 return this.constraint;
292 }
293
294
295 @SuppressWarnings("checkstyle:designforextension")
296 @Override
297 public String toString()
298 {
299 return "ParameterType [id=" + this.id + ", description=" + this.description + ", valueClass=" + this.valueClass + "]";
300 }
301
302 }