1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */
7 package javax.management.openmbean;
8
9 import java.io.Serializable;
10
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.Set;
15
16 import javax.management.MBeanParameterInfo;
17
18 /**
19 * OpenMBeanParameterInfo implementation
20 *
21 * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
22 *
23 * @version $Revision: 1.1.2.1 $
24 *
25 */
26 public class OpenMBeanParameterInfoSupport
27 extends MBeanParameterInfo
28 implements OpenMBeanParameterInfo, Serializable
29 {
30 // Constants -----------------------------------------------------
31
32 private static final long serialVersionUID = -7235016873758443122L;
33
34 // Attributes ----------------------------------------------------
35
36 /**
37 * The OpenType of this parameter
38 */
39 private OpenType openType;
40
41 /**
42 * The default value of this parameter
43 */
44 private Object defaultValue;
45
46 /**
47 * The legal values of this parameter
48 */
49 private Set legalValues;
50
51 /**
52 * The minimum value of this parameter
53 */
54 private Comparable minValue;
55
56 /**
57 * The maximum value of this parameter
58 */
59 private Comparable maxValue;
60
61 private transient int cachedHashCode;
62
63 private transient String cachedToString;
64
65 // Static --------------------------------------------------------
66
67 // Constructors --------------------------------------------------
68
69 /**
70 * Contruct an OpenMBeanParameterInfoSupport<p>
71 *
72 * @param name cannot be null or empty
73 * @param description cannot be null or empty
74 * @param openType cannot be null
75 * @exception IllegalArgumentException when one of the above
76 * constraints is not satisfied
77 */
78 public OpenMBeanParameterInfoSupport(String name, String description,
79 OpenType openType)
80 {
81 super(name, openType == null ? null : openType.getClassName(), description);
82 try
83 {
84 init(name, description, openType, null, null, null, null);
85 }
86 catch (OpenDataException notRelevent)
87 {
88 }
89 }
90
91 /**
92 * Contruct an OpenMBeanParameterInfoSupport<p>
93 *
94 * @param name cannot be null or empty
95 * @param description cannot be null or empty
96 * @param openType cannot be null
97 * @param defaultValue the default value
98 * @exception IllegalArgumentException when one of the above
99 * constraints is not satisfied
100 * @exception OpenDataException when default value is not correct for
101 * the open type or cannot specify a default value for
102 * ArrayType and TabularType
103 */
104 public OpenMBeanParameterInfoSupport(String name, String description,
105 OpenType openType, Object defaultValue)
106 throws OpenDataException
107 {
108 super(name, openType == null ? null : openType.getClassName(), description);
109 init(name, description, openType, defaultValue, null, null, null);
110 }
111
112 /**
113 * Contruct an OpenMBeanParameterInfoSupport<p>
114 *
115 * @param name cannot be null or empty
116 * @param description cannot be null or empty
117 * @param openType cannot be null
118 * @param defaultValue the default value
119 * @param legalValues an array of legal values
120 * @exception IllegalArgumentException when one of the above
121 * constraints is not satisfied
122 */
123 public OpenMBeanParameterInfoSupport(String name, String description,
124 OpenType openType, Object defaultValue,
125 Object[] legalValues)
126 throws OpenDataException
127 {
128 super(name, openType == null ? null : openType.getClassName(), description);
129 init(name, description, openType, defaultValue, legalValues, null, null);
130 }
131
132 /**
133 * Contruct an OpenMBeanParameterInfoSupport<p>
134 *
135 * @param name cannot be null or empty
136 * @param description cannot be null or empty
137 * @param openType cannot be null
138 * @param defaultValue the default value
139 * @param minValue the minimum value
140 * @param maxValue the maximum value
141 * @exception IllegalArgumentException when one of the above
142 * constraints is not satisfied
143 */
144 public OpenMBeanParameterInfoSupport(String name, String description,
145 OpenType openType, Object defaultValue,
146 Comparable minValue, Comparable maxValue)
147 throws OpenDataException
148 {
149 super(name, openType == null ? null : openType.getClassName(), description);
150 init(name, description, openType, defaultValue, null, minValue, maxValue);
151 }
152
153 // Public --------------------------------------------------------
154
155 // OpenMBeanParameterInfo Implementation -------------------------
156
157 public Object getDefaultValue()
158 {
159 return defaultValue;
160 }
161
162 public Set getLegalValues()
163 {
164 return legalValues;
165 }
166
167 public Comparable getMinValue()
168 {
169 return minValue;
170 }
171
172 public Comparable getMaxValue()
173 {
174 return maxValue;
175 }
176
177 public OpenType getOpenType()
178 {
179 return openType;
180 }
181
182 public boolean hasDefaultValue()
183 {
184 return (defaultValue != null);
185 }
186
187 public boolean hasLegalValues()
188 {
189 return (legalValues != null);
190 }
191
192 public boolean hasMinValue()
193 {
194 return (minValue != null);
195 }
196
197 public boolean hasMaxValue()
198 {
199 return (maxValue != null);
200 }
201
202 public boolean isValue(Object obj)
203 {
204 if (openType.isValue(obj) == false)
205 return false;
206 if (minValue != null && minValue.compareTo(obj) > 0)
207 return false;
208 if (maxValue != null && maxValue.compareTo(obj) < 0)
209 return false;
210 if (legalValues != null && legalValues.contains(obj) == false)
211 return false;
212 return true;
213 }
214
215 // Object Overrides ----------------------------------------------
216
217 public boolean equals(Object obj)
218 {
219 if (this == obj)
220 return true;
221 if (obj == null || !(obj instanceof OpenMBeanParameterInfoSupport))
222 return false;
223 OpenMBeanParameterInfo other = (OpenMBeanParameterInfo) obj;
224
225 if (this.getName().equals(other.getName()) == false)
226 return false;
227
228 if (this.getOpenType().equals(other.getOpenType()) == false)
229 return false;
230
231 if (hasDefaultValue() == false && other.hasDefaultValue() == true)
232 return false;
233 if (hasDefaultValue() == true && this.getDefaultValue().equals(other.getDefaultValue()) == false)
234 return false;
235
236 if (hasMinValue() == false && other.hasMinValue() == true)
237 return false;
238 if (hasMinValue() == true && this.getMinValue().equals(other.getMinValue()) == false)
239 return false;
240
241 if (hasMaxValue() == false && other.hasMaxValue() == true)
242 return false;
243 if (hasMaxValue() == true && this.getMaxValue().equals(other.getMaxValue()) == false)
244 return false;
245
246 if (hasLegalValues() == false && other.hasLegalValues() == true)
247 return false;
248 if (hasLegalValues() == true)
249 {
250 Set otherLegal = other.getLegalValues();
251 if (otherLegal == null)
252 return false;
253 Set thisLegal = this.getLegalValues();
254 if (thisLegal.size() != otherLegal.size())
255 return false;
256 if (thisLegal.containsAll(otherLegal) == false)
257 return false;
258 }
259 return true;
260 }
261
262 public int hashCode()
263 {
264 if (cachedHashCode != 0)
265 return cachedHashCode;
266 cachedHashCode = getName().hashCode();
267 cachedHashCode += getOpenType().hashCode();
268 if (defaultValue != null)
269 cachedHashCode += getDefaultValue().hashCode();
270 if (minValue != null)
271 cachedHashCode += getMinValue().hashCode();
272 if (maxValue != null)
273 cachedHashCode += getMaxValue().hashCode();
274 if (legalValues != null)
275 cachedHashCode += getLegalValues().hashCode();
276 return cachedHashCode;
277 }
278
279 public String toString()
280 {
281 if (cachedToString != null)
282 return cachedToString;
283 StringBuffer buffer = new StringBuffer(getClass().getName());
284 buffer.append(": name=");
285 buffer.append(getName());
286 buffer.append(", openType=");
287 buffer.append(getOpenType());
288 buffer.append(", defaultValue=");
289 buffer.append(getDefaultValue());
290 buffer.append(", minValue=");
291 buffer.append(getMinValue());
292 buffer.append(", maxValue=");
293 buffer.append(getMaxValue());
294 buffer.append(", legalValues=");
295 buffer.append(getLegalValues());
296 cachedToString = buffer.toString();
297 return cachedToString;
298 }
299
300 // Protected -----------------------------------------------------
301
302 // Private -------------------------------------------------------
303
304 /**
305 * Initialise an OpenMBeanParameterInfoSupport<p>
306 *
307 * WARNING: For the MBeanParameterInfo only validation is performed
308 *
309 * @param name cannot be null or empty
310 * @param description cannot be null or empty
311 * @param openType cannot be null
312 * @param defaultValue the default value
313 * @param minValue the minimum value
314 * @param maxValue the maximum value
315 * @exception IllegalArgumentException when one of the above
316 * constraints is not satisfied
317 */
318 private void init(String name, String Description,
319 OpenType openType, Object defaultValue, Object[] legalValues,
320 Comparable minValue, Comparable maxValue)
321 throws OpenDataException
322 {
323 if (name == null || name.trim().length() == 0)
324 throw new IllegalArgumentException("null or empty name");
325
326 if (description == null || description.trim().length() == 0)
327 throw new IllegalArgumentException("null or empty description");
328
329 if (openType == null)
330 throw new IllegalArgumentException("null open type");
331 this.openType = openType;
332
333 if (defaultValue != null &&
334 (openType instanceof ArrayType || openType instanceof TabularType))
335 throw new OpenDataException("default value is not supported for "
336 + openType.getClass().getName());
337 if (defaultValue != null && openType.isValue(defaultValue) == false)
338 throw new OpenDataException("default value is not valid for "
339 + openType.getClass().getName());
340
341 if (legalValues != null && legalValues.length != 0)
342 {
343 if (openType instanceof ArrayType || openType instanceof TabularType)
344 throw new OpenDataException("legal values are not supported for "
345 + openType.getClass().getName());
346 HashSet legals = new HashSet(legalValues.length);
347 for (int i = 0; i < legalValues.length; i++)
348 {
349 if (openType.isValue(legalValues[i]) == false)
350 throw new OpenDataException("legal value " + legalValues[i] + " at index " + i
351 + " is not valid for " + openType.getClass().getName());
352 legals.add(legalValues[i]);
353 }
354 if (defaultValue != null && legals.contains(defaultValue) == false)
355 throw new OpenDataException("default value is not a legal value");
356 this.legalValues = Collections.unmodifiableSet(legals);
357 }
358
359 if (minValue != null && openType.isValue(minValue) == false)
360 throw new OpenDataException("minimum value is not valid for "
361 + openType.getClass().getName());
362 if (defaultValue != null && minValue != null && minValue.compareTo(defaultValue) > 0)
363 throw new OpenDataException("the default value is less than the minimum value ");
364
365 if (maxValue != null && openType.isValue(maxValue) == false)
366 throw new OpenDataException("maximum value is not valid for "
367 + openType.getClass().getName());
368 if (defaultValue != null && maxValue != null && maxValue.compareTo(defaultValue) < 0)
369 throw new OpenDataException("the default value is greater than the maximum value ");
370
371 if (minValue != null && maxValue != null && minValue.compareTo(maxValue) > 0)
372 throw new OpenDataException("the minimum value is greater than the maximum value ");
373
374 this.defaultValue = defaultValue;
375 this.minValue = minValue;
376 this.maxValue = maxValue;
377 }
378
379 // Inner Classes -------------------------------------------------
380 }