1 7 8 9 package javax.management.openmbean; 10 11 12 import java.io.Serializable ; 15 import java.util.Set ; 16 import java.util.TreeMap ; 17 import java.util.Collections ; 18 import java.util.Iterator ; 19 20 23 24 34 public class CompositeType 35 extends OpenType 36 implements Serializable { 37 38 39 static final long serialVersionUID = -5366242454346948798L; 40 41 44 private TreeMap nameToDescription; 45 46 49 private TreeMap nameToType; 50 51 private transient Integer myHashCode = null; private transient String myToString = null; private transient Set myNamesSet = null; 55 56 57 58 104 public CompositeType(String typeName, 105 String description, 106 String [] itemNames, 107 String [] itemDescriptions, 108 OpenType [] itemTypes) throws OpenDataException { 109 110 super(CompositeData .class.getName(), typeName, description); 113 114 checkForNullElement(itemNames, "itemNames"); 117 checkForNullElement(itemDescriptions, "itemDescriptions"); 118 checkForNullElement(itemTypes, "itemTypes"); 119 checkForEmptyString(itemNames, "itemNames"); 120 checkForEmptyString(itemDescriptions, "itemDescriptions"); 121 122 if ( (itemNames.length != itemDescriptions.length) || (itemNames.length != itemTypes.length) ) { 125 throw new IllegalArgumentException ("Array arguments itemNames[], itemDescriptions[] and itemTypes[] "+ 126 "should be of same length (got "+ itemNames.length +", "+ 127 itemDescriptions.length +" and "+ itemTypes.length +")."); 128 } 129 130 nameToDescription = new TreeMap (); 134 nameToType = new TreeMap (); 135 String key; 136 for (int i=0; i<itemNames.length; i++) { 137 key = itemNames[i].trim(); 138 if (nameToDescription.containsKey(key)) { 139 throw new OpenDataException ("Argument's element itemNames["+ i +"]=\""+ itemNames[i] + 140 "\" duplicates a previous item names."); 141 } 142 nameToDescription.put(key, itemDescriptions[i].trim()); 143 nameToType.put(key, itemTypes[i]); 144 } 145 } 146 147 private static void checkForNullElement(Object [] arg, String argName) { 148 if ( (arg == null) || (arg.length == 0) ) { 149 throw new IllegalArgumentException ("Argument "+ argName +"[] cannot be null or empty."); 150 } 151 for (int i=0; i<arg.length; i++) { 152 if (arg[i] == null) { 153 throw new IllegalArgumentException ("Argument's element "+ argName +"["+ i +"] cannot be null."); 154 } 155 } 156 } 157 158 private static void checkForEmptyString(String [] arg, String argName) { 159 for (int i=0; i<arg.length; i++) { 160 if (arg[i].trim().equals("")) { 161 throw new IllegalArgumentException ("Argument's element "+ argName +"["+ i +"] cannot be an empty string."); 162 } 163 } 164 } 165 166 167 168 176 public boolean containsKey(String itemName) { 177 178 if (itemName == null) { 179 return false; 180 } 181 return nameToDescription.containsKey(itemName); 182 } 183 184 193 public String getDescription(String itemName) { 194 195 if (itemName == null) { 196 return null; 197 } 198 return (String ) nameToDescription.get(itemName); 199 } 200 201 210 public OpenType getType(String itemName) { 211 212 if (itemName == null) { 213 return null; 214 } 215 return (OpenType ) nameToType.get(itemName); 216 } 217 218 224 public Set keySet() { 225 226 if (myNamesSet == null) { 228 myNamesSet = Collections.unmodifiableSet(nameToDescription.keySet()); 229 } 230 231 return myNamesSet; } 233 234 235 249 public boolean isValue(Object obj) { 250 251 if (obj == null) { 254 return false; 255 } 256 257 CompositeData value; 260 try { 261 value = (CompositeData ) obj; 262 } catch (ClassCastException e) { 263 return false; 264 } 265 266 return this.equals(value.getCompositeType()); 269 } 270 271 272 273 274 288 public boolean equals(Object obj) { 289 290 if (obj == null) { 293 return false; 294 } 295 296 CompositeType other; 299 try { 300 other = (CompositeType ) obj; 301 } catch (ClassCastException e) { 302 return false; 303 } 304 305 308 if ( ! this.getTypeName().equals(other.getTypeName()) ) { 310 return false; 311 } 312 313 if ( ! this.nameToType.equals(other.nameToType) ) { 315 return false; 316 } 317 318 return true; 321 } 322 323 339 public int hashCode() { 340 341 if (myHashCode == null) { 344 int value = 0; 345 value += this.getTypeName().hashCode(); 346 String key; 347 for (Iterator k = nameToDescription.keySet().iterator(); k.hasNext(); ) { 348 key = (String ) k.next(); 349 value += key.hashCode(); 350 value += this.nameToType.get(key).hashCode(); 351 } 352 myHashCode = new Integer (value); 353 } 354 355 return myHashCode.intValue(); 358 } 359 360 372 public String toString() { 373 374 if (myToString == null) { 377 StringBuffer result = new StringBuffer (); 378 result.append(this.getClass().getName()); 379 result.append("(name="); 380 result.append(getTypeName()); 381 result.append(",items=("); 382 int i=0; 383 Iterator k=nameToType.keySet().iterator(); 384 String key; 385 while (k.hasNext()) { 386 key = (String ) k.next(); 387 if (i > 0) result.append(","); 388 result.append("(itemName="); 389 result.append(key); 390 result.append(",itemType="); 391 result.append(nameToType.get(key).toString() +")"); 392 i++; 393 } 394 result.append("))"); 395 myToString = result.toString(); 396 } 397 398 return myToString; 401 } 402 403 } 404 405 | Popular Tags |