1 17 package org.eclipse.emf.common.notify.impl; 18 19 20 import org.eclipse.emf.common.notify.Notification; 21 import org.eclipse.emf.common.notify.NotificationChain; 22 import org.eclipse.emf.common.notify.Notifier; 23 24 25 28 public class NotificationImpl implements Notification, NotificationChain 29 { 30 34 public static final int PRIMITIVE_TYPE_OBJECT = -1; 35 36 40 public static final int PRIMITIVE_TYPE_BOOLEAN = 0; 41 42 46 public static final int PRIMITIVE_TYPE_BYTE = 1; 47 48 52 public static final int PRIMITIVE_TYPE_CHAR = 2; 53 54 58 public static final int PRIMITIVE_TYPE_DOUBLE = 3; 59 60 64 public static final int PRIMITIVE_TYPE_FLOAT = 4; 65 66 70 public static final int PRIMITIVE_TYPE_INT = 5; 71 72 76 public static final int PRIMITIVE_TYPE_LONG = 6; 77 78 82 public static final int PRIMITIVE_TYPE_SHORT = 7; 83 84 88 protected static final int IS_SET_CHANGE_INDEX = NO_INDEX - 1; 89 90 102 protected int primitiveType; 103 104 108 protected int eventType; 109 110 114 protected Object oldValue; 115 116 120 protected Object newValue; 121 122 138 protected long oldSimplePrimitiveValue; 139 140 156 protected long newSimplePrimitiveValue; 157 158 166 protected double oldIEEEPrimitiveValue; 167 168 176 protected double newIEEEPrimitiveValue; 177 178 182 protected int position; 183 184 187 protected NotificationChain next; 188 189 195 public NotificationImpl(int eventType, Object oldValue, Object newValue) 196 { 197 this(eventType, oldValue, newValue, NO_INDEX); 198 } 199 200 207 public NotificationImpl(int eventType, Object oldValue, Object newValue, boolean isSetChange) 208 { 209 this(eventType, oldValue, newValue, isSetChange ? IS_SET_CHANGE_INDEX : NO_INDEX); 210 } 211 212 218 public NotificationImpl(int eventType, Object oldValue, Object newValue, int position) 219 { 220 this.eventType = eventType; 221 this.oldValue = oldValue; 222 this.newValue = newValue; 223 this.position = position; 224 this.primitiveType = PRIMITIVE_TYPE_OBJECT; 225 } 226 227 234 public NotificationImpl(int eventType, Object oldValue, Object newValue, int position, boolean wasSet) 235 { 236 this.eventType = eventType; 237 this.oldValue = oldValue; 238 this.newValue = newValue; 239 this.position = position; 240 this.primitiveType = PRIMITIVE_TYPE_OBJECT; 241 if (!wasSet) 242 { 243 this.position = IS_SET_CHANGE_INDEX - position - 1; 244 } 245 } 246 247 254 public NotificationImpl(int eventType, boolean oldBooleanValue, boolean newBooleanValue, boolean isSetChange) 255 { 256 this(eventType, oldBooleanValue, newBooleanValue); 257 if (isSetChange) 258 { 259 this.position = IS_SET_CHANGE_INDEX; 260 } 261 } 262 263 269 public NotificationImpl(int eventType, boolean oldBooleanValue, boolean newBooleanValue) 270 { 271 this.eventType = eventType; 272 this.oldSimplePrimitiveValue = oldBooleanValue ? 1 : 0; 273 this.newSimplePrimitiveValue = newBooleanValue ? 1 : 0; 274 this.position = NO_INDEX; 275 this.primitiveType = PRIMITIVE_TYPE_BOOLEAN; 276 } 277 278 285 public NotificationImpl(int eventType, byte oldByteValue, byte newByteValue, boolean isSetChange) 286 { 287 this(eventType, oldByteValue, newByteValue); 288 if (isSetChange) 289 { 290 this.position = IS_SET_CHANGE_INDEX; 291 } 292 } 293 294 300 public NotificationImpl(int eventType, byte oldByteValue, byte newByteValue) 301 { 302 this.eventType = eventType; 303 this.oldSimplePrimitiveValue = oldByteValue; 304 this.newSimplePrimitiveValue = newByteValue; 305 this.position = Notification.NO_INDEX; 306 this.primitiveType = PRIMITIVE_TYPE_BYTE; 307 } 308 309 316 public NotificationImpl(int eventType, char oldCharValue, char newCharValue, boolean isSetChange) 317 { 318 this(eventType, oldCharValue, newCharValue); 319 if (isSetChange) 320 { 321 this.position = IS_SET_CHANGE_INDEX; 322 } 323 } 324 325 331 public NotificationImpl(int eventType, char oldCharValue, char newCharValue) 332 { 333 this.eventType = eventType; 334 this.oldSimplePrimitiveValue = oldCharValue; 335 this.newSimplePrimitiveValue = newCharValue; 336 this.position = Notification.NO_INDEX; 337 this.primitiveType = PRIMITIVE_TYPE_CHAR; 338 } 339 340 347 public NotificationImpl(int eventType, double oldDoubleValue, double newDoubleValue, boolean isSetChange) 348 { 349 this(eventType, oldDoubleValue, newDoubleValue); 350 if (isSetChange) 351 { 352 this.position = IS_SET_CHANGE_INDEX; 353 } 354 } 355 356 362 public NotificationImpl(int eventType, double oldDoubleValue, double newDoubleValue) 363 { 364 this.eventType = eventType; 365 this.oldIEEEPrimitiveValue = oldDoubleValue; 366 this.newIEEEPrimitiveValue = newDoubleValue; 367 this.position = Notification.NO_INDEX; 368 this.primitiveType = PRIMITIVE_TYPE_DOUBLE; 369 } 370 371 378 public NotificationImpl(int eventType, float oldFloatValue, float newFloatValue, boolean isSetChange) 379 { 380 this(eventType, oldFloatValue, newFloatValue); 381 if (isSetChange) 382 { 383 this.position = IS_SET_CHANGE_INDEX; 384 } 385 } 386 387 393 public NotificationImpl(int eventType, float oldFloatValue, float newFloatValue) 394 { 395 this.eventType = eventType; 396 this.oldIEEEPrimitiveValue = oldFloatValue; 397 this.newIEEEPrimitiveValue = newFloatValue; 398 this.position = Notification.NO_INDEX; 399 this.primitiveType = PRIMITIVE_TYPE_FLOAT; 400 } 401 402 409 public NotificationImpl(int eventType, int oldIntValue, int newIntValue, boolean isSetChange) 410 { 411 this(eventType, oldIntValue, newIntValue); 412 if (isSetChange) 413 { 414 this.position = IS_SET_CHANGE_INDEX; 415 } 416 } 417 418 424 public NotificationImpl(int eventType, int oldIntValue, int newIntValue) 425 { 426 this.eventType = eventType; 427 this.oldSimplePrimitiveValue = oldIntValue; 428 this.newSimplePrimitiveValue = newIntValue; 429 this.position = Notification.NO_INDEX; 430 this.primitiveType = PRIMITIVE_TYPE_INT; 431 } 432 433 440 public NotificationImpl(int eventType, long oldLongValue, long newLongValue, boolean isSetChange) 441 { 442 this(eventType, oldLongValue, newLongValue); 443 if (isSetChange) 444 { 445 this.position = IS_SET_CHANGE_INDEX; 446 } 447 } 448 449 455 public NotificationImpl(int eventType, long oldLongValue, long newLongValue) 456 { 457 this.eventType = eventType; 458 this.oldSimplePrimitiveValue = oldLongValue; 459 this.newSimplePrimitiveValue = newLongValue; 460 this.position = Notification.NO_INDEX; 461 this.primitiveType = PRIMITIVE_TYPE_LONG; 462 } 463 464 471 public NotificationImpl(int eventType, short oldShortValue, short newShortValue, boolean isSetChange) 472 { 473 this(eventType, oldShortValue, newShortValue); 474 if (isSetChange) 475 { 476 this.position = IS_SET_CHANGE_INDEX; 477 } 478 } 479 480 486 public NotificationImpl(int eventType, short oldShortValue, short newShortValue) 487 { 488 this.eventType = eventType; 489 this.oldSimplePrimitiveValue = oldShortValue; 490 this.newSimplePrimitiveValue = newShortValue; 491 this.position = Notification.NO_INDEX; 492 this.primitiveType = PRIMITIVE_TYPE_SHORT; 493 } 494 495 public Object getNotifier() 496 { 497 return null; 498 } 499 500 public int getEventType() 501 { 502 return eventType; 503 } 504 505 public Object getFeature() 506 { 507 return null; 508 } 509 510 public int getFeatureID(Class expectedClass) 511 { 512 return NO_FEATURE_ID; 513 } 514 515 public Object getOldValue() 516 { 517 if (oldValue == null) 518 { 519 switch (primitiveType) 520 { 521 case PRIMITIVE_TYPE_BOOLEAN: 522 oldValue = getOldBooleanValue() ? Boolean.TRUE : Boolean.FALSE; 523 break; 524 case PRIMITIVE_TYPE_BYTE: 525 oldValue = new Byte (getOldByteValue()); 526 break; 527 case PRIMITIVE_TYPE_CHAR: 528 oldValue = new Character (getOldCharValue()); 529 break; 530 case PRIMITIVE_TYPE_DOUBLE: 531 oldValue = new Double (getOldDoubleValue()); 532 break; 533 case PRIMITIVE_TYPE_FLOAT: 534 oldValue = new Float (getOldFloatValue()); 535 break; 536 case PRIMITIVE_TYPE_LONG: 537 oldValue = new Long (getOldLongValue()); 538 break; 539 case PRIMITIVE_TYPE_INT: 540 oldValue = new Integer (getOldIntValue()); 541 break; 542 case PRIMITIVE_TYPE_SHORT: 543 oldValue = new Short (getOldShortValue()); 544 break; 545 } 546 } 547 return oldValue; 548 } 549 550 public Object getNewValue() 551 { 552 if (newValue == null) 553 { 554 switch (primitiveType) 555 { 556 case PRIMITIVE_TYPE_BOOLEAN: 557 newValue = getNewBooleanValue() ? Boolean.TRUE : Boolean.FALSE; 558 break; 559 case PRIMITIVE_TYPE_BYTE: 560 newValue = new Byte (getNewByteValue()); 561 break; 562 case PRIMITIVE_TYPE_CHAR: 563 newValue = new Character (getNewCharValue()); 564 break; 565 case PRIMITIVE_TYPE_DOUBLE: 566 newValue = new Double (getNewDoubleValue()); 567 break; 568 case PRIMITIVE_TYPE_FLOAT: 569 newValue = new Float (getNewFloatValue()); 570 break; 571 case PRIMITIVE_TYPE_LONG: 572 newValue = new Long (getNewLongValue()); 573 break; 574 case PRIMITIVE_TYPE_INT: 575 newValue = new Integer (getNewIntValue()); 576 break; 577 case PRIMITIVE_TYPE_SHORT: 578 newValue = new Short (getNewShortValue()); 579 break; 580 } 581 } 582 return newValue; 583 } 584 585 public boolean isTouch() 586 { 587 switch (eventType) 588 { 589 case Notification.RESOLVE: 590 case Notification.REMOVING_ADAPTER: 591 { 592 return true; 593 } 594 case Notification.ADD: 595 case Notification.ADD_MANY: 596 case Notification.REMOVE: 597 case Notification.REMOVE_MANY: 598 case Notification.MOVE: 599 { 600 return false; 601 } 602 case Notification.SET: 603 case Notification.UNSET: 604 { 605 if (position == IS_SET_CHANGE_INDEX) 606 { 607 return false; 608 } 609 else 610 { 611 switch (primitiveType) 612 { 613 case PRIMITIVE_TYPE_BOOLEAN: 614 case PRIMITIVE_TYPE_BYTE: 615 case PRIMITIVE_TYPE_CHAR: 616 case PRIMITIVE_TYPE_LONG: 617 case PRIMITIVE_TYPE_INT: 618 case PRIMITIVE_TYPE_SHORT: 619 { 620 return oldSimplePrimitiveValue == newSimplePrimitiveValue; 621 } 622 case PRIMITIVE_TYPE_DOUBLE: 623 case PRIMITIVE_TYPE_FLOAT: 624 { 625 return oldIEEEPrimitiveValue == newIEEEPrimitiveValue; 626 } 627 default: 628 { 629 return oldValue == null ? newValue == null : oldValue.equals(newValue); 630 } 631 } 632 } 633 } 634 default: 635 { 636 return false; 637 } 638 } 639 } 640 641 public boolean isReset() 642 { 643 switch (eventType) 644 { 645 case Notification.SET: 646 Object defaultValue = getFeatureDefaultValue(); 647 switch (primitiveType) 648 { 649 case PRIMITIVE_TYPE_BOOLEAN: 650 return defaultValue != null && ((Boolean )defaultValue).booleanValue() == (newSimplePrimitiveValue != 0); 651 case PRIMITIVE_TYPE_BYTE: 652 return defaultValue != null && ((Byte )defaultValue).byteValue() == (byte)newSimplePrimitiveValue; 653 case PRIMITIVE_TYPE_CHAR: 654 return defaultValue != null && ((Character )defaultValue).charValue() == (char)newSimplePrimitiveValue; 655 case PRIMITIVE_TYPE_LONG: 656 return defaultValue != null && ((Long )defaultValue).longValue() == newSimplePrimitiveValue; 657 case PRIMITIVE_TYPE_INT: 658 return defaultValue != null && ((Integer )defaultValue).intValue() == (int)newSimplePrimitiveValue; 659 case PRIMITIVE_TYPE_SHORT: 660 return defaultValue != null && ((Short )defaultValue).shortValue() == (short)newSimplePrimitiveValue; 661 case PRIMITIVE_TYPE_DOUBLE: 662 return defaultValue != null && ((Double )defaultValue).doubleValue() == newIEEEPrimitiveValue; 663 case PRIMITIVE_TYPE_FLOAT: 664 return defaultValue != null && ((Float )defaultValue).floatValue() == (float)newIEEEPrimitiveValue; 665 default: 666 return defaultValue == null ? newValue == null : defaultValue.equals(newValue); 667 } 668 case Notification.UNSET: 669 return true; 670 default: 671 return false; 672 } 673 } 674 675 public boolean wasSet() 676 { 677 switch (eventType) 678 { 679 case Notification.SET: 680 { 681 if (isFeatureUnsettable()) 682 { 683 return position != IS_SET_CHANGE_INDEX; 684 } 685 break; 686 } 687 case Notification.UNSET: 688 { 689 if (isFeatureUnsettable()) 690 { 691 return position == IS_SET_CHANGE_INDEX; 692 } 693 break; 694 } 695 case Notification.ADD: 696 case Notification.ADD_MANY: 697 case Notification.REMOVE: 698 case Notification.REMOVE_MANY: 699 case Notification.MOVE: 700 { 701 return position > IS_SET_CHANGE_INDEX; 702 } 703 default: 704 { 705 return false; 706 } 707 } 708 709 Object defaultValue = getFeatureDefaultValue(); 710 switch (primitiveType) 711 { 712 case PRIMITIVE_TYPE_BOOLEAN: 713 return defaultValue != null && ((Boolean )defaultValue).booleanValue() != (oldSimplePrimitiveValue != 0); 714 case PRIMITIVE_TYPE_BYTE: 715 return defaultValue != null && ((Byte )defaultValue).byteValue() != (byte)oldSimplePrimitiveValue; 716 case PRIMITIVE_TYPE_CHAR: 717 return defaultValue != null && ((Character )defaultValue).charValue() != (char)oldSimplePrimitiveValue; 718 case PRIMITIVE_TYPE_LONG: 719 return defaultValue != null && ((Long )defaultValue).longValue() != oldSimplePrimitiveValue; 720 case PRIMITIVE_TYPE_INT: 721 return defaultValue != null && ((Integer )defaultValue).intValue() != (int)oldSimplePrimitiveValue; 722 case PRIMITIVE_TYPE_SHORT: 723 return defaultValue != null && ((Short )defaultValue).shortValue() != (short)oldSimplePrimitiveValue; 724 case PRIMITIVE_TYPE_DOUBLE: 725 return defaultValue != null && ((Double )defaultValue).doubleValue() != oldIEEEPrimitiveValue; 726 case PRIMITIVE_TYPE_FLOAT: 727 return defaultValue != null && ((Float )defaultValue).floatValue() != (float)oldIEEEPrimitiveValue; 728 default: 729 return defaultValue == null ? oldValue != null : !defaultValue.equals(oldValue); 730 } 731 } 732 733 protected boolean isFeatureUnsettable() 734 { 735 return false; 736 } 737 738 protected Object getFeatureDefaultValue() 739 { 740 return null; 741 } 742 743 public int getPosition() 744 { 745 return position < 0 ? position < IS_SET_CHANGE_INDEX ? IS_SET_CHANGE_INDEX - position - 1 : NO_INDEX : position; 746 } 747 748 public boolean merge(Notification notification) 749 { 750 switch (eventType) 751 { 752 case Notification.SET: 753 case Notification.UNSET: 754 { 755 int notificationEventType = notification.getEventType(); 756 switch (notificationEventType) 757 { 758 case Notification.SET: 759 case Notification.UNSET: 760 { 761 Object notificationNotifier = notification.getNotifier(); 762 if (notificationNotifier == getNotifier() && getFeatureID(null) == notification.getFeatureID(null)) 763 { 764 newValue = notification.getNewValue(); 765 if (notification.getEventType() == Notification.SET) 766 { 767 eventType = Notification.SET; 768 } 769 return true; 770 } 771 } 772 } 773 } 774 } 775 776 return false; 777 } 778 779 public boolean getOldBooleanValue() 780 { 781 if (primitiveType != PRIMITIVE_TYPE_BOOLEAN) throw new IllegalStateException (); 782 return oldSimplePrimitiveValue != 0; 783 } 784 785 public boolean getNewBooleanValue() 786 { 787 if (primitiveType != PRIMITIVE_TYPE_BOOLEAN) throw new IllegalStateException (); 788 return newSimplePrimitiveValue != 0; 789 } 790 791 public byte getOldByteValue() 792 { 793 if (primitiveType != PRIMITIVE_TYPE_BYTE) throw new IllegalStateException (); 794 return (byte)oldSimplePrimitiveValue; 795 } 796 797 public byte getNewByteValue() 798 { 799 if (primitiveType != PRIMITIVE_TYPE_BYTE) throw new IllegalStateException (); 800 return (byte)newSimplePrimitiveValue; 801 } 802 803 public char getOldCharValue() 804 { 805 if (primitiveType != PRIMITIVE_TYPE_CHAR) throw new IllegalStateException (); 806 return (char)oldSimplePrimitiveValue; 807 } 808 809 public char getNewCharValue() 810 { 811 if (primitiveType != PRIMITIVE_TYPE_CHAR) throw new IllegalStateException (); 812 return (char)newSimplePrimitiveValue; 813 } 814 815 public double getOldDoubleValue() 816 { 817 if (primitiveType != PRIMITIVE_TYPE_DOUBLE) throw new IllegalStateException (); 818 return oldIEEEPrimitiveValue; 819 } 820 821 public double getNewDoubleValue() 822 { 823 if (primitiveType != PRIMITIVE_TYPE_DOUBLE) throw new IllegalStateException (); 824 return newIEEEPrimitiveValue; 825 } 826 827 public float getOldFloatValue() 828 { 829 if (primitiveType != PRIMITIVE_TYPE_FLOAT) throw new IllegalStateException (); 830 return (float)oldIEEEPrimitiveValue; 831 } 832 833 public float getNewFloatValue() 834 { 835 if (primitiveType != PRIMITIVE_TYPE_FLOAT) throw new IllegalStateException (); 836 return (float)newIEEEPrimitiveValue; 837 } 838 839 public int getOldIntValue() 840 { 841 if (primitiveType != PRIMITIVE_TYPE_INT) throw new IllegalStateException (); 842 return (int)oldSimplePrimitiveValue; 843 } 844 845 public int getNewIntValue() 846 { 847 if (primitiveType != PRIMITIVE_TYPE_INT) throw new IllegalStateException (); 848 return (int)newSimplePrimitiveValue; 849 } 850 851 public long getOldLongValue() 852 { 853 if (primitiveType != PRIMITIVE_TYPE_LONG) throw new IllegalStateException (); 854 return oldSimplePrimitiveValue; 855 } 856 857 public long getNewLongValue() 858 { 859 if (primitiveType != PRIMITIVE_TYPE_LONG) throw new IllegalStateException (); 860 return newSimplePrimitiveValue; 861 } 862 863 public short getOldShortValue() 864 { 865 if (primitiveType != PRIMITIVE_TYPE_SHORT) throw new IllegalStateException (); 866 return (short)oldSimplePrimitiveValue; 867 } 868 869 public short getNewShortValue() 870 { 871 if (primitiveType != PRIMITIVE_TYPE_SHORT) throw new IllegalStateException (); 872 return (short)newSimplePrimitiveValue; 873 } 874 875 public String getOldStringValue() 876 { 877 return oldValue == null ? null : oldValue.toString(); 878 } 879 880 public String getNewStringValue() 881 { 882 return newValue == null ? null : newValue.toString(); 883 } 884 885 890 public boolean add(Notification newNotification) 891 { 892 if (newNotification == null) 893 { 894 return false; 895 } 896 else 897 { 898 if (merge(newNotification)) 899 { 900 return false; 901 } 902 903 if (next == null) 904 { 905 try 906 { 907 next = (NotificationChain)newNotification; 908 return true; 909 } 910 catch (ClassCastException exception) 911 { 912 next = new NotificationChainImpl(); 913 return next.add(newNotification); 914 } 915 } 916 else 917 { 918 return next.add(newNotification); 919 } 920 } 921 } 922 923 926 public void dispatch() 927 { 928 Object notifier = getNotifier(); 929 if (notifier != null && getEventType() != -1) 930 { 931 ((Notifier)notifier).eNotify(this); 932 } 933 934 if (next != null) 935 { 936 next.dispatch(); 937 } 938 } 939 940 public String toString() 941 { 942 StringBuffer result = new StringBuffer (super.toString()); 943 result.append(" (eventType: "); 944 switch (eventType) 945 { 946 case Notification.SET: 947 { 948 result.append("SET"); 949 break; 950 } 951 case Notification.UNSET: 952 { 953 result.append("UNSET"); 954 break; 955 } 956 case Notification.ADD: 957 { 958 result.append("ADD"); 959 break; 960 } 961 case Notification.ADD_MANY: 962 { 963 result.append("ADD_MANY"); 964 break; 965 } 966 case Notification.REMOVE: 967 { 968 result.append("REMOVE"); 969 break; 970 } 971 case Notification.REMOVE_MANY: 972 { 973 result.append("REMOVE_MANY"); 974 break; 975 } 976 case Notification.MOVE: 977 { 978 result.append("MOVE"); 979 break; 980 } 981 case Notification.REMOVING_ADAPTER: 982 { 983 result.append("REMOVING_ADPATER"); 984 break; 985 } 986 case Notification.RESOLVE: 987 { 988 result.append("RESOLVE"); 989 break; 990 } 991 default: 992 { 993 result.append(eventType); 994 break; 995 } 996 } 997 if (isTouch()) 998 { 999 result.append(", touch: true"); 1000 } 1001 result.append(", position: "); 1002 result.append(getPosition()); 1003 result.append(", notifier: "); 1004 result.append(getNotifier()); 1005 result.append(", feature: "); 1006 result.append(getFeature()); 1007 result.append(", oldValue: "); 1008 result.append(getOldValue()); 1009 result.append(", newValue: "); 1010 if (eventType == Notification.REMOVE_MANY && newValue instanceof int []) 1011 { 1012 int [] positions = (int [])newValue; 1013 result.append("["); 1014 for (int i = 0; i < positions.length; ) 1015 { 1016 result.append(positions[i]); 1017 if (++i < positions.length) 1018 { 1019 result.append(", "); 1020 } 1021 } 1022 result.append("]"); 1023 } 1024 else 1025 { 1026 result.append(getNewValue()); 1027 } 1028 1029 result.append(", isTouch: "); 1030 result.append(isTouch()); 1031 result.append(", wasSet: "); 1032 result.append(wasSet()); 1033 result.append(")"); 1034 1035 return result.toString(); 1036 } 1037} 1038 | Popular Tags |