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.MBeanAttributeInfo;
014:
015: /**
016: * @version $Revision: 1.14 $
017: */
018: public class OpenMBeanAttributeInfoSupport extends MBeanAttributeInfo
019: implements OpenMBeanAttributeInfo, Serializable {
020: private static final long serialVersionUID = -4867215622149721849L;
021:
022: private OpenType openType;
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 hashCode = 0;
029: private transient String toStringName = null;
030:
031: public OpenMBeanAttributeInfoSupport(String name,
032: String description, OpenType openType, boolean isReadable,
033: boolean isWritable, boolean isIs) {
034: super (name, openType == null ? "" : openType.getClassName(),
035: description, isReadable, isWritable, isIs);
036: if (openType == null)
037: throw new IllegalArgumentException("OpenType can't be null");
038: if (name == null || name.length() == 0
039: || name.trim().length() == 0)
040: throw new IllegalArgumentException(
041: "name can't be null or empty");
042: if (description == null || description.length() == 0
043: || description.trim().length() == 0)
044: throw new IllegalArgumentException(
045: "description can't be null or empty");
046:
047: this .openType = openType;
048: }
049:
050: public OpenMBeanAttributeInfoSupport(String name,
051: String description, OpenType openType, boolean isReadable,
052: boolean isWritable, boolean isIs, Object defaultValue)
053: throws OpenDataException {
054: this (name, description, openType, isReadable, isWritable, isIs);
055:
056: if (openType instanceof ArrayType
057: || openType instanceof TabularType) {
058: if (defaultValue != null)
059: throw new OpenDataException(
060: "defaultValue is not supported for ArrayType and TabularType. Should be null");
061: }
062:
063: if (defaultValue != null && !openType.isValue(defaultValue))
064: throw new OpenDataException(
065: "defaultValue is not a valid value for the given OpenType");
066:
067: this .defaultValue = defaultValue;
068: }
069:
070: public OpenMBeanAttributeInfoSupport(String name,
071: String description, OpenType openType, boolean isReadable,
072: boolean isWritable, boolean isIs, Object defaultValue,
073: Object[] legalValues) throws OpenDataException {
074: this (name, description, openType, isReadable, isWritable, isIs,
075: defaultValue);
076:
077: if (openType instanceof ArrayType
078: || openType instanceof TabularType) {
079: if (legalValues != null && legalValues.length > 0)
080: throw new OpenDataException(
081: "legalValues isn't allowed for ArrayType and TabularType. Should be null or empty array");
082: } else if (legalValues != null && legalValues.length > 0) {
083: Set tmpSet = new HashSet(legalValues.length);
084:
085: for (int i = 0; i < legalValues.length; i++) {
086: Object lv = legalValues[i];
087: if (openType.isValue(lv)) {
088: tmpSet.add(lv);
089: } else {
090: throw new OpenDataException(
091: "An Entry in the set of legalValues is not a valid value for the given opentype");
092: }
093: }
094:
095: if (defaultValue != null && !tmpSet.contains(defaultValue)) {
096: throw new OpenDataException(
097: "The legal value set must include the default value");
098: }
099:
100: this .legalValues = Collections.unmodifiableSet(tmpSet);
101: }
102: }
103:
104: public OpenMBeanAttributeInfoSupport(String name,
105: String description, OpenType openType, boolean isReadable,
106: boolean isWritable, boolean isIs, Object defaultValue,
107: Comparable minValue, Comparable maxValue)
108: throws OpenDataException {
109: this (name, description, openType, isReadable, isWritable, isIs,
110: defaultValue);
111:
112: if (minValue != null)
113: if (!openType.isValue(minValue))
114: throw new OpenDataException(
115: "minValue is not a valid value for the specified openType");
116:
117: if (maxValue != null)
118: if (!openType.isValue(maxValue))
119: throw new OpenDataException(
120: "maxValue is not a valid value for the specified openType");
121:
122: if (minValue != null && maxValue != null)
123: if (minValue.compareTo(maxValue) > 0)
124: throw new OpenDataException(
125: "minValue and/or maxValue is "
126: + "invalid: minValue is greater than maxValue");
127: if (defaultValue != null && minValue != null)
128: if (minValue.compareTo(defaultValue) > 0)
129: throw new OpenDataException(
130: "defaultvalue and/or minValue is invalid: minValue is greater than defaultValue");
131:
132: if (defaultValue != null && maxValue != null)
133: if (((Comparable) defaultValue).compareTo(maxValue) > 0)
134: throw new OpenDataException(
135: "defaultvalue and/or maxValue is invalid: defaultValue is greater than maxValue");
136:
137: this .minValue = minValue;
138: this .maxValue = maxValue;
139: }
140:
141: public OpenType getOpenType() {
142: return openType;
143: }
144:
145: public Object getDefaultValue() {
146: return defaultValue;
147: }
148:
149: public Set getLegalValues() {
150: return legalValues;
151: }
152:
153: public Comparable getMinValue() {
154: return minValue;
155: }
156:
157: public Comparable getMaxValue() {
158: return maxValue;
159: }
160:
161: public boolean hasDefaultValue() {
162:
163: return defaultValue != null;
164: }
165:
166: public boolean hasLegalValues() {
167: return legalValues != null;
168: }
169:
170: public boolean hasMinValue() {
171: return minValue != null;
172: }
173:
174: public boolean hasMaxValue() {
175: return maxValue != null;
176: }
177:
178: public boolean isValue(Object obj) {
179: if (defaultValue != null) {
180: if (openType.isValue(obj))
181: return true;
182: } else {
183: if (obj == null)
184: return true;
185: }
186:
187: return false;
188: }
189:
190: public boolean equals(Object obj) {
191: if (obj == this )
192: return true;
193: // obj should not be null
194: if (obj == null)
195: return false;
196: // obj should implement OpenMBeanAttributeInfo
197: if (!(obj instanceof OpenMBeanAttributeInfo))
198: return false;
199:
200: OpenMBeanAttributeInfo other = (OpenMBeanAttributeInfo) obj;
201: if (!getName().equals(other.getName()))
202: return false;
203: if (!getOpenType().equals(other.getOpenType()))
204: return false;
205: if (isReadable() != other.isReadable())
206: return false;
207: if (isWritable() != other.isWritable())
208: return false;
209: if (isIs() != other.isIs())
210: return false;
211:
212: if (hasDefaultValue()) {
213: if (!getDefaultValue().equals(other.getDefaultValue()))
214: return false;
215: } else {
216: if (other.hasDefaultValue())
217: return false;
218: }
219:
220: if (hasMinValue()) {
221: if (!getMinValue().equals(other.getMinValue()))
222: return false;
223: } else {
224: if (other.hasMinValue())
225: return false;
226: }
227:
228: if (hasMaxValue()) {
229: if (!getMaxValue().equals(other.getMaxValue()))
230: return false;
231: } else {
232: if (other.hasMaxValue())
233: return false;
234: }
235:
236: if (hasLegalValues()) {
237: if (!getLegalValues().equals(other.getLegalValues()))
238: return false;
239: } else {
240: if (other.hasLegalValues())
241: return false;
242: }
243:
244: return true;
245: }
246:
247: public int hashCode() {
248: if (hashCode == 0) {
249: int result = getName().hashCode();
250: result += getOpenType().hashCode();
251: result += (hasDefaultValue() == false) ? 0
252: : getDefaultValue().hashCode();
253: result += (hasLegalValues() == false) ? 0
254: : getLegalValues().hashCode();
255: result += (hasMinValue() == false) ? 0 : getMinValue()
256: .hashCode();
257: result += (hasMaxValue() == false) ? 0 : getMaxValue()
258: .hashCode();
259: hashCode = result;
260: }
261: return hashCode;
262: }
263:
264: public String toString() {
265: if (toStringName == null) {
266: StringBuffer sb = new StringBuffer(getClass().getName());
267: sb.append("(name=");
268: sb.append(getName());
269: sb.append(", opentype=");
270: sb.append(openType.toString());
271: sb.append(", defaultValue=");
272: sb.append(hasDefaultValue() ? getDefaultValue().toString()
273: : "null");
274: sb.append(", minValue=");
275: sb
276: .append(hasMinValue() ? getMinValue().toString()
277: : "null");
278: sb.append(", maxValue=");
279: sb
280: .append(hasMaxValue() ? getMaxValue().toString()
281: : "null");
282: sb.append(", legalValues=");
283: sb.append(hasLegalValues() ? getLegalValues().toString()
284: : "null");
285: sb.append(")");
286: toStringName = sb.toString();
287: }
288: return toStringName;
289: }
290: }
|