001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package javax.management.openmbean;
023:
024: import java.io.Serializable;
025: import java.util.Collections;
026: import java.util.HashSet;
027: import java.util.Set;
028:
029: import javax.management.MBeanParameterInfo;
030:
031: /**
032: * OpenMBeanParameterInfo implementation
033: *
034: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
035: *
036: * @version $Revision: 57200 $
037: *
038: */
039: public class OpenMBeanParameterInfoSupport extends MBeanParameterInfo
040: implements OpenMBeanParameterInfo, Serializable {
041: // Constants -----------------------------------------------------
042:
043: private static final long serialVersionUID = -7235016873758443122L;
044:
045: // Attributes ----------------------------------------------------
046:
047: /**
048: * The OpenType of this parameter
049: */
050: private OpenType openType;
051:
052: /**
053: * The default value of this parameter
054: */
055: private Object defaultValue;
056:
057: /**
058: * The legal values of this parameter
059: */
060: private Set legalValues;
061:
062: /**
063: * The minimum value of this parameter
064: */
065: private Comparable minValue;
066:
067: /**
068: * The maximum value of this parameter
069: */
070: private Comparable maxValue;
071:
072: private transient int cachedHashCode;
073:
074: private transient String cachedToString;
075:
076: // Static --------------------------------------------------------
077:
078: // Constructors --------------------------------------------------
079:
080: /**
081: * Contruct an OpenMBeanParameterInfoSupport<p>
082: *
083: * @param name cannot be null or empty
084: * @param description cannot be null or empty
085: * @param openType cannot be null
086: * @exception IllegalArgumentException when one of the above
087: * constraints is not satisfied
088: */
089: public OpenMBeanParameterInfoSupport(String name,
090: String description, OpenType openType) {
091: super (name, openType == null ? null : openType.getClassName(),
092: description);
093: try {
094: init(name, description, openType, null, null, null, null);
095: } catch (OpenDataException notRelevent) {
096: }
097: }
098:
099: /**
100: * Contruct an OpenMBeanParameterInfoSupport<p>
101: *
102: * @param name cannot be null or empty
103: * @param description cannot be null or empty
104: * @param openType cannot be null
105: * @param defaultValue the default value
106: * @exception IllegalArgumentException when one of the above
107: * constraints is not satisfied
108: * @exception OpenDataException when default value is not correct for
109: * the open type or cannot specify a default value for
110: * ArrayType and TabularType
111: */
112: public OpenMBeanParameterInfoSupport(String name,
113: String description, OpenType openType, Object defaultValue)
114: throws OpenDataException {
115: super (name, openType == null ? null : openType.getClassName(),
116: description);
117: init(name, description, openType, defaultValue, null, null,
118: null);
119: }
120:
121: /**
122: * Contruct an OpenMBeanParameterInfoSupport<p>
123: *
124: * @param name cannot be null or empty
125: * @param description cannot be null or empty
126: * @param openType cannot be null
127: * @param defaultValue the default value
128: * @param legalValues an array of legal values
129: * @exception IllegalArgumentException when one of the above
130: * constraints is not satisfied
131: */
132: public OpenMBeanParameterInfoSupport(String name,
133: String description, OpenType openType, Object defaultValue,
134: Object[] legalValues) throws OpenDataException {
135: super (name, openType == null ? null : openType.getClassName(),
136: description);
137: init(name, description, openType, defaultValue, legalValues,
138: null, null);
139: }
140:
141: /**
142: * Contruct an OpenMBeanParameterInfoSupport<p>
143: *
144: * @param name cannot be null or empty
145: * @param description cannot be null or empty
146: * @param openType cannot be null
147: * @param defaultValue the default value
148: * @param minValue the minimum value
149: * @param maxValue the maximum value
150: * @exception IllegalArgumentException when one of the above
151: * constraints is not satisfied
152: */
153: public OpenMBeanParameterInfoSupport(String name,
154: String description, OpenType openType, Object defaultValue,
155: Comparable minValue, Comparable maxValue)
156: throws OpenDataException {
157: super (name, openType == null ? null : openType.getClassName(),
158: description);
159: init(name, description, openType, defaultValue, null, minValue,
160: maxValue);
161: }
162:
163: // Public --------------------------------------------------------
164:
165: // OpenMBeanParameterInfo Implementation -------------------------
166:
167: public Object getDefaultValue() {
168: return defaultValue;
169: }
170:
171: public Set getLegalValues() {
172: return legalValues;
173: }
174:
175: public Comparable getMinValue() {
176: return minValue;
177: }
178:
179: public Comparable getMaxValue() {
180: return maxValue;
181: }
182:
183: public OpenType getOpenType() {
184: return openType;
185: }
186:
187: public boolean hasDefaultValue() {
188: return (defaultValue != null);
189: }
190:
191: public boolean hasLegalValues() {
192: return (legalValues != null);
193: }
194:
195: public boolean hasMinValue() {
196: return (minValue != null);
197: }
198:
199: public boolean hasMaxValue() {
200: return (maxValue != null);
201: }
202:
203: public boolean isValue(Object obj) {
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: if (this == obj)
219: return true;
220: if (obj == null
221: || !(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
232: && other.hasDefaultValue() == true)
233: return false;
234: if (hasDefaultValue() == true
235: && this .getDefaultValue().equals(
236: other.getDefaultValue()) == false)
237: return false;
238:
239: if (hasMinValue() == false && other.hasMinValue() == true)
240: return false;
241: if (hasMinValue() == true
242: && this .getMinValue().equals(other.getMinValue()) == false)
243: return false;
244:
245: if (hasMaxValue() == false && other.hasMaxValue() == true)
246: return false;
247: if (hasMaxValue() == true
248: && this .getMaxValue().equals(other.getMaxValue()) == false)
249: return false;
250:
251: if (hasLegalValues() == false && other.hasLegalValues() == true)
252: return false;
253: if (hasLegalValues() == true) {
254: Set otherLegal = other.getLegalValues();
255: if (otherLegal == null)
256: return false;
257: Set this Legal = this .getLegalValues();
258: if (this Legal.size() != otherLegal.size())
259: return false;
260: if (this Legal.containsAll(otherLegal) == false)
261: return false;
262: }
263: return true;
264: }
265:
266: public int hashCode() {
267: if (cachedHashCode != 0)
268: return cachedHashCode;
269: cachedHashCode = getName().hashCode();
270: cachedHashCode += getOpenType().hashCode();
271: if (defaultValue != null)
272: cachedHashCode += getDefaultValue().hashCode();
273: if (minValue != null)
274: cachedHashCode += getMinValue().hashCode();
275: if (maxValue != null)
276: cachedHashCode += getMaxValue().hashCode();
277: if (legalValues != null)
278: cachedHashCode += getLegalValues().hashCode();
279: return cachedHashCode;
280: }
281:
282: public String toString() {
283: if (cachedToString != null)
284: return cachedToString;
285: StringBuffer buffer = new StringBuffer(getClass().getName());
286: buffer.append(": name=");
287: buffer.append(getName());
288: buffer.append(", openType=");
289: buffer.append(getOpenType());
290: buffer.append(", defaultValue=");
291: buffer.append(getDefaultValue());
292: buffer.append(", minValue=");
293: buffer.append(getMinValue());
294: buffer.append(", maxValue=");
295: buffer.append(getMaxValue());
296: buffer.append(", legalValues=");
297: buffer.append(getLegalValues());
298: cachedToString = buffer.toString();
299: return cachedToString;
300: }
301:
302: // Protected -----------------------------------------------------
303:
304: // Private -------------------------------------------------------
305:
306: /**
307: * Initialise an OpenMBeanParameterInfoSupport<p>
308: *
309: * WARNING: For the MBeanParameterInfo only validation is performed
310: *
311: * @param name cannot be null or empty
312: * @param description cannot be null or empty
313: * @param openType cannot be null
314: * @param defaultValue the default value
315: * @param minValue the minimum value
316: * @param maxValue the maximum value
317: * @exception IllegalArgumentException when one of the above
318: * constraints is not satisfied
319: */
320: private void init(String name, String Description,
321: OpenType openType, Object defaultValue,
322: Object[] legalValues, Comparable minValue,
323: Comparable maxValue) throws OpenDataException {
324: if (name == null || name.trim().length() == 0)
325: throw new IllegalArgumentException("null or empty name");
326:
327: if (description == null || description.trim().length() == 0)
328: throw new IllegalArgumentException(
329: "null or empty description");
330:
331: if (openType == null)
332: throw new IllegalArgumentException("null open type");
333: this .openType = openType;
334:
335: if (defaultValue != null
336: && (openType instanceof ArrayType || openType instanceof TabularType))
337: throw new OpenDataException(
338: "default value is not supported for "
339: + openType.getClass().getName());
340: if (defaultValue != null
341: && openType.isValue(defaultValue) == false)
342: throw new OpenDataException(
343: "default value is not valid for "
344: + openType.getClass().getName());
345:
346: if (legalValues != null && legalValues.length != 0) {
347: if (openType instanceof ArrayType
348: || openType instanceof TabularType)
349: throw new OpenDataException(
350: "legal values are not supported for "
351: + openType.getClass().getName());
352: HashSet legals = new HashSet(legalValues.length);
353: for (int i = 0; i < legalValues.length; i++) {
354: if (openType.isValue(legalValues[i]) == false)
355: throw new OpenDataException("legal value "
356: + legalValues[i] + " at index " + i
357: + " is not valid for "
358: + openType.getClass().getName());
359: legals.add(legalValues[i]);
360: }
361: if (defaultValue != null
362: && legals.contains(defaultValue) == false)
363: throw new OpenDataException(
364: "default value is not a legal value");
365: this .legalValues = Collections.unmodifiableSet(legals);
366: }
367:
368: if (minValue != null && openType.isValue(minValue) == false)
369: throw new OpenDataException(
370: "minimum value is not valid for "
371: + openType.getClass().getName());
372: if (defaultValue != null && minValue != null
373: && minValue.compareTo(defaultValue) > 0)
374: throw new OpenDataException(
375: "the default value is less than the minimum value ");
376:
377: if (maxValue != null && openType.isValue(maxValue) == false)
378: throw new OpenDataException(
379: "maximum value is not valid for "
380: + openType.getClass().getName());
381: if (defaultValue != null && maxValue != null
382: && maxValue.compareTo(defaultValue) < 0)
383: throw new OpenDataException(
384: "the default value is greater than the maximum value ");
385:
386: if (minValue != null && maxValue != null
387: && minValue.compareTo(maxValue) > 0)
388: throw new OpenDataException(
389: "the minimum value is greater than the maximum value ");
390:
391: this .defaultValue = defaultValue;
392: this .minValue = minValue;
393: this .maxValue = maxValue;
394: }
395:
396: // Inner Classes -------------------------------------------------
397: }
|