1 7 8 9 package javax.management.openmbean; 10 11 12 import java.io.Serializable ; 15 import java.util.Set ; 16 import java.util.Map ; 17 import java.util.SortedMap ; 18 import java.util.TreeMap ; 19 import java.util.Collection ; 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 import java.util.Arrays ; 23 24 25 28 29 38 public class CompositeDataSupport 39 implements CompositeData , Serializable { 40 41 42 static final long serialVersionUID = 8003518976613702244L; 43 44 48 private SortedMap contents = new TreeMap (); 49 50 53 private CompositeType compositeType; 54 55 56 57 92 public CompositeDataSupport(CompositeType compositeType, String [] itemNames, Object [] itemValues) 93 throws OpenDataException { 94 95 if (compositeType == null) { 98 throw new IllegalArgumentException ("Argument compositeType cannot be null."); 99 } 100 101 Set namesSet = compositeType.keySet(); 103 104 checkForNullElement(itemNames, "itemNames"); 108 checkForEmptyString(itemNames, "itemNames"); 109 110 if ( (itemValues == null) || (itemValues.length == 0) ) { 114 throw new IllegalArgumentException ("Argument itemValues[] cannot be null or empty."); 115 } 116 117 if (itemNames.length != itemValues.length) { 120 throw new IllegalArgumentException ("Array arguments itemNames[] and itemValues[] "+ 121 "should be of same length (got "+ itemNames.length + 122 " and "+ itemValues.length +")."); 123 } 124 125 if (itemNames.length != namesSet.size()) { 128 throw new OpenDataException ("The size of array arguments itemNames[] and itemValues[] should be equal to the number of items defined"+ 129 " in argument compositeType (found "+ itemNames.length +" elements in itemNames[] and itemValues[],"+ 130 " expecting "+ namesSet.size() +" elements according to compositeType."); 131 } 132 133 if ( ! Arrays.asList(itemNames).containsAll(namesSet) ) { 136 throw new OpenDataException ("Argument itemNames[] does not contain all names defined in the compositeType of this instance."); 137 } 138 139 OpenType itemType; 142 for (int i=0; i<itemValues.length; i++) { 143 itemType = compositeType.getType(itemNames[i]); 144 if ( (itemValues[i] != null) && (! itemType.isValue(itemValues[i])) ) { 145 throw new OpenDataException ("Argument's element itemValues["+ i +"]=\""+ itemValues[i] +"\" is not a valid value for"+ 146 " this item (itemName="+ itemNames[i] +",itemType="+ itemType +")."); 147 } 148 } 149 150 this.compositeType = compositeType; 153 for (int i=0; i<itemNames.length; i++) { 154 this.contents.put(itemNames[i], itemValues[i]); 155 } 156 } 157 158 184 public CompositeDataSupport(CompositeType compositeType, Map items) 185 throws OpenDataException { 186 187 188 this( compositeType, 191 (items==null ? null : (String []) items.keySet().toArray(new String [items.size()])), (items==null ? null : items.values().toArray()) ); 193 } 194 195 198 private static void checkForNullElement(Object [] arg, String argName) { 199 if ( (arg == null) || (arg.length == 0) ) { 200 throw new IllegalArgumentException ("Argument "+ argName +"[] cannot be null or empty."); 201 } 202 for (int i=0; i<arg.length; i++) { 203 if (arg[i] == null) { 204 throw new IllegalArgumentException ("Argument's element "+ argName +"["+ i +"] cannot be null."); 205 } 206 } 207 } 208 209 212 private static void checkForEmptyString(String [] arg, String argName) { 213 for (int i=0; i<arg.length; i++) { 214 if (arg[i].trim().equals("")) { 215 throw new IllegalArgumentException ("Argument's element "+ argName +"["+ i +"] cannot be an empty string."); 216 } 217 } 218 } 219 220 223 public CompositeType getCompositeType() { 224 225 return compositeType; 226 } 227 228 235 public Object get(String key) { 236 237 if ( (key == null) || (key.trim().equals("")) ) { 238 throw new IllegalArgumentException ("Argument key cannot be a null or empty String."); 239 } 240 if ( ! contents.containsKey(key.trim())) { 241 throw new InvalidKeyException ("Argument key=\""+ key.trim() +"\" is not an existing item name for this CompositeData instance."); 242 } 243 return contents.get(key.trim()); 244 } 245 246 253 public Object [] getAll(String [] keys) { 254 255 if ( (keys == null) || (keys.length == 0) ) { 256 return new Object [0]; 257 } 258 Object [] results = new Object [keys.length]; 259 for (int i=0; i<keys.length; i++) { 260 results[i] = this.get(keys[i]); 261 } 262 return results; 263 } 264 265 270 public boolean containsKey(String key) { 271 272 if ( (key == null) || (key.trim().equals("")) ) { 273 return false; 274 } 275 return contents.containsKey(key); 276 } 277 278 282 public boolean containsValue(Object value) { 283 284 return contents.containsValue(value); 285 } 286 287 292 public Collection values() { 293 294 return Collections.unmodifiableCollection(contents.values()); 295 } 296 297 316 public boolean equals(Object obj) { 317 318 if (obj == null) { 321 return false; 322 } 323 324 CompositeData other; 327 try { 328 other = (CompositeData ) obj; 329 } catch (ClassCastException e) { 330 return false; 331 } 332 333 if ( ! this.getCompositeType().equals(other.getCompositeType()) ) { 335 return false; 336 } 337 338 339 343 Map.Entry entry; 345 boolean ok; 346 for (Iterator iter = contents.entrySet().iterator(); iter.hasNext(); ) { 347 entry = (Map.Entry ) iter.next(); 348 ok = ( entry.getValue() == null ? 349 other.get((String )entry.getKey()) == null : 350 entry.getValue().equals(other.get((String )entry.getKey())) ); 351 if ( ! ok ) { 352 return false; 353 } 354 } 355 356 return true; 359 } 360 361 379 public int hashCode() { 380 381 int result = 0; 382 result += compositeType.hashCode(); 383 Map.Entry entry; 384 for (Iterator iter = contents.entrySet().iterator(); iter.hasNext(); ) { 385 entry = (Map.Entry ) iter.next(); 386 result += ( entry.getValue() == null ? 0 : entry.getValue().hashCode() ); 387 } 388 return result; 389 } 390 391 400 public String toString() { 401 402 return new StringBuffer () 403 .append(this.getClass().getName()) 404 .append("(compositeType=") 405 .append(compositeType.toString()) 406 .append(",contents=") 407 .append(contents.toString()) 408 .append(")") 409 .toString(); 410 } 411 412 } 413 | Popular Tags |