1 7 8 9 package javax.management.openmbean; 10 11 12 import java.io.IOException ; 15 import java.io.Serializable ; 16 import java.io.ObjectInputStream ; 17 import java.util.Iterator ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 import java.util.Set ; 21 import java.util.Collection ; 22 import java.util.Collections ; 23 import java.util.Arrays ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 30 31 41 public class TabularDataSupport 42 implements TabularData , Map , Cloneable , Serializable { 43 44 45 46 static final long serialVersionUID = 5720150593236309827L; 47 48 49 52 private Map dataMap; 53 54 57 private TabularType tabularType; 58 59 62 private transient String [] indexNamesArray; 63 64 65 66 67 68 69 80 public TabularDataSupport(TabularType tabularType) { 81 82 this(tabularType, 101, 0.75f); 83 } 84 85 100 public TabularDataSupport(TabularType tabularType, int initialCapacity, float loadFactor) { 101 102 if (tabularType == null) { 105 throw new IllegalArgumentException ("Argument tabularType cannot be null."); 106 } 107 108 this.tabularType = tabularType; 111 List tmpNames = tabularType.getIndexNames(); 112 this.indexNamesArray = (String []) tmpNames.toArray(new String [tmpNames.size()]); 113 114 this.dataMap = new HashMap (initialCapacity, loadFactor); 117 } 118 119 120 121 122 123 124 125 128 public TabularType getTabularType() { 129 130 return tabularType; 131 } 132 133 151 public Object [] calculateIndex(CompositeData value) { 152 153 checkValueType(value); 156 157 return internalCalculateIndex(value).toArray(); 160 } 161 162 163 164 165 166 167 168 178 public boolean containsKey(Object key) { 179 180 Object [] k; 183 try { 184 k = (Object []) key; 185 } catch (ClassCastException e) { 186 return false; 187 } 188 189 return this.containsKey(k); 190 } 191 192 201 public boolean containsKey(Object [] key) { 202 203 return ( key == null ? false : dataMap.containsKey(Arrays.asList(key)) ); 204 } 205 206 215 public boolean containsValue(CompositeData value) { 216 217 return dataMap.containsValue(value); 218 } 219 220 228 public boolean containsValue(Object value) { 229 230 return dataMap.containsValue(value); 231 } 232 233 241 public Object get(Object key) { 242 243 return get((Object []) key); 244 } 245 246 262 public CompositeData get(Object [] key) { 263 264 checkKeyType(key); 268 269 return (CompositeData ) dataMap.get(Arrays.asList(key)); 272 } 273 274 275 276 277 278 279 280 297 public Object put(Object key, Object value) { 298 299 put((CompositeData ) value); 300 return value; 301 } 302 303 public void put(CompositeData value) { 304 305 List index = checkValueAndIndex(value); 310 311 dataMap.put(index, value); 314 } 315 316 329 public Object remove(Object key) { 330 331 return remove((Object []) key); 332 } 333 334 349 public CompositeData remove(Object [] key) { 350 351 checkKeyType(key); 355 356 return (CompositeData ) dataMap.remove(Arrays.asList(key)); 359 } 360 361 362 363 364 365 366 367 386 public void putAll(Map t) { 387 388 if ( (t == null) || (t.size() == 0) ) { 391 return; 392 } 393 394 CompositeData [] values; 397 try { 398 values = (CompositeData []) t.values().toArray(new CompositeData [t.size()]); 399 } catch (java.lang.ArrayStoreException e) { 400 throw new ClassCastException ("Map argument t contains values which are not instances of <tt>CompositeData</tt>"); 401 } 402 403 putAll(values); 406 } 407 408 427 public void putAll(CompositeData [] values) { 428 429 if ( (values == null) || (values.length == 0) ) { 432 return; 433 } 434 435 ArrayList indexes = new ArrayList (values.length + 1); 437 438 List index; 441 for (int i=0; i<values.length; i++) { 442 index = checkValueAndIndex(values[i]); 444 if (indexes.contains(index)) { 446 throw new KeyAlreadyExistsException ("Argument elements values["+ i +"] and values["+ indexes.indexOf(index) + 447 "] have the same indexes, "+ 448 "calculated according to this TabularData instance's tabularType."); 449 } 450 indexes.add(index); 452 } 453 454 for (int i=0; i<values.length; i++) { 457 dataMap.put(indexes.get(i), values[i]); 458 } 459 } 460 461 464 public void clear() { 465 466 dataMap.clear(); 467 } 468 469 470 471 472 473 478 public int size() { 479 480 return dataMap.size(); 481 } 482 483 488 public boolean isEmpty() { 489 490 return (this.size() == 0); 491 } 492 493 494 495 496 497 511 public Set keySet() { 512 513 return dataMap.keySet() ; 514 } 515 516 529 public Collection values() { 530 531 return dataMap.values() ; 532 } 533 534 535 552 public Set entrySet() { 553 554 return dataMap.entrySet(); 555 } 556 557 558 559 560 561 566 public Object clone() { 567 try { 568 TabularDataSupport c = (TabularDataSupport ) super.clone(); 569 c.dataMap = (HashMap ) ((HashMap ) c.dataMap).clone(); 570 return c; 571 } 572 catch (CloneNotSupportedException e) { 573 throw new InternalError (e.toString()); 574 } 575 } 576 577 578 595 public boolean equals(Object obj) { 596 597 if (obj == null) { 600 return false; 601 } 602 603 TabularData other; 606 try { 607 other = (TabularData ) obj; 608 } catch (ClassCastException e) { 609 return false; 610 } 611 612 615 if ( ! this.getTabularType().equals(other.getTabularType()) ) { 617 return false; 618 } 619 620 625 if (this.size() != other.size()) { 626 return false; 627 } 628 for (Iterator iter = this.values().iterator(); iter.hasNext(); ) { 629 CompositeData value = (CompositeData ) iter.next(); 630 if ( ! other.containsValue(value) ) { 631 return false; 632 } 633 } 634 635 return true; 638 } 639 640 658 public int hashCode() { 659 660 int result = 0; 661 662 result += this.tabularType.hashCode(); 663 for (Iterator iter = this.values().iterator(); iter.hasNext(); ) { 664 result += ((CompositeData )iter.next()).hashCode(); 665 } 666 667 return result; 668 669 } 670 671 681 public String toString() { 682 683 return new StringBuffer () 684 .append(this.getClass().getName()) 685 .append("(tabularType=") 686 .append(tabularType.toString()) 687 .append(",contents=") 688 .append(dataMap.toString()) 689 .append(")") 690 .toString(); 691 } 692 693 694 695 696 697 698 699 709 private List internalCalculateIndex(CompositeData value) { 710 711 return Collections.unmodifiableList(Arrays.asList(value.getAll(this.indexNamesArray))); 712 } 713 714 720 private void checkKeyType(Object [] key) { 721 722 if ( (key == null) || (key.length == 0) ) { 725 throw new NullPointerException ("Argument key cannot be null or empty."); 726 } 727 728 729 730 if (key.length != this.indexNamesArray.length) { 733 throw new InvalidKeyException ("Argument key's length="+ key.length + 734 " is different from the number of item values, which is "+ indexNamesArray.length + 735 ", specified for the indexing rows in this TabularData instance."); 736 } 737 738 OpenType keyElementType; 741 for (int i=0; i<key.length; i++) { 742 keyElementType = tabularType.getRowType().getType(this.indexNamesArray[i]); 743 if ( (key[i] != null) && (! keyElementType.isValue(key[i])) ) { 744 throw new InvalidKeyException ("Argument element key["+ i +"] is not a value for the open type expected for "+ 745 "this element of the index, whose name is \""+ indexNamesArray[i] + 746 "\" and whose open type is "+ keyElementType); 747 } 748 } 749 } 750 751 758 private void checkValueType(CompositeData value) { 759 760 if (value == null) { 763 throw new NullPointerException ("Argument value cannot be null."); 764 } 765 766 if ( ! value.getCompositeType().equals(tabularType.getRowType()) ) { 769 throw new InvalidOpenTypeException ("Argument value's composite type ["+ value.getCompositeType() + 770 "] is not equal to "+ 771 "this TabularData instance's row type ["+ tabularType.getRowType() +"]."); 772 } 773 } 774 775 787 private List checkValueAndIndex(CompositeData value) { 788 789 checkValueType(value); 792 793 List index = internalCalculateIndex(value); 797 798 if (dataMap.containsKey(index)) { 799 throw new KeyAlreadyExistsException ("Argument value's index, calculated according to this TabularData "+ 800 "instance's tabularType, already refers to a value in this table."); 801 } 802 803 return index; 806 } 807 808 811 private void readObject(ObjectInputStream in) 812 throws IOException , ClassNotFoundException { 813 in.defaultReadObject(); 814 List tmpNames = tabularType.getIndexNames(); 815 indexNamesArray = (String []) tmpNames.toArray(new String [tmpNames.size()]); 816 } 817 } 818 | Popular Tags |