001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package javax.management.openmbean;
009:
010: import java.io.Serializable;
011: import java.util.SortedMap;
012: import java.util.TreeMap;
013: import java.util.Set;
014: import java.util.Arrays;
015: import java.util.Map;
016: import java.util.Iterator;
017: import java.util.Collections;
018: import java.util.Collection;
019:
020: /**
021: * The <tt>CompositeDataSupport</tt> class is the <i>open data</i> class which implements the <tt>CompositeData</tt> interface.
022: *
023: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
024: */
025:
026: public class CompositeDataSupport implements CompositeData,
027: Serializable {
028:
029: /**
030: * @serial Internal representation of the mapping of item names to their respective values.
031: * A {@link SortedMap} is used for faster retrieval of elements.
032: */
033: /**
034: * @serial Internal representation of the mapping of item names to their respective values.
035: * A {@link SortedMap} is used for faster retrieval of elements.
036: */
037: private SortedMap contents = new TreeMap();
038:
039: /**
040: * @serial The <i>composite type </i> of this <i>composite data</i> instance.
041: */
042: private CompositeType compositeType;
043:
044: /**
045: * <p>
046: * Constructs a <tt>CompositeDataSupport</tt> instance with the specified <tt>compositeType</tt>, whose item values
047: * are specified by <tt>itemValues[]</tt>, in the same order as in <tt>itemNames[]</tt>.
048: * As a <tt>CompositeType</tt> does not specify any order on its items, the <tt>itemNames[]</tt> parameter is used
049: * to specify the order in which the values are given in <tt>itemValues[]</tt>.
050: * The items contained in this <tt>CompositeDataSupport</tt> instance are internally stored in a <tt>TreeMap</tt>,
051: * thus sorted in ascending lexicographic order of their names, for faster retrieval of individual item values.
052: * <p>
053: * The constructor checks that all the constraints listed below for each parameter are satisfied,
054: * and throws the appropriate exception if they are not.
055: * <p>
056: * @param compositeType the <i>composite type </i> of this <i>composite data</i> instance;
057: * must not be null.
058: * <p>
059: * @param itemNames <tt>itemNames</tt> must list, in any order, all the item names defined in <tt>compositeType</tt>;
060: * the order in which the names are listed, is used to match values in <tt>itemValues[]</tt>;
061: * must not be null or empty.
062: * <p>
063: * @param itemValues the values of the items, listed in the same order as their respective names in <tt>itemNames</tt>;
064: * each item value can be null, but if it is non-null it must be
065: * a valid value for the open type defined in <tt>compositeType</tt> for the corresponding item;
066: * must be of the same size as <tt>itemNames</tt>; must not be null or empty.
067: * <p>
068: * @throws IllegalArgumentException <tt>compositeType</tt> is null, or <tt>itemNames[]</tt> or <tt>itemValues[]</tt> is null or empty,
069: * or one of the elements in <tt>itemNames[]</tt> is a null or empty string,
070: * or <tt>itemNames[]</tt> and <tt>itemValues[]</tt> are not of the same size.
071: * <p>
072: * @throws OpenDataException <tt>itemNames[]</tt> or <tt>itemValues[]</tt>'s size differs from
073: * the number of items defined in <tt>compositeType</tt>,
074: * or one of the elements in <tt>itemNames[]</tt> does not exist as an item name defined in <tt>compositeType</tt>,
075: * or one of the elements in <tt>itemValues[]</tt> is not a valid value for the corresponding item
076: * as defined in <tt>compositeType</tt>.
077: * <p>
078: */
079: public CompositeDataSupport(CompositeType compositeType,
080: String[] itemNames, Object[] itemValues)
081: throws OpenDataException {
082:
083: // Check compositeType is not null
084: //
085: if (compositeType == null) {
086: throw new IllegalArgumentException(
087: "Argument compositeType cannot be null.");
088: }
089:
090: // item names defined in compositeType:
091: Set namesSet = compositeType.keySet();
092:
093: // Check the array itemNames is not null or empty (length!=0) and
094: // that there is no null element or empty string in it
095: //
096: checkForNullElement(itemNames, "itemNames");
097: checkForEmptyString(itemNames, "itemNames");
098:
099: // Check the array itemValues is not null or empty (length!=0)
100: // (NOTE: we allow null values as array elements)
101: //
102: if ((itemValues == null) || (itemValues.length == 0)) {
103: throw new IllegalArgumentException(
104: "Argument itemValues[] cannot be null or empty.");
105: }
106:
107: // Check that the sizes of the 2 arrays itemNames and itemValues are the same
108: //
109: if (itemNames.length != itemValues.length) {
110: throw new IllegalArgumentException(
111: "Array arguments itemNames[] and itemValues[] "
112: + "should be of same length (got "
113: + itemNames.length + " and "
114: + itemValues.length + ").");
115: }
116:
117: // Check the size of the 2 arrays is equal to the number of items defined in compositeType
118: //
119: if (itemNames.length != namesSet.size()) {
120: throw new OpenDataException(
121: "The size of array arguments itemNames[] and itemValues[] should be equal to the number of items defined"
122: + " in argument compositeType (found "
123: + itemNames.length
124: + " elements in itemNames[] and itemValues[],"
125: + " expecting "
126: + namesSet.size()
127: + " elements according to compositeType.");
128: }
129:
130: // Check parameter itemNames[] contains all names defined in the compositeType of this instance
131: //
132: if (!Arrays.asList(itemNames).containsAll(namesSet)) {
133: throw new OpenDataException(
134: "Argument itemNames[] does not contain all names defined in the compositeType of this instance.");
135: }
136:
137: // Check each element of itemValues[], if not null, is of the open type defined for the corresponding item
138: //
139: OpenType itemType;
140: for (int i = 0; i < itemValues.length; i++) {
141: itemType = compositeType.getType(itemNames[i]);
142: if ((itemValues[i] != null)
143: && (!itemType.isValue(itemValues[i]))) {
144: throw new OpenDataException(
145: "Argument's element itemValues[" + i + "]=\""
146: + itemValues[i]
147: + "\" is not a valid value for"
148: + "this item (itemName=" + itemNames[i]
149: + ",itemType=" + itemType + ").");
150: }
151: }
152:
153: // Initialize internal fields: compositeType and contents
154: //
155: this .compositeType = compositeType;
156: for (int i = 0; i < itemNames.length; i++) {
157: this .contents.put(itemNames[i], itemValues[i]);
158: }
159: }
160:
161: /**
162: * <p>
163: * Constructs a <tt>CompositeDataSupport</tt> instance with the specified <tt>compositeType</tt>, whose item names and corresponding values
164: * are given by the mappings in the map <tt>items</tt>.
165: * This constructor converts the keys to a string array and the values to an object array and calls
166: * <tt>CompositeDataSupport(javax.management.openmbean.CompositeType, java.lang.String[], java.lang.Object[])</tt>.
167: * <p>
168: * @param compositeType the <i>composite type </i> of this <i>composite data</i> instance;
169: * must not be null.
170: * <p>
171: * @param items the mappings of all the item names to their values;
172: * <tt>items</tt> must contain all the item names defined in <tt>compositeType</tt>;
173: * must not be null or empty.
174: * <p>
175: * @throws IllegalArgumentException <tt>compositeType</tt> is null, or <tt>items</tt> is null or empty,
176: * or one of the keys in <tt>items</tt> is a null or empty string,
177: * or one of the values in <tt>items</tt> is null.
178: * <p>
179: * @throws OpenDataException <tt>items</tt>' size differs from the number of items defined in <tt>compositeType</tt>,
180: * or one of the keys in <tt>items</tt> does not exist as an item name defined in <tt>compositeType</tt>,
181: * or one of the values in <tt>items</tt> is not a valid value for the corresponding item
182: * as defined in <tt>compositeType</tt>.
183: * <p>
184: * @throws ArrayStoreException one or more keys in <tt>items</tt> is not of the class <tt>java.lang.String</tt>.
185: * <p>
186: */
187: public CompositeDataSupport(CompositeType compositeType, Map items)
188: throws OpenDataException {
189:
190: // Let the other constructor do the job, as the call to another constructor must be the first call
191: //
192: this (compositeType, (items == null ? null : (String[]) items
193: .keySet().toArray(new String[items.size()])), // may raise an ArrayStoreException
194: (items == null ? null : items.values().toArray()));
195: }
196:
197: /**
198: *
199: */
200: private static void checkForNullElement(Object[] arg, String argName) {
201: if ((arg == null) || (arg.length == 0)) {
202: throw new IllegalArgumentException("Argument " + argName
203: + "[] cannot be null or empty.");
204: }
205: for (int i = 0; i < arg.length; i++) {
206: if (arg[i] == null) {
207: throw new IllegalArgumentException(
208: "Argument's element " + argName + "[" + i
209: + "] cannot be null.");
210: }
211: }
212: }
213:
214: /**
215: *
216: */
217: private static void checkForEmptyString(String[] arg, String argName) {
218: for (int i = 0; i < arg.length; i++) {
219: if (arg[i].trim().equals("")) {
220: throw new IllegalArgumentException(
221: "Argument's element " + argName + "[" + i
222: + "] cannot be an empty string.");
223: }
224: }
225: }
226:
227: /**
228: * Returns the <i>composite type </i> of this <i>composite data</i> instance.
229: */
230: public CompositeType getCompositeType() {
231:
232: return compositeType;
233: }
234:
235: /**
236: * Returns the value of the item whose name is <tt>key</tt>.
237: *
238: * @throws IllegalArgumentException if <tt>key</tt> is a null or empty String.
239: *
240: * @throws InvalidKeyException if <tt>key</tt> is not an existing item name for this <tt>CompositeData</tt> instance.
241: */
242: public Object get(String key) {
243:
244: if ((key == null) || (key.trim().equals(""))) {
245: throw new IllegalArgumentException(
246: "Argument key cannot be a null or empty String.");
247: }
248: if (!contents.containsKey(key.trim())) {
249: throw new InvalidKeyException(
250: "Argument key=\""
251: + key.trim()
252: + "\" is not an existing item name for this CompositeData instance.");
253: }
254: return contents.get(key.trim());
255: }
256:
257: /**
258: * Returns an array of the values of the items whose names are specified by <tt>keys</tt>, in the same order as <tt>keys</tt>.
259: *
260: * @throws IllegalArgumentException if an element in <tt>keys</tt> is a null or empty String.
261: *
262: * @throws InvalidKeyException if an element in <tt>keys</tt> is not an existing item name for this <tt>CompositeData</tt> instance.
263: */
264: public Object[] getAll(String[] keys) {
265:
266: if ((keys == null) || (keys.length == 0)) {
267: return new Object[0];
268: }
269: Object[] results = new Object[keys.length];
270: for (int i = 0; i < keys.length; i++) {
271: results[i] = this .get(keys[i]);
272: }
273: return results;
274: }
275:
276: /**
277: * Returns <tt>true</tt> if and only if this <tt>CompositeData</tt> instance contains
278: * an item whose name is <tt>key</tt>.
279: * If <tt>key</tt> is a null or empty String, this method simply returns false.
280: */
281: public boolean containsKey(String key) {
282:
283: if ((key == null) || (key.trim().equals(""))) {
284: return false;
285: }
286: return contents.containsKey(key);
287: }
288:
289: /**
290: * Returns <tt>true</tt> if and only if this <tt>CompositeData</tt> instance contains an item
291: * whose value is <tt>value</tt>.
292: */
293: public boolean containsValue(Object value) {
294:
295: return contents.containsValue(value);
296: }
297:
298: /**
299: * Returns an unmodifiable Collection view of the item values contained in this <tt>CompositeData</tt> instance.
300: * The returned collection's iterator will return the values in the ascending lexicographic order of the corresponding
301: * item names.
302: */
303: public Collection values() {
304:
305: return Collections.unmodifiableCollection(contents.values());
306: }
307:
308: /**
309: * Compares the specified <var>obj</var> parameter with this <code>CompositeDataSupport</code> instance for equality.
310: * <p>
311: * Returns <tt>true</tt> if and only if all of the following statements are true:
312: * <ul>
313: * <li><var>obj</var> is non null,</li>
314: * <li><var>obj</var> also implements the <code>CompositeData</code> interface,</li>
315: * <li>their composite types are equal</li>
316: * <li>their contents, i.e. (name, value) pairs are equal. </li>
317: * </ul>
318: * This ensures that this <tt>equals</tt> method works properly for <var>obj</var> parameters which are
319: * different implementations of the <code>CompositeData</code> interface, with the restrictions mentioned in the
320: * <tt>{@link <a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Collection.html#equals(java.lang.Object)">equals</a>}</tt>
321: * method of the <tt>java.util.Collection</tt> interface.
322: * <br>
323: * @param obj the object to be compared for equality with this <code>CompositeDataSupport</code> instance;
324: *
325: * @return <code>true</code> if the specified object is equal to this <code>CompositeDataSupport</code> instance.
326: */
327: public boolean equals(Object obj) {
328:
329: // if obj is null, return false
330: //
331: if (obj == null) {
332: return false;
333: }
334:
335: // if obj is not a CompositeData, return false
336: //
337: CompositeData other;
338: try {
339: other = (CompositeData) obj;
340: } catch (ClassCastException e) {
341: return false;
342: }
343:
344: // their compositeType should be equal
345: if (!this .getCompositeType().equals(other.getCompositeType())) {
346: return false;
347: }
348:
349: // Currently this test returns false if we have different Array instances with same contents.
350: // Array objects are equals only if their references are equal, ie they are the same object!
351: // CompositeData equals() method need to be modified to compare Array contents...
352:
353: // their content, i.e. (name, value) pairs, should be equal
354: Map.Entry entry;
355: boolean ok;
356: for (Iterator iter = contents.entrySet().iterator(); iter
357: .hasNext();) {
358: entry = (Map.Entry) iter.next();
359: ok = (entry.getValue() == null ? other.get((String) entry
360: .getKey()) == null : entry.getValue().equals(
361: other.get((String) entry.getKey())));
362: if (!ok) {
363: return false;
364: }
365: }
366:
367: return true;
368: }
369:
370: /**
371: * Returns the hash code value for this <code>CompositeDataSupport</code> instance.
372: * <p>
373: * The hash code of a <code>CompositeDataSupport</code> instance is the sum of the hash codes
374: * of all elements of information used in <code>equals</code> comparisons
375: * (ie: its <i>composite type</i> and all the item values).
376: * <p>
377: * This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code>
378: * for any two <code>CompositeDataSupport</code> instances <code>t1</code> and <code>t2</code>,
379: * as required by the general contract of the method
380: * {@link <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html#hashCode()">
381: * <code>Object.hashCode</code> </a>}.
382: * <p>
383: * However, note that another instance of a class implementing the <code>CompositeData</code> interface
384: * may be equal to this <code>CompositeDataSupport</code> instance as defined by {@link #equals},
385: * but may have a different hash code if it is calculated differently.
386: *
387: * @return the hash code value for this <code>CompositeDataSupport</code> instance
388: */
389: public int hashCode() {
390:
391: int result = 0;
392: result += compositeType.hashCode();
393: Map.Entry entry;
394: for (Iterator iter = contents.entrySet().iterator(); iter
395: .hasNext();) {
396: entry = (Map.Entry) iter.next();
397: result += (entry.getValue() == null ? 0 : entry.getValue()
398: .hashCode());
399: }
400: return result;
401: }
402:
403: /**
404: * Returns a string representation of this <code>CompositeDataSupport</code> instance.
405: * <p>
406: * The string representation consists of the name of this class (ie <code>javax.management.openmbean.CompositeDataSupport</code>),
407: * the string representation of the composite type of this instance, and the string representation of the contents
408: * (ie list the itemName=itemValue mappings).
409: *
410: * @return a string representation of this <code>CompositeDataSupport</code> instance
411: */
412: public String toString() {
413: return new StringBuffer().append(this .getClass().getName())
414: .append("(compositeType=").append(
415: compositeType.toString()).append(",contents=")
416: .append(contents.toString()).append(")").toString();
417: }
418: }
|