1 7 8 9 package javax.management.openmbean; 10 11 12 import java.io.Serializable ; 15 import java.util.Set ; 16 import java.util.HashSet ; 17 import java.util.Collections ; 18 import java.lang.Comparable ; 20 21 import javax.management.MBeanParameterInfo ; 24 25 26 35 public class OpenMBeanParameterInfoSupport 36 extends MBeanParameterInfo 37 implements OpenMBeanParameterInfo , Serializable { 38 39 40 41 static final long serialVersionUID = -7235016873758443122L; 42 43 46 private OpenType openType; 47 48 51 private Object defaultValue = null; 52 53 56 private Set legalValues = null; 58 61 private Comparable minValue = null; 62 63 66 private Comparable maxValue = null; 67 68 69 private transient Integer myHashCode = null; private transient String myToString = null; 72 73 87 public OpenMBeanParameterInfoSupport(String name, 88 String description, 89 OpenType openType) { 90 91 super(name, ( (openType==null) ? null : openType.getClassName() ), description); 94 95 if ( (name == null) || (name.trim().equals("")) ) { 98 throw new IllegalArgumentException ("Argument name cannot be null or empty."); 99 } 100 if ( (description == null) || (description.trim().equals("")) ) { 101 throw new IllegalArgumentException ("Argument description cannot be null or empty."); 102 } 103 if (openType == null) { 104 throw new IllegalArgumentException ("Argument openType cannot be null."); 105 } 106 107 this.openType = openType; 110 } 111 112 113 135 public OpenMBeanParameterInfoSupport(String name, 136 String description, 137 OpenType openType, 138 Object defaultValue) throws OpenDataException { 139 140 this(name, description, openType); 143 144 if (defaultValue != null) { 147 if ( (openType.isArray()) || (openType instanceof TabularType ) ) { 149 throw new OpenDataException ("Default value not supported for ArrayType and TabularType."); 150 } 151 if ( ! openType.isValue(defaultValue) ) { 153 throw new OpenDataException ("Argument defaultValue's class [\""+ defaultValue.getClass().getName() + 154 "\"] does not match the one defined in openType[\""+ openType.getClassName() +"\"]."); 155 } 156 this.defaultValue = defaultValue; 159 } 160 } 161 162 198 public OpenMBeanParameterInfoSupport(String name, 199 String description, 200 OpenType openType, 201 Object defaultValue, 202 Object [] legalValues) throws OpenDataException { 203 204 this(name, description, openType, defaultValue); 207 208 if ( (legalValues != null) && (legalValues.length > 0) ) { 211 if ( (openType instanceof TabularType ) || (openType.isArray()) ) { 213 throw new OpenDataException ("Legal values not supported for TabularType and arrays"); 214 } 215 for (int i = 0; i < legalValues.length; i++ ) { 217 if ( ! openType.isValue(legalValues[i]) ) { 218 throw new OpenDataException ("Element legalValues["+ i +"]="+ legalValues[i] + 219 " is not a valid value for the specified openType ["+ openType.toString() +"]."); 220 } 221 } 222 Set tmpSet = new HashSet (legalValues.length+1, 1); 225 for (int i = 0; i < legalValues.length; i++ ) { 226 tmpSet.add(legalValues[i]); 227 } 228 this.legalValues = Collections.unmodifiableSet(tmpSet); 230 } 231 232 if ( (this.hasDefaultValue()) && (this.hasLegalValues()) ) { 235 if ( ! this.legalValues.contains(defaultValue) ) { 236 throw new OpenDataException ("defaultValue is not contained in legalValues"); 237 } 238 } 239 240 } 241 242 243 282 public OpenMBeanParameterInfoSupport(String name, 283 String description, 284 OpenType openType, 285 Object defaultValue, 286 Comparable minValue, 287 Comparable maxValue) throws OpenDataException { 288 289 this(name, description, openType, defaultValue); 292 293 if (minValue != null) { 297 if ( ! openType.isValue(minValue) ) { 298 throw new OpenDataException ("Argument minValue's class [\""+ minValue.getClass().getName() + 299 "\"] does not match openType's definition [\""+ openType.getClassName() +"\"]."); 300 } 301 this.minValue = minValue; 303 } 304 305 if (maxValue != null) { 309 if ( ! openType.isValue(maxValue) ) { 310 throw new OpenDataException ("Argument maxValue's class [\""+ maxValue.getClass().getName() + 311 "\"] does not match openType's definition [\""+ openType.getClassName() +"\"]."); 312 } 313 this.maxValue = maxValue; 315 } 316 317 if (hasMinValue() && hasMaxValue()) { 320 if (minValue.compareTo(maxValue) > 0) { 321 throw new OpenDataException ("minValue cannot be greater than maxValue."); 322 } 323 } 324 325 if ( (this.hasDefaultValue()) && (this.hasMinValue()) ) { 328 if (minValue.compareTo((Comparable )defaultValue) > 0) { 329 throw new OpenDataException ("minValue cannot be greater than defaultValue."); 330 } 331 } 332 if ( (this.hasDefaultValue()) && (this.hasMaxValue()) ) { 333 if (((Comparable )defaultValue).compareTo(maxValue) > 0) { 334 throw new OpenDataException ("defaultValue cannot be greater than maxValue."); 335 } 336 } 337 } 338 339 342 public OpenType getOpenType() { 343 return openType; 344 } 345 346 350 public Object getDefaultValue() { 351 352 357 return defaultValue; 358 } 359 360 364 public Set getLegalValues() { 365 366 371 return (legalValues); 373 } 374 375 379 public Comparable getMinValue() { 380 381 383 return minValue; 384 } 385 386 390 public Comparable getMaxValue() { 391 392 394 return maxValue; 395 } 396 397 401 public boolean hasDefaultValue() { 402 403 return (defaultValue != null); 404 } 405 406 410 public boolean hasLegalValues() { 411 412 return (legalValues != null); 413 } 414 415 419 public boolean hasMinValue() { 420 421 return (minValue != null); 422 } 423 424 428 public boolean hasMaxValue() { 429 430 return (maxValue != null); 431 } 432 433 434 445 public boolean isValue(Object obj) { 446 447 boolean result; 448 449 if ( hasDefaultValue() && obj == null ) { 450 result = true; 451 } 452 else if ( ! openType.isValue(obj) ) { 453 result = false; 454 } 455 else if ( hasLegalValues() && ! legalValues.contains(obj) ) { 456 result = false; 457 } 458 else if ( hasMinValue() && (minValue.compareTo(obj)>0) ) { 459 result = false; 460 } 461 else if ( hasMaxValue() && (maxValue.compareTo(obj)<0) ) { 462 result = false; 463 } 464 else { 465 result = true; 466 } 467 468 return result; 469 } 470 471 472 473 474 475 493 public boolean equals(Object obj) { 494 495 if (obj == null) { 498 return false; 499 } 500 501 OpenMBeanParameterInfo other; 504 try { 505 other = (OpenMBeanParameterInfo ) obj; 506 } catch (ClassCastException e) { 507 return false; 508 } 509 510 513 if ( ! this.getName().equals(other.getName()) ) { 515 return false; 516 } 517 518 if ( ! this.getOpenType().equals(other.getOpenType()) ) { 520 return false; 521 } 522 523 if (this.hasDefaultValue()) { 525 if ( ! this.defaultValue.equals(other.getDefaultValue()) ) { 526 return false; 527 } 528 } else { 529 if (other.hasDefaultValue()) { 530 return false; 531 } 532 } 533 534 if (this.hasMinValue()) { 536 if ( ! this.minValue.equals(other.getMinValue()) ) { 537 return false; 538 } 539 } else { 540 if (other.hasMinValue()) { 541 return false; 542 } 543 } 544 545 if (this.hasMaxValue()) { 547 if ( ! this.maxValue.equals(other.getMaxValue()) ) { 548 return false; 549 } 550 } else { 551 if (other.hasMaxValue()) { 552 return false; 553 } 554 } 555 556 if (this.hasLegalValues()) { 558 if ( ! this.legalValues.equals(other.getLegalValues()) ) { 559 return false; 560 } 561 } else { 562 if (other.hasLegalValues()) { 563 return false; 564 } 565 } 566 567 return true; 570 } 571 572 593 public int hashCode() { 594 595 if (myHashCode == null) { 598 int value = 0; 599 value += this.getName().hashCode(); 600 value += this.openType.hashCode(); 601 if (this.hasDefaultValue()) { 602 value += this.defaultValue.hashCode(); 603 } 604 if (this.hasMinValue()) { 605 value += this.minValue.hashCode(); 606 } 607 if (this.hasMaxValue()) { 608 value += this.maxValue.hashCode(); 609 } 610 if (this.hasLegalValues()) { 611 value += this.legalValues.hashCode(); 612 } 613 myHashCode = new Integer (value); 614 } 615 616 return myHashCode.intValue(); 619 } 620 621 633 public String toString() { 634 635 if (myToString == null) { 638 myToString = new StringBuffer () 639 .append(this.getClass().getName()) 640 .append("(name=") 641 .append(this.getName()) 642 .append(",openType=") 643 .append(this.openType.toString()) 644 .append(",default=") 645 .append(String.valueOf(this.defaultValue)) 646 .append(",min=") 647 .append(String.valueOf(this.minValue)) 648 .append(",max=") 649 .append(String.valueOf(this.maxValue)) 650 .append(",legals=") 651 .append(String.valueOf(this.legalValues)) 652 .append(")") 653 .toString(); 654 } 655 656 return myToString; 659 } 660 661 } 662 | Popular Tags |