001: /**
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */package javax.management.openmbean;
008:
009: import java.io.Serializable;
010: import java.util.Collections;
011: import java.util.HashSet;
012: import java.util.Set;
013: import javax.management.MBeanParameterInfo;
014:
015: /**
016: * @version $Revision: 1.9 $
017: */
018: public class OpenMBeanParameterInfoSupport extends MBeanParameterInfo
019: implements OpenMBeanParameterInfo, Serializable {
020: private static final long serialVersionUID = -7235016873758443122L;
021:
022: private OpenType openType = null;
023: private Object defaultValue = null;
024: private Set legalValues = null;
025: private Comparable minValue = null;
026: private Comparable maxValue = null;
027:
028: private transient int m_hashcode = 0;
029:
030: public OpenMBeanParameterInfoSupport(String name,
031: String description, OpenType openType) {
032: super (name, openType == null ? "" : openType.getClassName(),
033: description);
034: if (name == null || name.trim().length() == 0)
035: throw new IllegalArgumentException(
036: "name parameter cannot be null or an empty string.");
037: if (description == null || description.trim().length() == 0)
038: throw new IllegalArgumentException(
039: "description parameter cannot be null or an empty string.");
040: if (openType == null)
041: throw new IllegalArgumentException(
042: "OpenType parameter cannot be null.");
043: this .openType = openType;
044: }
045:
046: public OpenMBeanParameterInfoSupport(String name,
047: String description, OpenType openType, Object defaultValue)
048: throws OpenDataException {
049: this (name, description, openType);
050: if (defaultValue != null) {
051: if (openType.isArray() || openType instanceof TabularType)
052: throw new OpenDataException(
053: "openType should not be an ArrayType or a TabularType when a default value is required.");
054: if (!(openType.isValue(defaultValue)))
055: throw new OpenDataException(
056: "defaultValue class name "
057: + defaultValue.getClass().getName()
058: + " does not match the one defined in openType.");
059: this .defaultValue = defaultValue;
060: }
061: }
062:
063: public OpenMBeanParameterInfoSupport(String name,
064: String description, OpenType openType, Object defaultValue,
065: Object[] legalValues) throws OpenDataException {
066: this (name, description, openType, defaultValue);
067: if (legalValues != null && legalValues.length > 0) {
068: if (openType.isArray() || openType instanceof TabularType)
069: throw new OpenDataException(
070: "legalValues not supported if openType is an ArrayType or an instanceof TabularType");
071: for (int i = 0; i < legalValues.length; i++) {
072: if (!(openType.isValue(legalValues[i])))
073: throw new OpenDataException("The element at index "
074: + i + " of type " + legalValues[i]
075: + " is not an value specified in openType.");
076: }
077: // all checked assign Object[] legalValues to set legalValues
078: assignLegalValues(legalValues);
079: if (hasDefaultValue() && hasLegalValues()
080: && !(this .legalValues.contains(defaultValue)))
081: throw new OpenDataException(
082: "LegalValues must contain the defaultValue");
083: }
084: }
085:
086: public OpenMBeanParameterInfoSupport(String name,
087: String description, OpenType openType, Object defaultValue,
088: Comparable minValue, Comparable maxValue)
089: throws OpenDataException {
090: this (name, description, openType, defaultValue);
091: if (minValue != null) {
092: /** test is a valid value for the specified openType */
093: if (!(openType.isValue(minValue)))
094: throw new OpenDataException("Comparable value of "
095: + minValue.getClass().getName()
096: + " does not match the openType value of "
097: + openType.getClassName());
098: this .minValue = minValue;
099: }
100: if (maxValue != null) {
101: if (!(openType.isValue(maxValue)))
102: throw new OpenDataException("Comparable value of "
103: + maxValue.getClass().getName()
104: + " does not match the openType value of "
105: + openType.getClassName());
106: this .maxValue = maxValue;
107: }
108: if (hasMinValue() && hasMaxValue()
109: && minValue.compareTo(maxValue) > 0)
110: throw new OpenDataException(
111: "minValue cannot be greater than maxValue.");
112: if (hasDefaultValue() && hasMinValue()
113: && minValue.compareTo(defaultValue) > 0)
114: throw new OpenDataException(
115: "minValue cannot be greater than defaultValue.");
116: if (hasDefaultValue() && hasMaxValue()
117: && ((Comparable) defaultValue).compareTo(maxValue) > 0)
118: throw new OpenDataException(
119: "defaultValue cannot be greater than maxValue.");
120: }
121:
122: /**
123: * Assigns the validated Object[] into the set legal values as the constructor states
124: * this is unmodifiable will create an unmodifiable set
125: */
126: private void assignLegalValues(Object[] legalValues) {
127: HashSet modifiableSet = new HashSet();
128: for (int i = 0; i < legalValues.length; i++) {
129: modifiableSet.add(legalValues[i]);
130: }
131: this .legalValues = Collections.unmodifiableSet(modifiableSet);
132: }
133:
134: public OpenType getOpenType() {
135: return openType;
136: }
137:
138: public Object getDefaultValue() {
139: return defaultValue;
140: }
141:
142: public Set getLegalValues() {
143: return legalValues;
144: }
145:
146: public Comparable getMinValue() {
147: return minValue;
148: }
149:
150: public Comparable getMaxValue() {
151: return maxValue;
152: }
153:
154: public boolean hasDefaultValue() {
155: return defaultValue != null;
156: }
157:
158: public boolean hasLegalValues() {
159: return legalValues != null;
160: }
161:
162: public boolean hasMinValue() {
163: return minValue != null;
164: }
165:
166: public boolean hasMaxValue() {
167: return maxValue != null;
168: }
169:
170: public boolean isValue(Object obj) {
171: // if any object is null and they have a defaultValue then isValue is true for anything else null obj returns false (must be first test) as the rest will return false for the null object
172: if (hasDefaultValue() && obj == null)
173: return true;
174:
175: if (!(openType.isValue(obj)))
176: return false;
177: if (hasLegalValues() && (!(legalValues.contains(obj))))
178: return false;
179: if (hasMinValue() && minValue.compareTo(obj) > 0)
180: return false;
181: if (hasMaxValue() && maxValue.compareTo(obj) < 0)
182: return false;
183: return true;
184: }
185:
186: public boolean equals(Object obj) {
187: if (obj == this )
188: return true;
189: if (obj == null)
190: return false;
191: if (!(obj instanceof OpenMBeanParameterInfo))
192: return false;
193:
194: OpenMBeanParameterInfo paramObj = (OpenMBeanParameterInfo) obj;
195:
196: if (!getName().equals(paramObj.getName()))
197: return false;
198: if (!getOpenType().equals(paramObj.getOpenType()))
199: return false;
200:
201: if (hasDefaultValue()
202: && (!getDefaultValue().equals(
203: paramObj.getDefaultValue())))
204: return false;
205: if (!hasDefaultValue() && paramObj.hasDefaultValue())
206: return false;
207:
208: if (hasMinValue()
209: && !(getMinValue().equals(paramObj.getMinValue())))
210: return false;
211: if (!hasMinValue() && paramObj.hasMinValue())
212: return false;
213:
214: if (hasMaxValue()
215: && !(getMaxValue().equals(paramObj.getMaxValue())))
216: return false;
217: if (!hasMaxValue() && paramObj.hasMaxValue())
218: return false;
219:
220: if (hasLegalValues()
221: && !(getLegalValues().equals(paramObj.getLegalValues())))
222: return false;
223: if (!hasLegalValues() && paramObj.hasLegalValues())
224: return false;
225:
226: return true;
227: }
228:
229: public int hashCode() {
230: if (m_hashcode == 0) {
231: int result = getName().hashCode();
232: result += getOpenType().hashCode();
233: result += (hasDefaultValue() == false) ? 0
234: : getDefaultValue().hashCode();
235: result += (hasLegalValues() == false) ? 0
236: : getLegalValues().hashCode();
237: result += (hasMinValue() == false) ? 0 : getMinValue()
238: .hashCode();
239: result += (hasMaxValue() == false) ? 0 : getMaxValue()
240: .hashCode();
241: m_hashcode = result;
242: }
243: return m_hashcode;
244: }
245:
246: public String toString() {
247: StringBuffer buf = new StringBuffer(getClass().getName());
248: buf.append("\t(name = ");
249: buf.append(getName());
250: buf.append("\topenType = ");
251: buf.append(openType.toString());
252: buf.append("\tdefault value = ");
253: buf.append(String.valueOf(defaultValue));
254: buf.append("\tmin value = ");
255: buf.append(String.valueOf(minValue));
256: buf.append("\tmax value = ");
257: buf.append(String.valueOf(maxValue));
258: buf.append("\tlegal values = ");
259: buf.append(String.valueOf(legalValues));
260: buf.append(")");
261: return buf.toString();
262: }
263: }
|