001 /*
002 * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.management.openmbean;
027
028 // java import
029 //
030 import java.io.Serializable;
031 import java.util.Arrays;
032 import java.util.Collection;
033 import java.util.Collections;
034 import java.util.Map;
035 import java.util.Set;
036 import java.util.SortedMap;
037 import java.util.TreeMap;
038
039 // jmx import
040 //
041
042 /**
043 * The <tt>CompositeDataSupport</tt> class is the <i>open data</i> class which
044 * implements the <tt>CompositeData</tt> interface.
045 *
046 *
047 * @since 1.5
048 */
049 public class CompositeDataSupport implements CompositeData,
050 Serializable {
051
052 /* Serial version */
053 static final long serialVersionUID = 8003518976613702244L;
054
055 /**
056 * @serial Internal representation of the mapping of item names to their
057 * respective values.
058 * A {@link SortedMap} is used for faster retrieval of elements.
059 */
060 private SortedMap<String, Object> contents = new TreeMap<String, Object>();
061
062 /**
063 * @serial The <i>composite type </i> of this <i>composite data</i> instance.
064 */
065 private CompositeType compositeType;
066
067 /**
068 * <p>
069 * Constructs a <tt>CompositeDataSupport</tt> instance with the specified
070 * <tt>compositeType</tt>, whose item values
071 * are specified by <tt>itemValues[]</tt>, in the same order as in
072 * <tt>itemNames[]</tt>.
073 * As a <tt>CompositeType</tt> does not specify any order on its items,
074 * the <tt>itemNames[]</tt> parameter is used
075 * to specify the order in which the values are given in <tt>itemValues[]</tt>.
076 * The items contained in this <tt>CompositeDataSupport</tt> instance are
077 * internally stored in a <tt>TreeMap</tt>,
078 * thus sorted in ascending lexicographic order of their names, for faster
079 * retrieval of individual item values.
080 * <p>
081 * The constructor checks that all the constraints listed below for each
082 * parameter are satisfied,
083 * and throws the appropriate exception if they are not.
084 * <p>
085 * @param compositeType the <i>composite type </i> of this <i>composite
086 * data</i> instance;
087 * must not be null.
088 * <p>
089 * @param itemNames <tt>itemNames</tt> must list, in any order, all the
090 * item names defined in <tt>compositeType</tt>;
091 * the order in which the names are listed, is used to
092 * match values in <tt>itemValues[]</tt>;
093 * must not be null or empty.
094 * <p>
095 * @param itemValues the values of the items, listed in the same order as
096 * their respective names in <tt>itemNames</tt>;
097 * each item value can be null, but if it is non-null it must be
098 * a valid value for the open type defined in <tt>compositeType</tt> for the corresponding item;
099 * must be of the same size as <tt>itemNames</tt>; must not be null or empty.
100 * <p>
101 * @throws IllegalArgumentException <tt>compositeType</tt> is null, or <tt>itemNames[]</tt> or <tt>itemValues[]</tt> is null or empty,
102 * or one of the elements in <tt>itemNames[]</tt> is a null or empty string,
103 * or <tt>itemNames[]</tt> and <tt>itemValues[]</tt> are not of the same size.
104 * <p>
105 * @throws OpenDataException <tt>itemNames[]</tt> or <tt>itemValues[]</tt>'s size differs from
106 * the number of items defined in <tt>compositeType</tt>,
107 * or one of the elements in <tt>itemNames[]</tt> does not exist as an item name defined in <tt>compositeType</tt>,
108 * or one of the elements in <tt>itemValues[]</tt> is not a valid value for the corresponding item
109 * as defined in <tt>compositeType</tt>.
110 * <p>
111 */
112 public CompositeDataSupport(CompositeType compositeType,
113 String[] itemNames, Object[] itemValues)
114 throws OpenDataException {
115
116 // Check compositeType is not null
117 //
118 if (compositeType == null) {
119 throw new IllegalArgumentException(
120 "Argument compositeType cannot be null.");
121 }
122
123 // item names defined in compositeType:
124 Set<String> namesSet = compositeType.keySet();
125
126 // Check the array itemNames is not null or empty (length!=0) and
127 // that there is no null element or empty string in it
128 //
129 checkForNullElement(itemNames, "itemNames");
130 checkForEmptyString(itemNames, "itemNames");
131
132 // Check the array itemValues is not null or empty (length!=0)
133 // (NOTE: we allow null values as array elements)
134 //
135 if ((itemValues == null) || (itemValues.length == 0)) {
136 throw new IllegalArgumentException(
137 "Argument itemValues[] cannot be null or empty.");
138 }
139
140 // Check that the sizes of the 2 arrays itemNames and itemValues are the same
141 //
142 if (itemNames.length != itemValues.length) {
143 throw new IllegalArgumentException(
144 "Array arguments itemNames[] and itemValues[] "
145 + "should be of same length (got "
146 + itemNames.length + " and "
147 + itemValues.length + ").");
148 }
149
150 // Check the size of the 2 arrays is equal to the number of items defined in compositeType
151 //
152 if (itemNames.length != namesSet.size()) {
153 throw new OpenDataException(
154 "The size of array arguments itemNames[] and itemValues[] should be equal to the number of items defined"
155 + " in argument compositeType (found "
156 + itemNames.length
157 + " elements in itemNames[] and itemValues[],"
158 + " expecting "
159 + namesSet.size()
160 + " elements according to compositeType.");
161 }
162
163 // Check parameter itemNames[] contains all names defined in the compositeType of this instance
164 //
165 if (!Arrays.asList(itemNames).containsAll(namesSet)) {
166 throw new OpenDataException(
167 "Argument itemNames[] does not contain all names defined in the compositeType of this instance.");
168 }
169
170 // Check each element of itemValues[], if not null, is of the open type defined for the corresponding item
171 //
172 OpenType<?> itemType;
173 for (int i = 0; i < itemValues.length; i++) {
174 itemType = compositeType.getType(itemNames[i]);
175 if ((itemValues[i] != null)
176 && (!itemType.isValue(itemValues[i]))) {
177 throw new OpenDataException(
178 "Argument's element itemValues[" + i + "]=\""
179 + itemValues[i]
180 + "\" is not a valid value for"
181 + " this item (itemName="
182 + itemNames[i] + ",itemType="
183 + itemType + ").");
184 }
185 }
186
187 // Initialize internal fields: compositeType and contents
188 //
189 this .compositeType = compositeType;
190 for (int i = 0; i < itemNames.length; i++) {
191 this .contents.put(itemNames[i], itemValues[i]);
192 }
193 }
194
195 /**
196 * <p>
197 * Constructs a <tt>CompositeDataSupport</tt> instance with the specified <tt>compositeType</tt>, whose item names and corresponding values
198 * are given by the mappings in the map <tt>items</tt>.
199 * This constructor converts the keys to a string array and the values to an object array and calls
200 * <tt>CompositeDataSupport(javax.management.openmbean.CompositeType, java.lang.String[], java.lang.Object[])</tt>.
201 * <p>
202 * @param compositeType the <i>composite type </i> of this <i>composite data</i> instance;
203 * must not be null.
204 * <p>
205 * @param items the mappings of all the item names to their values;
206 * <tt>items</tt> must contain all the item names defined in <tt>compositeType</tt>;
207 * must not be null or empty.
208 * <p>
209 * @throws IllegalArgumentException <tt>compositeType</tt> is null, or <tt>items</tt> is null or empty,
210 * or one of the keys in <tt>items</tt> is a null or empty string,
211 * or one of the values in <tt>items</tt> is null.
212 * <p>
213 * @throws OpenDataException <tt>items</tt>' size differs from the number of items defined in <tt>compositeType</tt>,
214 * or one of the keys in <tt>items</tt> does not exist as an item name defined in <tt>compositeType</tt>,
215 * or one of the values in <tt>items</tt> is not a valid value for the corresponding item
216 * as defined in <tt>compositeType</tt>.
217 * <p>
218 * @throws ArrayStoreException one or more keys in <tt>items</tt> is not of the class <tt>java.lang.String</tt>.
219 * <p>
220 */
221 public CompositeDataSupport(CompositeType compositeType,
222 Map<String, ?> items) throws OpenDataException {
223
224 // Let the other constructor do the job, as the call to another constructor must be the first call
225 //
226 this (compositeType, (items == null ? null : items.keySet()
227 .toArray(new String[items.size()])), // may raise an ArrayStoreException
228 (items == null ? null : items.values().toArray()));
229 }
230
231 /**
232 *
233 */
234 private static void checkForNullElement(Object[] arg, String argName) {
235 if ((arg == null) || (arg.length == 0)) {
236 throw new IllegalArgumentException("Argument " + argName
237 + "[] cannot be null or empty.");
238 }
239 for (int i = 0; i < arg.length; i++) {
240 if (arg[i] == null) {
241 throw new IllegalArgumentException(
242 "Argument's element " + argName + "[" + i
243 + "] cannot be null.");
244 }
245 }
246 }
247
248 /**
249 *
250 */
251 private static void checkForEmptyString(String[] arg, String argName) {
252 for (int i = 0; i < arg.length; i++) {
253 if (arg[i].trim().equals("")) {
254 throw new IllegalArgumentException(
255 "Argument's element " + argName + "[" + i
256 + "] cannot be an empty string.");
257 }
258 }
259 }
260
261 /**
262 * Returns the <i>composite type </i> of this <i>composite data</i> instance.
263 */
264 public CompositeType getCompositeType() {
265
266 return compositeType;
267 }
268
269 /**
270 * Returns the value of the item whose name is <tt>key</tt>.
271 *
272 * @throws IllegalArgumentException if <tt>key</tt> is a null or empty String.
273 *
274 * @throws InvalidKeyException if <tt>key</tt> is not an existing item name for
275 * this <tt>CompositeData</tt> instance.
276 */
277 public Object get(String key) {
278
279 if ((key == null) || (key.trim().equals(""))) {
280 throw new IllegalArgumentException(
281 "Argument key cannot be a null or empty String.");
282 }
283 if (!contents.containsKey(key.trim())) {
284 throw new InvalidKeyException(
285 "Argument key=\""
286 + key.trim()
287 + "\" is not an existing item name for this CompositeData instance.");
288 }
289 return contents.get(key.trim());
290 }
291
292 /**
293 * Returns an array of the values of the items whose names are specified by
294 * <tt>keys</tt>, in the same order as <tt>keys</tt>.
295 *
296 * @throws IllegalArgumentException if an element in <tt>keys</tt> is a null
297 * or empty String.
298 *
299 * @throws InvalidKeyException if an element in <tt>keys</tt> is not an existing
300 * item name for this <tt>CompositeData</tt> instance.
301 */
302 public Object[] getAll(String[] keys) {
303
304 if ((keys == null) || (keys.length == 0)) {
305 return new Object[0];
306 }
307 Object[] results = new Object[keys.length];
308 for (int i = 0; i < keys.length; i++) {
309 results[i] = this .get(keys[i]);
310 }
311 return results;
312 }
313
314 /**
315 * Returns <tt>true</tt> if and only if this <tt>CompositeData</tt> instance contains
316 * an item whose name is <tt>key</tt>.
317 * If <tt>key</tt> is a null or empty String, this method simply returns false.
318 */
319 public boolean containsKey(String key) {
320
321 if ((key == null) || (key.trim().equals(""))) {
322 return false;
323 }
324 return contents.containsKey(key);
325 }
326
327 /**
328 * Returns <tt>true</tt> if and only if this <tt>CompositeData</tt> instance
329 * contains an item
330 * whose value is <tt>value</tt>.
331 */
332 public boolean containsValue(Object value) {
333
334 return contents.containsValue(value);
335 }
336
337 /**
338 * Returns an unmodifiable Collection view of the item values contained in this
339 * <tt>CompositeData</tt> instance.
340 * The returned collection's iterator will return the values in the ascending
341 * lexicographic order of the corresponding
342 * item names.
343 */
344 public Collection<?> values() {
345
346 return Collections.unmodifiableCollection(contents.values());
347 }
348
349 /**
350 * Compares the specified <var>obj</var> parameter with this
351 * <code>CompositeDataSupport</code> instance for equality.
352 * <p>
353 * Returns <tt>true</tt> if and only if all of the following statements are true:
354 * <ul>
355 * <li><var>obj</var> is non null,</li>
356 * <li><var>obj</var> also implements the <code>CompositeData</code> interface,</li>
357 * <li>their composite types are equal</li>
358 * <li>their contents, i.e. (name, value) pairs are equal. If a value contained in
359 * the content is an array, the value comparison is done as if by calling
360 * the {@link java.util.Arrays#deepEquals(Object[], Object[]) deepEquals} method
361 * for arrays of object reference types or the appropriate overloading of
362 * {@code Arrays.equals(e1,e2)} for arrays of primitive types</li>
363 * </ul>
364 * <p>
365 * This ensures that this <tt>equals</tt> method works properly for
366 * <var>obj</var> parameters which are different implementations of the
367 * <code>CompositeData</code> interface, with the restrictions mentioned in the
368 * {@link java.util.Collection#equals(Object) equals}
369 * method of the <tt>java.util.Collection</tt> interface.
370 *
371 * @param obj the object to be compared for equality with this
372 * <code>CompositeDataSupport</code> instance.
373 * @return <code>true</code> if the specified object is equal to this
374 * <code>CompositeDataSupport</code> instance.
375 */
376 public boolean equals(Object obj) {
377 if (this == obj) {
378 return true;
379 }
380
381 // if obj is not a CompositeData, return false
382 if (!(obj instanceof CompositeData)) {
383 return false;
384 }
385
386 CompositeData other = (CompositeData) obj;
387
388 // their compositeType should be equal
389 if (!this .getCompositeType().equals(other.getCompositeType())) {
390 return false;
391 }
392
393 if (contents.size() != other.values().size()) {
394 return false;
395 }
396
397 for (Map.Entry<String, Object> entry : contents.entrySet()) {
398 Object e1 = entry.getValue();
399 Object e2 = other.get(entry.getKey());
400
401 if (e1 == e2)
402 continue;
403 if (e1 == null)
404 return false;
405
406 boolean eq = e1.getClass().isArray() ? Arrays.deepEquals(
407 new Object[] { e1 }, new Object[] { e2 }) : e1
408 .equals(e2);
409
410 if (!eq)
411 return false;
412 }
413
414 // All tests for equality were successful
415 //
416 return true;
417 }
418
419 /**
420 * Returns the hash code value for this <code>CompositeDataSupport</code> instance.
421 * <p>
422 * The hash code of a <code>CompositeDataSupport</code> instance is the sum of the hash codes
423 * of all elements of information used in <code>equals</code> comparisons
424 * (ie: its <i>composite type</i> and all the item values).
425 * <p>
426 * This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code>
427 * for any two <code>CompositeDataSupport</code> instances <code>t1</code> and <code>t2</code>,
428 * as required by the general contract of the method
429 * {@link Object#hashCode() Object.hashCode()}.
430 * <p>
431 * Each item value's hash code is added to the returned hash code.
432 * If an item value is an array,
433 * its hash code is obtained as if by calling the
434 * {@link java.util.Arrays#deepHashCode(Object[]) deepHashCode} method
435 * for arrays of object reference types or the appropriate overloading
436 * of {@code Arrays.hashCode(e)} for arrays of primitive types.
437 *
438 * @return the hash code value for this <code>CompositeDataSupport</code> instance
439 */
440 public int hashCode() {
441 int hashcode = compositeType.hashCode();
442
443 for (Object o : contents.values()) {
444 if (o instanceof Object[])
445 hashcode += Arrays.deepHashCode((Object[]) o);
446 else if (o instanceof byte[])
447 hashcode += Arrays.hashCode((byte[]) o);
448 else if (o instanceof short[])
449 hashcode += Arrays.hashCode((short[]) o);
450 else if (o instanceof int[])
451 hashcode += Arrays.hashCode((int[]) o);
452 else if (o instanceof long[])
453 hashcode += Arrays.hashCode((long[]) o);
454 else if (o instanceof char[])
455 hashcode += Arrays.hashCode((char[]) o);
456 else if (o instanceof float[])
457 hashcode += Arrays.hashCode((float[]) o);
458 else if (o instanceof double[])
459 hashcode += Arrays.hashCode((double[]) o);
460 else if (o instanceof boolean[])
461 hashcode += Arrays.hashCode((boolean[]) o);
462 else if (o != null)
463 hashcode += o.hashCode();
464 }
465
466 return hashcode;
467 }
468
469 /**
470 * Returns a string representation of this <code>CompositeDataSupport</code> instance.
471 * <p>
472 * The string representation consists of the name of this class (ie <code>javax.management.openmbean.CompositeDataSupport</code>),
473 * the string representation of the composite type of this instance, and the string representation of the contents
474 * (ie list the itemName=itemValue mappings).
475 *
476 * @return a string representation of this <code>CompositeDataSupport</code> instance
477 */
478 public String toString() {
479
480 return new StringBuilder().append(this .getClass().getName())
481 .append("(compositeType=").append(
482 compositeType.toString()).append(",contents=")
483 .append(contents.toString()).append(")").toString();
484 }
485
486 }
|