You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CompareUtil.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Common Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/cpl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.testing.compare;
  14. import java.util.Iterator;
  15. import java.util.Collection;
  16. import java.util.List;
  17. /** Minor methods for short-circuiting comparisons */
  18. public class CompareUtil {
  19. /**
  20. * Callers may abort equality checks with false
  21. * if this passes its misc short-circuit semantics.
  22. * A false result does not mean the arguments
  23. * are equal, but a true result does mean they are not equal.
  24. * <pre>if (notSame(foo,bar,true)) then return false;</pre>
  25. * @param lhs the Object on the left-hand-side to compare
  26. * @param rhs the Object on the right-hand-side to compare
  27. * @param considerType if true, then also return true if the
  28. * right-hand-side cannot be assigned to the left-hand-side
  29. * @return true if lhs and rhs are not the same per considerType.
  30. */
  31. public static boolean notSame(Object lhs, Object rhs, boolean considerType) {
  32. if (null == lhs) {
  33. return (!(null == rhs));
  34. } else if (null == rhs) {
  35. return true;
  36. } else if (lhs== rhs) {
  37. return false; // known to be same
  38. } else if (considerType) {
  39. Class lhClass = lhs.getClass();
  40. Class rhClass = rhs.getClass();
  41. if (!lhClass.isAssignableFrom(rhClass)) {
  42. return true;
  43. }
  44. }
  45. return false; // unknown whether equal or not
  46. }
  47. /**
  48. * Return null/equal comparison:
  49. * <li>null considered to be lesser</li>
  50. * <li>reference or Object.equals() considered to be 0</li>
  51. * <li>return Integer.MAX_VALUE for all other cases</li>
  52. * <table>
  53. * <tr><td>result</td><td>input</td></tr>
  54. * <tr><td>-1</td><td>null &lt; rhs</td></tr>
  55. * <tr><td>1</td><td>lhs &gt; null</td></tr>
  56. * <tr><td>0</td><td>null == null</td></tr>
  57. * <tr><td>0</td><td>lhs == rhs</td></tr>
  58. * <tr><td>0</td><td>lhs.equals(rhs)</td></tr>
  59. * <tr><td>Integer.MAX_VALUE</td><td>{all other cases}</td></tr>
  60. * </table>
  61. * @see Comparator
  62. * @return Integer.MAX_VALUE if uncertain, value otherwise
  63. */
  64. public static int compare(Object lhs, Object rhs) {
  65. if (null == lhs) {
  66. return (null == rhs ? 0 : -1);
  67. } else if (null == rhs) {
  68. return 1;
  69. } else if (lhs == rhs) {
  70. return 0; // known to be same
  71. } else {
  72. return Integer.MAX_VALUE;
  73. }
  74. }
  75. /**
  76. * Return boolean comparison where true > false.
  77. * (Comparable not defined for Boolean)
  78. * @see Comparator
  79. */
  80. public static int compare(boolean lhs, boolean rhs) {
  81. return (lhs == rhs ? 0 : (lhs ? 1 : -1));
  82. }
  83. /**
  84. * Return String comparison based on {@link compare(Object,Object)}
  85. * and {@link String.compareTo(String)}.
  86. * @see Comparator
  87. */
  88. public static int compare(String lhs, String rhs) {
  89. int result = compare((Object) lhs, (Object) rhs);
  90. if (Integer.MAX_VALUE == result) {
  91. result = lhs.compareTo(rhs);
  92. }
  93. return result;
  94. }
  95. /**
  96. * Compare two Collections by reference to a standard List.
  97. * The first Collection to not contain a standard element
  98. * when the other does loses. Order is ignored.
  99. * The left-hand-side acts as the standard if the standard is null.
  100. * @param lhs the List from the left-hand-side
  101. * @param rhs the Collection from the right-hand-side
  102. * @param standard the List to act as the standard (if null, use lhs)
  103. * @param return -1 if lhs is null and rhs is not, 1 if reverse;
  104. * 0 if both have all elements in standard,
  105. * 1 if lhs has a standard element rhs does not, -1 if reverse
  106. * (testing in standard order)
  107. */
  108. public static int compare(List lhs, Collection rhs, List standard) {
  109. int result = compare(lhs, rhs);
  110. if (result == Integer.MAX_VALUE) {
  111. if (null == standard) {
  112. result = compare(lhs, rhs, lhs); // use lhs as standard
  113. } else {
  114. boolean leftHasThem = lhs.containsAll(standard);
  115. boolean rightHasThem = rhs.containsAll(standard);
  116. if (leftHasThem != rightHasThem) {
  117. result = (leftHasThem ? 1 : -1);
  118. } else if (leftHasThem) {
  119. result = 0; // they both have them
  120. } else { // first to not have an element loses
  121. Iterator standardIterator = standard.iterator();
  122. while (standardIterator.hasNext()) {
  123. Object standardObject = standardIterator.next();
  124. boolean leftHasIt = lhs.contains(standardObject);
  125. boolean rightHasIt = rhs.contains(standardObject);
  126. if (leftHasIt != rightHasIt) {
  127. result = (leftHasIt ? 1 : -1);
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. return result;
  135. }
  136. } // class Util