J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2007 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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.Map; |
| 37 | import java.util.Set; |
| 38 | import java.util.SortedMap; |
| 39 | import java.util.TreeMap; |
| 40 | |
| 41 | // jmx import |
| 42 | // |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * The <tt>CompositeDataSupport</tt> class is the <i>open data</i> class which |
| 47 | * implements the <tt>CompositeData</tt> interface. |
| 48 | * |
| 49 | * |
| 50 | * @since 1.5 |
| 51 | */ |
| 52 | public class CompositeDataSupport |
| 53 | implements CompositeData, Serializable { |
| 54 | |
| 55 | /* Serial version */ |
| 56 | static final long serialVersionUID = 8003518976613702244L; |
| 57 | |
| 58 | /** |
| 59 | * @serial Internal representation of the mapping of item names to their |
| 60 | * respective values. |
| 61 | * A {@link SortedMap} is used for faster retrieval of elements. |
| 62 | */ |
| 63 | private SortedMap<String, Object> contents = new TreeMap<String, Object>(); |
| 64 | |
| 65 | /** |
| 66 | * @serial The <i>composite type </i> of this <i>composite data</i> instance. |
| 67 | */ |
| 68 | private CompositeType compositeType; |
| 69 | |
| 70 | /** |
| 71 | * <p> |
| 72 | * Constructs a <tt>CompositeDataSupport</tt> instance with the specified |
| 73 | * <tt>compositeType</tt>, whose item values |
| 74 | * are specified by <tt>itemValues[]</tt>, in the same order as in |
| 75 | * <tt>itemNames[]</tt>. |
| 76 | * As a <tt>CompositeType</tt> does not specify any order on its items, |
| 77 | * the <tt>itemNames[]</tt> parameter is used |
| 78 | * to specify the order in which the values are given in <tt>itemValues[]</tt>. |
| 79 | * The items contained in this <tt>CompositeDataSupport</tt> instance are |
| 80 | * internally stored in a <tt>TreeMap</tt>, |
| 81 | * thus sorted in ascending lexicographic order of their names, for faster |
| 82 | * retrieval of individual item values. |
| 83 | * <p> |
| 84 | * The constructor checks that all the constraints listed below for each |
| 85 | * parameter are satisfied, |
| 86 | * and throws the appropriate exception if they are not. |
| 87 | * <p> |
| 88 | * @param compositeType the <i>composite type </i> of this <i>composite |
| 89 | * data</i> instance; |
| 90 | * must not be null. |
| 91 | * <p> |
| 92 | * @param itemNames <tt>itemNames</tt> must list, in any order, all the |
| 93 | * item names defined in <tt>compositeType</tt>; |
| 94 | * the order in which the names are listed, is used to |
| 95 | * match values in <tt>itemValues[]</tt>; |
| 96 | * must not be null or empty. |
| 97 | * <p> |
| 98 | * @param itemValues the values of the items, listed in the same order as |
| 99 | * their respective names in <tt>itemNames</tt>; |
| 100 | * each item value can be null, but if it is non-null it must be |
| 101 | * a valid value for the open type defined in <tt>compositeType</tt> for the corresponding item; |
| 102 | * must be of the same size as <tt>itemNames</tt>; must not be null or empty. |
| 103 | * <p> |
| 104 | * @throws IllegalArgumentException <tt>compositeType</tt> is null, or <tt>itemNames[]</tt> or <tt>itemValues[]</tt> is null or empty, |
| 105 | * or one of the elements in <tt>itemNames[]</tt> is a null or empty string, |
| 106 | * or <tt>itemNames[]</tt> and <tt>itemValues[]</tt> are not of the same size. |
| 107 | * <p> |
| 108 | * @throws OpenDataException <tt>itemNames[]</tt> or <tt>itemValues[]</tt>'s size differs from |
| 109 | * the number of items defined in <tt>compositeType</tt>, |
| 110 | * or one of the elements in <tt>itemNames[]</tt> does not exist as an item name defined in <tt>compositeType</tt>, |
| 111 | * or one of the elements in <tt>itemValues[]</tt> is not a valid value for the corresponding item |
| 112 | * as defined in <tt>compositeType</tt>. |
| 113 | * <p> |
| 114 | */ |
| 115 | public CompositeDataSupport(CompositeType compositeType, String[] itemNames, Object[] itemValues) |
| 116 | throws OpenDataException { |
| 117 | |
| 118 | // Check compositeType is not null |
| 119 | // |
| 120 | if (compositeType == null) { |
| 121 | throw new IllegalArgumentException("Argument compositeType cannot be null."); |
| 122 | } |
| 123 | |
| 124 | // item names defined in compositeType: |
| 125 | Set<String> namesSet = compositeType.keySet(); |
| 126 | |
| 127 | // Check the array itemNames is not null or empty (length!=0) and |
| 128 | // that there is no null element or empty string in it |
| 129 | // |
| 130 | checkForNullElement(itemNames, "itemNames"); |
| 131 | checkForEmptyString(itemNames, "itemNames"); |
| 132 | |
| 133 | // Check the array itemValues is not null or empty (length!=0) |
| 134 | // (NOTE: we allow null values as array elements) |
| 135 | // |
| 136 | if ( (itemValues == null) || (itemValues.length == 0) ) { |
| 137 | throw new IllegalArgumentException("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("Array arguments itemNames[] and itemValues[] "+ |
| 144 | "should be of same length (got "+ itemNames.length + |
| 145 | " and "+ itemValues.length +")."); |
| 146 | } |
| 147 | |
| 148 | // Check the size of the 2 arrays is equal to the number of items defined in compositeType |
| 149 | // |
| 150 | if (itemNames.length != namesSet.size()) { |
| 151 | throw new OpenDataException("The size of array arguments itemNames[] and itemValues[] should be equal to the number of items defined"+ |
| 152 | " in argument compositeType (found "+ itemNames.length +" elements in itemNames[] and itemValues[],"+ |
| 153 | " expecting "+ namesSet.size() +" elements according to compositeType."); |
| 154 | } |
| 155 | |
| 156 | // Check parameter itemNames[] contains all names defined in the compositeType of this instance |
| 157 | // |
| 158 | if ( ! Arrays.asList(itemNames).containsAll(namesSet) ) { |
| 159 | throw new OpenDataException("Argument itemNames[] does not contain all names defined in the compositeType of this instance."); |
| 160 | } |
| 161 | |
| 162 | // Check each element of itemValues[], if not null, is of the open type defined for the corresponding item |
| 163 | // |
| 164 | OpenType<?> itemType; |
| 165 | for (int i=0; i<itemValues.length; i++) { |
| 166 | itemType = compositeType.getType(itemNames[i]); |
| 167 | if ( (itemValues[i] != null) && (! itemType.isValue(itemValues[i])) ) { |
| 168 | throw new OpenDataException("Argument's element itemValues["+ i +"]=\""+ itemValues[i] +"\" is not a valid value for"+ |
| 169 | " this item (itemName="+ itemNames[i] +",itemType="+ itemType +")."); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // Initialize internal fields: compositeType and contents |
| 174 | // |
| 175 | this.compositeType = compositeType; |
| 176 | for (int i=0; i<itemNames.length; i++) { |
| 177 | this.contents.put(itemNames[i], itemValues[i]); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * <p> |
| 183 | * Constructs a <tt>CompositeDataSupport</tt> instance with the specified <tt>compositeType</tt>, whose item names and corresponding values |
| 184 | * are given by the mappings in the map <tt>items</tt>. |
| 185 | * This constructor converts the keys to a string array and the values to an object array and calls |
| 186 | * <tt>CompositeDataSupport(javax.management.openmbean.CompositeType, java.lang.String[], java.lang.Object[])</tt>. |
| 187 | * <p> |
| 188 | * @param compositeType the <i>composite type </i> of this <i>composite data</i> instance; |
| 189 | * must not be null. |
| 190 | * <p> |
| 191 | * @param items the mappings of all the item names to their values; |
| 192 | * <tt>items</tt> must contain all the item names defined in <tt>compositeType</tt>; |
| 193 | * must not be null or empty. |
| 194 | * <p> |
| 195 | * @throws IllegalArgumentException <tt>compositeType</tt> is null, or <tt>items</tt> is null or empty, |
| 196 | * or one of the keys in <tt>items</tt> is a null or empty string, |
| 197 | * or one of the values in <tt>items</tt> is null. |
| 198 | * <p> |
| 199 | * @throws OpenDataException <tt>items</tt>' size differs from the number of items defined in <tt>compositeType</tt>, |
| 200 | * or one of the keys in <tt>items</tt> does not exist as an item name defined in <tt>compositeType</tt>, |
| 201 | * or one of the values in <tt>items</tt> is not a valid value for the corresponding item |
| 202 | * as defined in <tt>compositeType</tt>. |
| 203 | * <p> |
| 204 | * @throws ArrayStoreException one or more keys in <tt>items</tt> is not of the class <tt>java.lang.String</tt>. |
| 205 | * <p> |
| 206 | */ |
| 207 | public CompositeDataSupport(CompositeType compositeType, |
| 208 | Map<String,?> items) |
| 209 | throws OpenDataException { |
| 210 | |
| 211 | |
| 212 | // Let the other constructor do the job, as the call to another constructor must be the first call |
| 213 | // |
| 214 | this( compositeType, |
| 215 | (items==null ? null : items.keySet().toArray(new String[items.size()])), // may raise an ArrayStoreException |
| 216 | (items==null ? null : items.values().toArray()) ); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * |
| 221 | */ |
| 222 | private static void checkForNullElement(Object[] arg, String argName) { |
| 223 | if ( (arg == null) || (arg.length == 0) ) { |
| 224 | throw new IllegalArgumentException( |
| 225 | "Argument "+ argName +"[] cannot be null or empty."); |
| 226 | } |
| 227 | for (int i=0; i<arg.length; i++) { |
| 228 | if (arg[i] == null) { |
| 229 | throw new IllegalArgumentException( |
| 230 | "Argument's element "+ argName +"["+ i +"] cannot be null."); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * |
| 237 | */ |
| 238 | private static void checkForEmptyString(String[] arg, String argName) { |
| 239 | for (int i=0; i<arg.length; i++) { |
| 240 | if (arg[i].trim().equals("")) { |
| 241 | throw new IllegalArgumentException( |
| 242 | "Argument's element "+ argName +"["+ i +"] cannot be an empty string."); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Returns the <i>composite type </i> of this <i>composite data</i> instance. |
| 249 | */ |
| 250 | public CompositeType getCompositeType() { |
| 251 | |
| 252 | return compositeType; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Returns the value of the item whose name is <tt>key</tt>. |
| 257 | * |
| 258 | * @throws IllegalArgumentException if <tt>key</tt> is a null or empty String. |
| 259 | * |
| 260 | * @throws InvalidKeyException if <tt>key</tt> is not an existing item name for |
| 261 | * this <tt>CompositeData</tt> instance. |
| 262 | */ |
| 263 | public Object get(String key) { |
| 264 | |
| 265 | if ( (key == null) || (key.trim().equals("")) ) { |
| 266 | throw new IllegalArgumentException("Argument key cannot be a null or empty String."); |
| 267 | } |
| 268 | if ( ! contents.containsKey(key.trim())) { |
| 269 | throw new InvalidKeyException("Argument key=\""+ key.trim() +"\" is not an existing item name for this CompositeData instance."); |
| 270 | } |
| 271 | return contents.get(key.trim()); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Returns an array of the values of the items whose names are specified by |
| 276 | * <tt>keys</tt>, in the same order as <tt>keys</tt>. |
| 277 | * |
| 278 | * @throws IllegalArgumentException if an element in <tt>keys</tt> is a null |
| 279 | * or empty String. |
| 280 | * |
| 281 | * @throws InvalidKeyException if an element in <tt>keys</tt> is not an existing |
| 282 | * item name for this <tt>CompositeData</tt> instance. |
| 283 | */ |
| 284 | public Object[] getAll(String[] keys) { |
| 285 | |
| 286 | if ( (keys == null) || (keys.length == 0) ) { |
| 287 | return new Object[0]; |
| 288 | } |
| 289 | Object[] results = new Object[keys.length]; |
| 290 | for (int i=0; i<keys.length; i++) { |
| 291 | results[i] = this.get(keys[i]); |
| 292 | } |
| 293 | return results; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Returns <tt>true</tt> if and only if this <tt>CompositeData</tt> instance contains |
| 298 | * an item whose name is <tt>key</tt>. |
| 299 | * If <tt>key</tt> is a null or empty String, this method simply returns false. |
| 300 | */ |
| 301 | public boolean containsKey(String key) { |
| 302 | |
| 303 | if ( (key == null) || (key.trim().equals("")) ) { |
| 304 | return false; |
| 305 | } |
| 306 | return contents.containsKey(key); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Returns <tt>true</tt> if and only if this <tt>CompositeData</tt> instance |
| 311 | * contains an item |
| 312 | * whose value is <tt>value</tt>. |
| 313 | */ |
| 314 | public boolean containsValue(Object value) { |
| 315 | |
| 316 | return contents.containsValue(value); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Returns an unmodifiable Collection view of the item values contained in this |
| 321 | * <tt>CompositeData</tt> instance. |
| 322 | * The returned collection's iterator will return the values in the ascending |
| 323 | * lexicographic order of the corresponding |
| 324 | * item names. |
| 325 | */ |
| 326 | public Collection<?> values() { |
| 327 | |
| 328 | return Collections.unmodifiableCollection(contents.values()); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Compares the specified <var>obj</var> parameter with this |
| 333 | * <code>CompositeDataSupport</code> instance for equality. |
| 334 | * <p> |
| 335 | * Returns <tt>true</tt> if and only if all of the following statements are true: |
| 336 | * <ul> |
| 337 | * <li><var>obj</var> is non null,</li> |
| 338 | * <li><var>obj</var> also implements the <code>CompositeData</code> interface,</li> |
| 339 | * <li>their composite types are equal</li> |
| 340 | * <li>their contents, i.e. (name, value) pairs are equal. If a value contained in |
| 341 | * the content is an array, the value comparison is done as if by calling |
| 342 | * the {@link java.util.Arrays#deepEquals(Object[], Object[]) deepEquals} method |
| 343 | * for arrays of object reference types or the appropriate overloading of |
| 344 | * {@code Arrays.equals(e1,e2)} for arrays of primitive types</li> |
| 345 | * </ul> |
| 346 | * <p> |
| 347 | * This ensures that this <tt>equals</tt> method works properly for |
| 348 | * <var>obj</var> parameters which are different implementations of the |
| 349 | * <code>CompositeData</code> interface, with the restrictions mentioned in the |
| 350 | * {@link java.util.Collection#equals(Object) equals} |
| 351 | * method of the <tt>java.util.Collection</tt> interface. |
| 352 | * |
| 353 | * @param obj the object to be compared for equality with this |
| 354 | * <code>CompositeDataSupport</code> instance. |
| 355 | * @return <code>true</code> if the specified object is equal to this |
| 356 | * <code>CompositeDataSupport</code> instance. |
| 357 | */ |
| 358 | public boolean equals(Object obj) { |
| 359 | if (this == obj) { |
| 360 | return true; |
| 361 | } |
| 362 | |
| 363 | // if obj is not a CompositeData, return false |
| 364 | if (!(obj instanceof CompositeData)) { |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | CompositeData other = (CompositeData) obj; |
| 369 | |
| 370 | // their compositeType should be equal |
| 371 | if (!this.getCompositeType().equals(other.getCompositeType()) ) { |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | if (contents.size() != other.values().size()) { |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | for (Map.Entry<String,Object> entry : contents.entrySet()) { |
| 380 | Object e1 = entry.getValue(); |
| 381 | Object e2 = other.get(entry.getKey()); |
| 382 | |
| 383 | if (e1 == e2) |
| 384 | continue; |
| 385 | if (e1 == null) |
| 386 | return false; |
| 387 | |
| 388 | boolean eq = e1.getClass().isArray() ? |
| 389 | Arrays.deepEquals(new Object[] {e1}, new Object[] {e2}) : |
| 390 | e1.equals(e2); |
| 391 | |
| 392 | if (!eq) |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | // All tests for equality were successful |
| 397 | // |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Returns the hash code value for this <code>CompositeDataSupport</code> instance. |
| 403 | * <p> |
| 404 | * The hash code of a <code>CompositeDataSupport</code> instance is the sum of the hash codes |
| 405 | * of all elements of information used in <code>equals</code> comparisons |
| 406 | * (ie: its <i>composite type</i> and all the item values). |
| 407 | * <p> |
| 408 | * This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code> |
| 409 | * for any two <code>CompositeDataSupport</code> instances <code>t1</code> and <code>t2</code>, |
| 410 | * as required by the general contract of the method |
| 411 | * {@link Object#hashCode() Object.hashCode()}. |
| 412 | * <p> |
| 413 | * Each item value's hash code is added to the returned hash code. |
| 414 | * If an item value is an array, |
| 415 | * its hash code is obtained as if by calling the |
| 416 | * {@link java.util.Arrays#deepHashCode(Object[]) deepHashCode} method |
| 417 | * for arrays of object reference types or the appropriate overloading |
| 418 | * of {@code Arrays.hashCode(e)} for arrays of primitive types. |
| 419 | * |
| 420 | * @return the hash code value for this <code>CompositeDataSupport</code> instance |
| 421 | */ |
| 422 | public int hashCode() { |
| 423 | int hashcode = compositeType.hashCode(); |
| 424 | |
| 425 | for (Object o : contents.values()) { |
| 426 | if (o instanceof Object[]) |
| 427 | hashcode += Arrays.deepHashCode((Object[]) o); |
| 428 | else if (o instanceof byte[]) |
| 429 | hashcode += Arrays.hashCode((byte[]) o); |
| 430 | else if (o instanceof short[]) |
| 431 | hashcode += Arrays.hashCode((short[]) o); |
| 432 | else if (o instanceof int[]) |
| 433 | hashcode += Arrays.hashCode((int[]) o); |
| 434 | else if (o instanceof long[]) |
| 435 | hashcode += Arrays.hashCode((long[]) o); |
| 436 | else if (o instanceof char[]) |
| 437 | hashcode += Arrays.hashCode((char[]) o); |
| 438 | else if (o instanceof float[]) |
| 439 | hashcode += Arrays.hashCode((float[]) o); |
| 440 | else if (o instanceof double[]) |
| 441 | hashcode += Arrays.hashCode((double[]) o); |
| 442 | else if (o instanceof boolean[]) |
| 443 | hashcode += Arrays.hashCode((boolean[]) o); |
| 444 | else if (o != null) |
| 445 | hashcode += o.hashCode(); |
| 446 | } |
| 447 | |
| 448 | return hashcode; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * Returns a string representation of this <code>CompositeDataSupport</code> instance. |
| 453 | * <p> |
| 454 | * The string representation consists of the name of this class (ie <code>javax.management.openmbean.CompositeDataSupport</code>), |
| 455 | * the string representation of the composite type of this instance, and the string representation of the contents |
| 456 | * (ie list the itemName=itemValue mappings). |
| 457 | * |
| 458 | * @return a string representation of this <code>CompositeDataSupport</code> instance |
| 459 | */ |
| 460 | public String toString() { |
| 461 | |
| 462 | return new StringBuilder() |
| 463 | .append(this.getClass().getName()) |
| 464 | .append("(compositeType=") |
| 465 | .append(compositeType.toString()) |
| 466 | .append(",contents=") |
| 467 | .append(contents.toString()) |
| 468 | .append(")") |
| 469 | .toString(); |
| 470 | } |
| 471 | |
| 472 | } |