1 16 package org.apache.commons.lang.builder; 17 18 import java.lang.reflect.AccessibleObject ; 19 import java.lang.reflect.Field ; 20 import java.lang.reflect.Modifier ; 21 import java.util.Comparator ; 22 23 import org.apache.commons.lang.math.NumberUtils; 24 25 90 public class CompareToBuilder { 91 92 95 private int comparison; 96 97 104 public CompareToBuilder() { 105 super(); 106 comparison = 0; 107 } 108 109 136 public static int reflectionCompare(Object lhs, Object rhs) { 137 return reflectionCompare(lhs, rhs, false, null); 138 } 139 140 168 public static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients) { 169 return reflectionCompare(lhs, rhs, compareTransients, null); 170 } 171 172 203 public static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class reflectUpToClass) { 204 if (lhs == rhs) { 205 return 0; 206 } 207 if (lhs == null || rhs == null) { 208 throw new NullPointerException (); 209 } 210 Class lhsClazz = lhs.getClass(); 211 if (!lhsClazz.isInstance(rhs)) { 212 throw new ClassCastException (); 213 } 214 CompareToBuilder compareToBuilder = new CompareToBuilder(); 215 reflectionAppend(lhs, rhs, lhsClazz, compareToBuilder, compareTransients); 216 while (lhsClazz.getSuperclass() != null && lhsClazz != reflectUpToClass) { 217 lhsClazz = lhsClazz.getSuperclass(); 218 reflectionAppend(lhs, rhs, lhsClazz, compareToBuilder, compareTransients); 219 } 220 return compareToBuilder.toComparison(); 221 } 222 223 233 private static void reflectionAppend( 234 Object lhs, 235 Object rhs, 236 Class clazz, 237 CompareToBuilder builder, 238 boolean useTransients) { 239 240 Field [] fields = clazz.getDeclaredFields(); 241 AccessibleObject.setAccessible(fields, true); 242 for (int i = 0; i < fields.length && builder.comparison == 0; i++) { 243 Field f = fields[i]; 244 if ((f.getName().indexOf('$') == -1) 245 && (useTransients || !Modifier.isTransient(f.getModifiers())) 246 && (!Modifier.isStatic(f.getModifiers()))) { 247 try { 248 builder.append(f.get(lhs), f.get(rhs)); 249 } catch (IllegalAccessException e) { 250 throw new InternalError ("Unexpected IllegalAccessException"); 253 } 254 } 255 } 256 } 257 258 267 public CompareToBuilder appendSuper(int superCompareTo) { 268 if (comparison != 0) { 269 return this; 270 } 271 comparison = superCompareTo; 272 return this; 273 } 274 275 295 public CompareToBuilder append(Object lhs, Object rhs) { 296 return append(lhs, rhs, null); 297 } 298 299 324 public CompareToBuilder append(Object lhs, Object rhs, Comparator comparator) { 325 if (comparison != 0) { 326 return this; 327 } 328 if (lhs == rhs) { 329 return this; 330 } 331 if (lhs == null) { 332 comparison = -1; 333 return this; 334 } 335 if (rhs == null) { 336 comparison = +1; 337 return this; 338 } 339 if (lhs.getClass().isArray()) { 340 if (lhs instanceof long[]) { 344 append((long[]) lhs, (long[]) rhs); 345 } else if (lhs instanceof int[]) { 346 append((int[]) lhs, (int[]) rhs); 347 } else if (lhs instanceof short[]) { 348 append((short[]) lhs, (short[]) rhs); 349 } else if (lhs instanceof char[]) { 350 append((char[]) lhs, (char[]) rhs); 351 } else if (lhs instanceof byte[]) { 352 append((byte[]) lhs, (byte[]) rhs); 353 } else if (lhs instanceof double[]) { 354 append((double[]) lhs, (double[]) rhs); 355 } else if (lhs instanceof float[]) { 356 append((float[]) lhs, (float[]) rhs); 357 } else if (lhs instanceof boolean[]) { 358 append((boolean[]) lhs, (boolean[]) rhs); 359 } else { 360 append((Object []) lhs, (Object []) rhs, comparator); 363 } 364 } else { 365 if (comparator == null) { 367 comparison = ((Comparable ) lhs).compareTo(rhs); 368 } else { 369 comparison = comparator.compare(lhs, rhs); 370 } 371 } 372 return this; 373 } 374 375 384 public CompareToBuilder append(long lhs, long rhs) { 385 if (comparison != 0) { 386 return this; 387 } 388 comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0)); 389 return this; 390 } 391 392 400 public CompareToBuilder append(int lhs, int rhs) { 401 if (comparison != 0) { 402 return this; 403 } 404 comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0)); 405 return this; 406 } 407 408 416 public CompareToBuilder append(short lhs, short rhs) { 417 if (comparison != 0) { 418 return this; 419 } 420 comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0)); 421 return this; 422 } 423 424 432 public CompareToBuilder append(char lhs, char rhs) { 433 if (comparison != 0) { 434 return this; 435 } 436 comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0)); 437 return this; 438 } 439 440 448 public CompareToBuilder append(byte lhs, byte rhs) { 449 if (comparison != 0) { 450 return this; 451 } 452 comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0)); 453 return this; 454 } 455 456 469 public CompareToBuilder append(double lhs, double rhs) { 470 if (comparison != 0) { 471 return this; 472 } 473 comparison = NumberUtils.compare(lhs, rhs); 474 return this; 475 } 476 477 490 public CompareToBuilder append(float lhs, float rhs) { 491 if (comparison != 0) { 492 return this; 493 } 494 comparison = NumberUtils.compare(lhs, rhs); 495 return this; 496 } 497 498 506 public CompareToBuilder append(boolean lhs, boolean rhs) { 507 if (comparison != 0) { 508 return this; 509 } 510 if (lhs == rhs) { 511 return this; 512 } 513 if (lhs == false) { 514 comparison = -1; 515 } else { 516 comparison = +1; 517 } 518 return this; 519 } 520 521 542 public CompareToBuilder append(Object [] lhs, Object [] rhs) { 543 return append(lhs, rhs, null); 544 } 545 546 569 public CompareToBuilder append(Object [] lhs, Object [] rhs, Comparator comparator) { 570 if (comparison != 0) { 571 return this; 572 } 573 if (lhs == rhs) { 574 return this; 575 } 576 if (lhs == null) { 577 comparison = -1; 578 return this; 579 } 580 if (rhs == null) { 581 comparison = +1; 582 return this; 583 } 584 if (lhs.length != rhs.length) { 585 comparison = (lhs.length < rhs.length) ? -1 : +1; 586 return this; 587 } 588 for (int i = 0; i < lhs.length && comparison == 0; i++) { 589 append(lhs[i], rhs[i], comparator); 590 } 591 return this; 592 } 593 594 609 public CompareToBuilder append(long[] lhs, long[] rhs) { 610 if (comparison != 0) { 611 return this; 612 } 613 if (lhs == rhs) { 614 return this; 615 } 616 if (lhs == null) { 617 comparison = -1; 618 return this; 619 } 620 if (rhs == null) { 621 comparison = +1; 622 return this; 623 } 624 if (lhs.length != rhs.length) { 625 comparison = (lhs.length < rhs.length) ? -1 : +1; 626 return this; 627 } 628 for (int i = 0; i < lhs.length && comparison == 0; i++) { 629 append(lhs[i], rhs[i]); 630 } 631 return this; 632 } 633 634 649 public CompareToBuilder append(int[] lhs, int[] rhs) { 650 if (comparison != 0) { 651 return this; 652 } 653 if (lhs == rhs) { 654 return this; 655 } 656 if (lhs == null) { 657 comparison = -1; 658 return this; 659 } 660 if (rhs == null) { 661 comparison = +1; 662 return this; 663 } 664 if (lhs.length != rhs.length) { 665 comparison = (lhs.length < rhs.length) ? -1 : +1; 666 return this; 667 } 668 for (int i = 0; i < lhs.length && comparison == 0; i++) { 669 append(lhs[i], rhs[i]); 670 } 671 return this; 672 } 673 674 689 public CompareToBuilder append(short[] lhs, short[] rhs) { 690 if (comparison != 0) { 691 return this; 692 } 693 if (lhs == rhs) { 694 return this; 695 } 696 if (lhs == null) { 697 comparison = -1; 698 return this; 699 } 700 if (rhs == null) { 701 comparison = +1; 702 return this; 703 } 704 if (lhs.length != rhs.length) { 705 comparison = (lhs.length < rhs.length) ? -1 : +1; 706 return this; 707 } 708 for (int i = 0; i < lhs.length && comparison == 0; i++) { 709 append(lhs[i], rhs[i]); 710 } 711 return this; 712 } 713 714 729 public CompareToBuilder append(char[] lhs, char[] rhs) { 730 if (comparison != 0) { 731 return this; 732 } 733 if (lhs == rhs) { 734 return this; 735 } 736 if (lhs == null) { 737 comparison = -1; 738 return this; 739 } 740 if (rhs == null) { 741 comparison = +1; 742 return this; 743 } 744 if (lhs.length != rhs.length) { 745 comparison = (lhs.length < rhs.length) ? -1 : +1; 746 return this; 747 } 748 for (int i = 0; i < lhs.length && comparison == 0; i++) { 749 append(lhs[i], rhs[i]); 750 } 751 return this; 752 } 753 754 769 public CompareToBuilder append(byte[] lhs, byte[] rhs) { 770 if (comparison != 0) { 771 return this; 772 } 773 if (lhs == rhs) { 774 return this; 775 } 776 if (lhs == null) { 777 comparison = -1; 778 return this; 779 } 780 if (rhs == null) { 781 comparison = +1; 782 return this; 783 } 784 if (lhs.length != rhs.length) { 785 comparison = (lhs.length < rhs.length) ? -1 : +1; 786 return this; 787 } 788 for (int i = 0; i < lhs.length && comparison == 0; i++) { 789 append(lhs[i], rhs[i]); 790 } 791 return this; 792 } 793 794 809 public CompareToBuilder append(double[] lhs, double[] rhs) { 810 if (comparison != 0) { 811 return this; 812 } 813 if (lhs == rhs) { 814 return this; 815 } 816 if (lhs == null) { 817 comparison = -1; 818 return this; 819 } 820 if (rhs == null) { 821 comparison = +1; 822 return this; 823 } 824 if (lhs.length != rhs.length) { 825 comparison = (lhs.length < rhs.length) ? -1 : +1; 826 return this; 827 } 828 for (int i = 0; i < lhs.length && comparison == 0; i++) { 829 append(lhs[i], rhs[i]); 830 } 831 return this; 832 } 833 834 849 public CompareToBuilder append(float[] lhs, float[] rhs) { 850 if (comparison != 0) { 851 return this; 852 } 853 if (lhs == rhs) { 854 return this; 855 } 856 if (lhs == null) { 857 comparison = -1; 858 return this; 859 } 860 if (rhs == null) { 861 comparison = +1; 862 return this; 863 } 864 if (lhs.length != rhs.length) { 865 comparison = (lhs.length < rhs.length) ? -1 : +1; 866 return this; 867 } 868 for (int i = 0; i < lhs.length && comparison == 0; i++) { 869 append(lhs[i], rhs[i]); 870 } 871 return this; 872 } 873 874 889 public CompareToBuilder append(boolean[] lhs, boolean[] rhs) { 890 if (comparison != 0) { 891 return this; 892 } 893 if (lhs == rhs) { 894 return this; 895 } 896 if (lhs == null) { 897 comparison = -1; 898 return this; 899 } 900 if (rhs == null) { 901 comparison = +1; 902 return this; 903 } 904 if (lhs.length != rhs.length) { 905 comparison = (lhs.length < rhs.length) ? -1 : +1; 906 return this; 907 } 908 for (int i = 0; i < lhs.length && comparison == 0; i++) { 909 append(lhs[i], rhs[i]); 910 } 911 return this; 912 } 913 914 923 public int toComparison() { 924 return comparison; 925 } 926 927 } 928 929 | Popular Tags |