|
1 /* |
|
2 * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 |
|
27 package javax.management.openmbean; |
|
28 |
|
29 |
|
30 // java import |
|
31 // |
|
32 import java.io.Serializable; |
|
33 import java.util.Arrays; |
|
34 import java.util.Collection; |
|
35 import java.util.Collections; |
|
36 import java.util.LinkedHashMap; |
|
37 import java.util.Map; |
|
38 import java.util.Set; |
|
39 import java.util.SortedMap; |
|
40 import java.util.TreeMap; |
|
41 |
|
42 // jmx import |
|
43 import java.util.TreeSet; |
|
44 // |
|
45 |
|
46 |
|
47 /** |
|
48 * The {@code CompositeDataSupport} class is the <i>open data</i> class which |
|
49 * implements the {@code CompositeData} interface. |
|
50 * |
|
51 * |
|
52 * @since 1.5 |
|
53 */ |
|
54 public class CompositeDataSupport |
|
55 implements CompositeData, Serializable { |
|
56 |
|
57 /* Serial version */ |
|
58 static final long serialVersionUID = 8003518976613702244L; |
|
59 |
|
60 /** |
|
61 * @serial Internal representation of the mapping of item names to their |
|
62 * respective values. |
|
63 * A {@link SortedMap} is used for faster retrieval of elements. |
|
64 */ |
|
65 private final SortedMap<String, Object> contents; |
|
66 |
|
67 /** |
|
68 * @serial The <i>composite type </i> of this <i>composite data</i> instance. |
|
69 */ |
|
70 private final CompositeType compositeType; |
|
71 |
|
72 /** |
|
73 * <p>Constructs a {@code CompositeDataSupport} instance with the specified |
|
74 * {@code compositeType}, whose item values |
|
75 * are specified by {@code itemValues[]}, in the same order as in |
|
76 * {@code itemNames[]}. |
|
77 * As a {@code CompositeType} does not specify any order on its items, |
|
78 * the {@code itemNames[]} parameter is used |
|
79 * to specify the order in which the values are given in {@code itemValues[]}. |
|
80 * The items contained in this {@code CompositeDataSupport} instance are |
|
81 * internally stored in a {@code TreeMap}, |
|
82 * thus sorted in ascending lexicographic order of their names, for faster |
|
83 * retrieval of individual item values.</p> |
|
84 * |
|
85 * <p>The constructor checks that all the constraints listed below for each |
|
86 * parameter are satisfied, |
|
87 * and throws the appropriate exception if they are not.</p> |
|
88 * |
|
89 * @param compositeType the <i>composite type </i> of this <i>composite |
|
90 * data</i> instance; must not be null. |
|
91 * |
|
92 * @param itemNames {@code itemNames} must list, in any order, all the |
|
93 * item names defined in {@code compositeType}; the order in which the |
|
94 * names are listed, is used to match values in {@code itemValues[]}; must |
|
95 * not be null or empty. |
|
96 * |
|
97 * @param itemValues the values of the items, listed in the same order as |
|
98 * their respective names in {@code itemNames}; each item value can be |
|
99 * null, but if it is non-null it must be a valid value for the open type |
|
100 * defined in {@code compositeType} for the corresponding item; must be of |
|
101 * the same size as {@code itemNames}; must not be null or empty. |
|
102 * |
|
103 * @throws IllegalArgumentException {@code compositeType} is null, or |
|
104 * {@code itemNames[]} or {@code itemValues[]} is null or empty, or one |
|
105 * of the elements in {@code itemNames[]} is a null or empty string, or |
|
106 * {@code itemNames[]} and {@code itemValues[]} are not of the same size. |
|
107 * |
|
108 * @throws OpenDataException {@code itemNames[]} or |
|
109 * {@code itemValues[]}'s size differs from the number of items defined in |
|
110 * {@code compositeType}, or one of the elements in {@code itemNames[]} |
|
111 * does not exist as an item name defined in {@code compositeType}, or one |
|
112 * of the elements in {@code itemValues[]} is not a valid value for the |
|
113 * corresponding item as defined in {@code compositeType}. |
|
114 */ |
|
115 public CompositeDataSupport( |
|
116 CompositeType compositeType, String[] itemNames, Object[] itemValues) |
|
117 throws OpenDataException { |
|
118 this(makeMap(itemNames, itemValues), compositeType); |
|
119 } |
|
120 |
|
121 private static SortedMap<String, Object> makeMap( |
|
122 String[] itemNames, Object[] itemValues) |
|
123 throws OpenDataException { |
|
124 |
|
125 if (itemNames == null || itemValues == null) |
|
126 throw new IllegalArgumentException("Null itemNames or itemValues"); |
|
127 if (itemNames.length == 0 || itemValues.length == 0) |
|
128 throw new IllegalArgumentException("Empty itemNames or itemValues"); |
|
129 if (itemNames.length != itemValues.length) { |
|
130 throw new IllegalArgumentException( |
|
131 "Different lengths: itemNames[" + itemNames.length + |
|
132 "], itemValues[" + itemValues.length + "]"); |
|
133 } |
|
134 |
|
135 SortedMap<String, Object> map = new TreeMap<String, Object>(); |
|
136 for (int i = 0; i < itemNames.length; i++) { |
|
137 String name = itemNames[i]; |
|
138 if (name == null || name.equals("")) |
|
139 throw new IllegalArgumentException("Null or empty item name"); |
|
140 if (map.containsKey(name)) |
|
141 throw new OpenDataException("Duplicate item name " + name); |
|
142 map.put(itemNames[i], itemValues[i]); |
|
143 } |
|
144 |
|
145 return map; |
|
146 } |
|
147 |
|
148 /** |
|
149 * <p> |
|
150 * Constructs a {@code CompositeDataSupport} instance with the specified {@code compositeType}, |
|
151 * whose item names and corresponding values |
|
152 * are given by the mappings in the map {@code items}. |
|
153 * This constructor converts the keys to a string array and the values to an object array and calls |
|
154 * {@code CompositeDataSupport(javax.management.openmbean.CompositeType, java.lang.String[], java.lang.Object[])}. |
|
155 * |
|
156 * @param compositeType the <i>composite type </i> of this <i>composite data</i> instance; |
|
157 * must not be null. |
|
158 * @param items the mappings of all the item names to their values; |
|
159 * {@code items} must contain all the item names defined in {@code compositeType}; |
|
160 * must not be null or empty. |
|
161 * |
|
162 * @throws IllegalArgumentException {@code compositeType} is null, or |
|
163 * {@code items} is null or empty, or one of the keys in {@code items} is a null |
|
164 * or empty string. |
|
165 * @throws OpenDataException {@code items}' size differs from the |
|
166 * number of items defined in {@code compositeType}, or one of the |
|
167 * keys in {@code items} does not exist as an item name defined in |
|
168 * {@code compositeType}, or one of the values in {@code items} |
|
169 * is not a valid value for the corresponding item as defined in |
|
170 * {@code compositeType}. |
|
171 * @throws ArrayStoreException one or more keys in {@code items} is not of |
|
172 * the class {@code java.lang.String}. |
|
173 */ |
|
174 public CompositeDataSupport(CompositeType compositeType, |
|
175 Map<String,?> items) |
|
176 throws OpenDataException { |
|
177 this(makeMap(items), compositeType); |
|
178 } |
|
179 |
|
180 private static SortedMap<String, Object> makeMap(Map<String, ?> items) { |
|
181 if (items == null || items.isEmpty()) |
|
182 throw new IllegalArgumentException("Null or empty items map"); |
|
183 |
|
184 SortedMap<String, Object> map = new TreeMap<String, Object>(); |
|
185 for (Object key : items.keySet()) { |
|
186 if (key == null || key.equals("")) |
|
187 throw new IllegalArgumentException("Null or empty item name"); |
|
188 if (!(key instanceof String)) { |
|
189 throw new ArrayStoreException("Item name is not string: " + key); |
|
190 // This can happen because of erasure. The particular |
|
191 // exception is a historical artifact - an implementation |
|
192 // detail that leaked into the API. |
|
193 } |
|
194 map.put((String) key, items.get(key)); |
|
195 } |
|
196 return map; |
|
197 } |
|
198 |
|
199 private CompositeDataSupport( |
|
200 SortedMap<String, Object> items, CompositeType compositeType) |
|
201 throws OpenDataException { |
|
202 |
|
203 // Check compositeType is not null |
|
204 // |
|
205 if (compositeType == null) { |
|
206 throw new IllegalArgumentException("Argument compositeType cannot be null."); |
|
207 } |
|
208 |
|
209 // item names defined in compositeType: |
|
210 Set<String> namesFromType = compositeType.keySet(); |
|
211 Set<String> namesFromItems = items.keySet(); |
|
212 |
|
213 // This is just a comparison, but we do it this way for a better |
|
214 // exception message. |
|
215 if (!namesFromType.equals(namesFromItems)) { |
|
216 Set<String> extraFromType = new TreeSet<String>(namesFromType); |
|
217 extraFromType.removeAll(namesFromItems); |
|
218 Set<String> extraFromItems = new TreeSet<String>(namesFromItems); |
|
219 extraFromItems.removeAll(namesFromType); |
|
220 if (!extraFromType.isEmpty() || !extraFromItems.isEmpty()) { |
|
221 throw new OpenDataException( |
|
222 "Item names do not match CompositeType: " + |
|
223 "names in items but not in CompositeType: " + extraFromItems + |
|
224 "; names in CompositeType but not in items: " + extraFromType); |
|
225 } |
|
226 } |
|
227 |
|
228 // Check each value, if not null, is of the open type defined for the |
|
229 // corresponding item |
|
230 for (String name : namesFromType) { |
|
231 Object value = items.get(name); |
|
232 if (value != null) { |
|
233 OpenType<?> itemType = compositeType.getType(name); |
|
234 if (!itemType.isValue(value)) { |
|
235 throw new OpenDataException( |
|
236 "Argument value of wrong type for item " + name + |
|
237 ": value " + value + ", type " + itemType); |
|
238 } |
|
239 } |
|
240 } |
|
241 |
|
242 // Initialize internal fields: compositeType and contents |
|
243 // |
|
244 this.compositeType = compositeType; |
|
245 this.contents = items; |
|
246 } |
|
247 |
|
248 /** |
|
249 * Returns the <i>composite type </i> of this <i>composite data</i> instance. |
|
250 */ |
|
251 public CompositeType getCompositeType() { |
|
252 |
|
253 return compositeType; |
|
254 } |
|
255 |
|
256 /** |
|
257 * Returns the value of the item whose name is {@code key}. |
|
258 * |
|
259 * @throws IllegalArgumentException if {@code key} is a null or empty String. |
|
260 * |
|
261 * @throws InvalidKeyException if {@code key} is not an existing item name for |
|
262 * this {@code CompositeData} instance. |
|
263 */ |
|
264 public Object get(String key) { |
|
265 |
|
266 if ( (key == null) || (key.trim().equals("")) ) { |
|
267 throw new IllegalArgumentException("Argument key cannot be a null or empty String."); |
|
268 } |
|
269 if ( ! contents.containsKey(key.trim())) { |
|
270 throw new InvalidKeyException("Argument key=\""+ key.trim() +"\" is not an existing item name for this CompositeData instance."); |
|
271 } |
|
272 return contents.get(key.trim()); |
|
273 } |
|
274 |
|
275 /** |
|
276 * Returns an array of the values of the items whose names are specified by |
|
277 * {@code keys}, in the same order as {@code keys}. |
|
278 * |
|
279 * @throws IllegalArgumentException if an element in {@code keys} is a null |
|
280 * or empty String. |
|
281 * |
|
282 * @throws InvalidKeyException if an element in {@code keys} is not an existing |
|
283 * item name for this {@code CompositeData} instance. |
|
284 */ |
|
285 public Object[] getAll(String[] keys) { |
|
286 |
|
287 if ( (keys == null) || (keys.length == 0) ) { |
|
288 return new Object[0]; |
|
289 } |
|
290 Object[] results = new Object[keys.length]; |
|
291 for (int i=0; i<keys.length; i++) { |
|
292 results[i] = this.get(keys[i]); |
|
293 } |
|
294 return results; |
|
295 } |
|
296 |
|
297 /** |
|
298 * Returns {@code true} if and only if this {@code CompositeData} instance contains |
|
299 * an item whose name is {@code key}. |
|
300 * If {@code key} is a null or empty String, this method simply returns false. |
|
301 */ |
|
302 public boolean containsKey(String key) { |
|
303 |
|
304 if ( (key == null) || (key.trim().equals("")) ) { |
|
305 return false; |
|
306 } |
|
307 return contents.containsKey(key); |
|
308 } |
|
309 |
|
310 /** |
|
311 * Returns {@code true} if and only if this {@code CompositeData} instance |
|
312 * contains an item |
|
313 * whose value is {@code value}. |
|
314 */ |
|
315 public boolean containsValue(Object value) { |
|
316 |
|
317 return contents.containsValue(value); |
|
318 } |
|
319 |
|
320 /** |
|
321 * Returns an unmodifiable Collection view of the item values contained in this |
|
322 * {@code CompositeData} instance. |
|
323 * The returned collection's iterator will return the values in the ascending |
|
324 * lexicographic order of the corresponding |
|
325 * item names. |
|
326 */ |
|
327 public Collection<?> values() { |
|
328 |
|
329 return Collections.unmodifiableCollection(contents.values()); |
|
330 } |
|
331 |
|
332 /** |
|
333 * Compares the specified <var>obj</var> parameter with this |
|
334 * <code>CompositeDataSupport</code> instance for equality. |
|
335 * <p> |
|
336 * Returns {@code true} if and only if all of the following statements are true: |
|
337 * <ul> |
|
338 * <li><var>obj</var> is non null,</li> |
|
339 * <li><var>obj</var> also implements the <code>CompositeData</code> interface,</li> |
|
340 * <li>their composite types are equal</li> |
|
341 * <li>their contents, i.e. (name, value) pairs are equal. If a value contained in |
|
342 * the content is an array, the value comparison is done as if by calling |
|
343 * the {@link java.util.Arrays#deepEquals(Object[], Object[]) deepEquals} method |
|
344 * for arrays of object reference types or the appropriate overloading of |
|
345 * {@code Arrays.equals(e1,e2)} for arrays of primitive types</li> |
|
346 * </ul> |
|
347 * <p> |
|
348 * This ensures that this {@code equals} method works properly for |
|
349 * <var>obj</var> parameters which are different implementations of the |
|
350 * <code>CompositeData</code> interface, with the restrictions mentioned in the |
|
351 * {@link java.util.Collection#equals(Object) equals} |
|
352 * method of the {@code java.util.Collection} interface. |
|
353 * |
|
354 * @param obj the object to be compared for equality with this |
|
355 * <code>CompositeDataSupport</code> instance. |
|
356 * @return <code>true</code> if the specified object is equal to this |
|
357 * <code>CompositeDataSupport</code> instance. |
|
358 */ |
|
359 @Override |
|
360 public boolean equals(Object obj) { |
|
361 if (this == obj) { |
|
362 return true; |
|
363 } |
|
364 |
|
365 // if obj is not a CompositeData, return false |
|
366 if (!(obj instanceof CompositeData)) { |
|
367 return false; |
|
368 } |
|
369 |
|
370 CompositeData other = (CompositeData) obj; |
|
371 |
|
372 // their compositeType should be equal |
|
373 if (!this.getCompositeType().equals(other.getCompositeType()) ) { |
|
374 return false; |
|
375 } |
|
376 |
|
377 if (contents.size() != other.values().size()) { |
|
378 return false; |
|
379 } |
|
380 |
|
381 for (Map.Entry<String,Object> entry : contents.entrySet()) { |
|
382 Object e1 = entry.getValue(); |
|
383 Object e2 = other.get(entry.getKey()); |
|
384 |
|
385 if (e1 == e2) |
|
386 continue; |
|
387 if (e1 == null) |
|
388 return false; |
|
389 |
|
390 boolean eq = e1.getClass().isArray() ? |
|
391 Arrays.deepEquals(new Object[] {e1}, new Object[] {e2}) : |
|
392 e1.equals(e2); |
|
393 |
|
394 if (!eq) |
|
395 return false; |
|
396 } |
|
397 |
|
398 // All tests for equality were successful |
|
399 // |
|
400 return true; |
|
401 } |
|
402 |
|
403 /** |
|
404 * Returns the hash code value for this <code>CompositeDataSupport</code> instance. |
|
405 * <p> |
|
406 * The hash code of a <code>CompositeDataSupport</code> instance is the sum of the hash codes |
|
407 * of all elements of information used in <code>equals</code> comparisons |
|
408 * (ie: its <i>composite type</i> and all the item values). |
|
409 * <p> |
|
410 * This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code> |
|
411 * for any two <code>CompositeDataSupport</code> instances <code>t1</code> and <code>t2</code>, |
|
412 * as required by the general contract of the method |
|
413 * {@link Object#hashCode() Object.hashCode()}. |
|
414 * <p> |
|
415 * Each item value's hash code is added to the returned hash code. |
|
416 * If an item value is an array, |
|
417 * its hash code is obtained as if by calling the |
|
418 * {@link java.util.Arrays#deepHashCode(Object[]) deepHashCode} method |
|
419 * for arrays of object reference types or the appropriate overloading |
|
420 * of {@code Arrays.hashCode(e)} for arrays of primitive types. |
|
421 * |
|
422 * @return the hash code value for this <code>CompositeDataSupport</code> instance |
|
423 */ |
|
424 @Override |
|
425 public int hashCode() { |
|
426 int hashcode = compositeType.hashCode(); |
|
427 |
|
428 for (Object o : contents.values()) { |
|
429 if (o instanceof Object[]) |
|
430 hashcode += Arrays.deepHashCode((Object[]) o); |
|
431 else if (o instanceof byte[]) |
|
432 hashcode += Arrays.hashCode((byte[]) o); |
|
433 else if (o instanceof short[]) |
|
434 hashcode += Arrays.hashCode((short[]) o); |
|
435 else if (o instanceof int[]) |
|
436 hashcode += Arrays.hashCode((int[]) o); |
|
437 else if (o instanceof long[]) |
|
438 hashcode += Arrays.hashCode((long[]) o); |
|
439 else if (o instanceof char[]) |
|
440 hashcode += Arrays.hashCode((char[]) o); |
|
441 else if (o instanceof float[]) |
|
442 hashcode += Arrays.hashCode((float[]) o); |
|
443 else if (o instanceof double[]) |
|
444 hashcode += Arrays.hashCode((double[]) o); |
|
445 else if (o instanceof boolean[]) |
|
446 hashcode += Arrays.hashCode((boolean[]) o); |
|
447 else if (o != null) |
|
448 hashcode += o.hashCode(); |
|
449 } |
|
450 |
|
451 return hashcode; |
|
452 } |
|
453 |
|
454 /** |
|
455 * Returns a string representation of this <code>CompositeDataSupport</code> instance. |
|
456 * <p> |
|
457 * The string representation consists of the name of this class (ie <code>javax.management.openmbean.CompositeDataSupport</code>), |
|
458 * the string representation of the composite type of this instance, and the string representation of the contents |
|
459 * (ie list the itemName=itemValue mappings). |
|
460 * |
|
461 * @return a string representation of this <code>CompositeDataSupport</code> instance |
|
462 */ |
|
463 @Override |
|
464 public String toString() { |
|
465 return new StringBuilder() |
|
466 .append(this.getClass().getName()) |
|
467 .append("(compositeType=") |
|
468 .append(compositeType.toString()) |
|
469 .append(",contents=") |
|
470 .append(contentString()) |
|
471 .append(")") |
|
472 .toString(); |
|
473 } |
|
474 |
|
475 private String contentString() { |
|
476 StringBuilder sb = new StringBuilder("{"); |
|
477 String sep = ""; |
|
478 for (Map.Entry<String, Object> entry : contents.entrySet()) { |
|
479 sb.append(sep).append(entry.getKey()).append("="); |
|
480 String s = Arrays.deepToString(new Object[] {entry.getValue()}); |
|
481 sb.append(s.substring(1, s.length() - 1)); |
|
482 sep = ", "; |
|
483 } |
|
484 sb.append("}"); |
|
485 return sb.toString(); |
|
486 } |
|
487 } |