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.MBeanAttributeInfo ; 24 25 26 35 public class OpenMBeanAttributeInfoSupport 36 extends MBeanAttributeInfo 37 implements OpenMBeanAttributeInfo , Serializable { 38 39 40 41 static final long serialVersionUID = -4867215622149721849L; 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 93 public OpenMBeanAttributeInfoSupport(String name, 94 String description, 95 OpenType openType, 96 boolean isReadable, 97 boolean isWritable, 98 boolean isIs) { 99 100 super(name, ( (openType==null) ? null : openType.getClassName() ), description, isReadable, isWritable, isIs); 103 104 if ( (name == null) || (name.trim().equals("")) ) { 107 throw new IllegalArgumentException ("Argument name cannot be null or empty."); 108 } 109 if (openType == null) { 110 throw new IllegalArgumentException ("Argument openType cannot be null."); 111 } 112 if ( (description == null) || (description.trim().equals("")) ) { 113 throw new IllegalArgumentException ("Argument description cannot be null or empty."); 114 } 115 116 this.openType = openType; 119 } 120 121 149 public OpenMBeanAttributeInfoSupport(String name, 150 String description, 151 OpenType openType, 152 boolean isReadable, 153 boolean isWritable, 154 boolean isIs, 155 Object defaultValue) throws OpenDataException { 156 157 this(name, description, openType, isReadable, isWritable, isIs); 160 161 if (defaultValue != null) { 164 if ( (openType.isArray()) || (openType instanceof TabularType ) ) { 166 throw new OpenDataException ("Default value not supported for ArrayType and TabularType."); 167 } 168 if ( ! openType.isValue(defaultValue) ) { 170 throw new OpenDataException ("Argument defaultValue's class [\""+ defaultValue.getClass().getName() + 171 "\"] does not match the one defined in openType[\""+ openType.getClassName() +"\"]."); 172 } 173 this.defaultValue = defaultValue; 176 } 177 } 178 179 180 222 public OpenMBeanAttributeInfoSupport(String name, 223 String description, 224 OpenType openType, 225 boolean isReadable, 226 boolean isWritable, 227 boolean isIs, 228 Object defaultValue, 229 Object [] legalValues) throws OpenDataException { 230 231 this(name, description, openType, isReadable, isWritable, isIs, defaultValue); 234 235 if ( (legalValues != null) && (legalValues.length > 0) ){ 238 if ( (openType instanceof TabularType ) || (openType.isArray()) ) { 240 throw new OpenDataException ("Legal values not supported for TabularType and arrays"); 241 } 242 for (int i = 0; i < legalValues.length; i++ ) { 244 if ( ! openType.isValue(legalValues[i]) ) { 245 throw new OpenDataException ("Element legalValues["+ i +"]="+ legalValues[i] + 246 " is not a valid value for the specified openType ["+ openType.toString() +"]."); 247 } 248 } 249 Set tmpSet = new HashSet (legalValues.length+1, 1); 252 for (int i = 0; i < legalValues.length; i++ ) { 253 tmpSet.add(legalValues[i]); 254 } 255 this.legalValues = Collections.unmodifiableSet(tmpSet); 257 } 258 259 if ( (this.hasDefaultValue()) && (this.hasLegalValues()) ) { 262 if ( ! this.legalValues.contains(defaultValue) ) { 263 throw new OpenDataException ("defaultValue is not contained in legalValues"); 264 } 265 } 266 } 267 268 269 314 public OpenMBeanAttributeInfoSupport(String name, 315 String description, 316 OpenType openType, 317 boolean isReadable, 318 boolean isWritable, 319 boolean isIs, 320 Object defaultValue, 321 Comparable minValue, 322 Comparable maxValue) throws OpenDataException { 323 324 this(name, description, openType, isReadable, isWritable, isIs, defaultValue); 327 328 if (minValue != null) { 332 if ( ! openType.isValue(minValue) ) { 333 throw new OpenDataException ("Argument minValue's class [\""+ minValue.getClass().getName() + 334 "\"] does not match openType's definition [\""+ openType.getClassName() +"\"]."); 335 } 336 this.minValue = minValue; 338 } 339 340 if (maxValue != null) { 344 if ( ! openType.isValue(maxValue) ) { 345 throw new OpenDataException ("Argument maxValue's class [\""+ maxValue.getClass().getName() + 346 "\"] does not match openType's definition [\""+ openType.getClassName() +"\"]."); 347 } 348 this.maxValue = maxValue; 350 } 351 352 if (hasMinValue() && hasMaxValue()) { 355 if (minValue.compareTo(maxValue) > 0) { 356 throw new OpenDataException ("minValue cannot be greater than maxValue."); 357 } 358 } 359 360 if ( (this.hasDefaultValue()) && (this.hasMinValue()) ) { 363 if (minValue.compareTo((Comparable )defaultValue) > 0) { 364 throw new OpenDataException ("minValue cannot be greater than defaultValue."); 365 } 366 } 367 if ( (this.hasDefaultValue()) && (this.hasMaxValue()) ) { 368 if (((Comparable )defaultValue).compareTo(maxValue) > 0) { 369 throw new OpenDataException ("defaultValue cannot be greater than maxValue."); 370 } 371 } 372 } 373 374 377 public OpenType getOpenType() { 378 return openType; 379 } 380 381 385 public Object getDefaultValue() { 386 387 392 return defaultValue; 393 } 394 395 399 public Set getLegalValues() { 400 401 406 return (legalValues); 408 } 409 410 414 public Comparable getMinValue() { 415 416 418 return minValue; 419 } 420 421 425 public Comparable getMaxValue() { 426 427 429 return maxValue; 430 } 431 432 436 public boolean hasDefaultValue() { 437 438 return (defaultValue != null); 439 } 440 441 445 public boolean hasLegalValues() { 446 447 return (legalValues != null); 448 } 449 450 454 public boolean hasMinValue() { 455 456 return (minValue != null); 457 } 458 459 463 public boolean hasMaxValue() { 464 465 return (maxValue != null); 466 } 467 468 469 479 public boolean isValue(Object obj) { 480 481 boolean result; 482 483 if ( hasDefaultValue() && obj == null ) { 484 result = true; 485 } 486 else if ( ! openType.isValue(obj) ) { 487 result = false; 488 } 489 else if ( hasLegalValues() && ! legalValues.contains(obj) ) { 490 result = false; 491 } 492 else if ( hasMinValue() && (minValue.compareTo(obj)>0) ) { 493 result = false; 494 } 495 else if ( hasMaxValue() && (maxValue.compareTo(obj)<0) ) { 496 result = false; 497 } 498 else { 499 result = true; 500 } 501 502 return result; 503 } 504 505 506 507 508 527 public boolean equals(Object obj) { 528 529 if (obj == null) { 532 return false; 533 } 534 535 OpenMBeanAttributeInfo other; 538 try { 539 other = (OpenMBeanAttributeInfo ) obj; 540 } catch (ClassCastException e) { 541 return false; 542 } 543 544 547 if ( ! this.getName().equals(other.getName()) ) { 549 return false; 550 } 551 552 if ( ! this.getOpenType().equals(other.getOpenType()) ) { 554 return false; 555 } 556 557 if ( (this.isReadable() != other.isReadable()) || 559 (this.isWritable() != other.isWritable()) || 560 (this.isIs() != other.isIs()) ) { 561 return false; 562 } 563 564 565 if (this.hasDefaultValue()) { 567 if ( ! this.defaultValue.equals(other.getDefaultValue()) ) { 568 return false; 569 } 570 } else { 571 if (other.hasDefaultValue()) { 572 return false; 573 } 574 } 575 576 if (this.hasMinValue()) { 578 if ( ! this.minValue.equals(other.getMinValue()) ) { 579 return false; 580 } 581 } else { 582 if (other.hasMinValue()) { 583 return false; 584 } 585 } 586 587 if (this.hasMaxValue()) { 589 if ( ! this.maxValue.equals(other.getMaxValue()) ) { 590 return false; 591 } 592 } else { 593 if (other.hasMaxValue()) { 594 return false; 595 } 596 } 597 598 if (this.hasLegalValues()) { 600 if ( ! this.legalValues.equals(other.getLegalValues()) ) { 601 return false; 602 } 603 } else { 604 if (other.hasLegalValues()) { 605 return false; 606 } 607 } 608 609 return true; 612 } 613 614 635 public int hashCode() { 636 637 if (myHashCode == null) { 640 int value = 0; 641 value += this.getName().hashCode(); 642 value += this.openType.hashCode(); 643 if (this.hasDefaultValue()) { 644 value += this.defaultValue.hashCode(); 645 } 646 if (this.hasMinValue()) { 647 value += this.minValue.hashCode(); 648 } 649 if (this.hasMaxValue()) { 650 value += this.maxValue.hashCode(); 651 } 652 if (this.hasLegalValues()) { 653 value += this.legalValues.hashCode(); 654 } 655 myHashCode = new Integer (value); 656 } 657 658 return myHashCode.intValue(); 661 } 662 663 675 public String toString() { 676 677 if (myToString == null) { 680 myToString = new StringBuffer () 681 .append(this.getClass().getName()) 682 .append("(name=") 683 .append(this.getName()) 684 .append(",openType=") 685 .append(this.openType.toString()) 686 .append(",default=") 687 .append(String.valueOf(this.defaultValue)) 688 .append(",min=") 689 .append(String.valueOf(this.minValue)) 690 .append(",max=") 691 .append(String.valueOf(this.maxValue)) 692 .append(",legals=") 693 .append(String.valueOf(this.legalValues)) 694 .append(")") 695 .toString(); 696 } 697 698 return myToString; 701 } 702 703 } 704 | Popular Tags |