|
| 1 | +package org.hamcrest.comparator; |
| 2 | + |
| 3 | +import org.hamcrest.Description; |
| 4 | +import org.hamcrest.Factory; |
| 5 | +import org.hamcrest.Matcher; |
| 6 | +import org.hamcrest.TypeSafeMatcher; |
| 7 | + |
| 8 | +import java.util.Comparator; |
| 9 | + |
| 10 | +import static java.lang.Integer.signum; |
| 11 | + |
| 12 | +public final class ComparatorMatcherBuilder<T> { |
| 13 | + |
| 14 | + private final Comparator<T> comparator; |
| 15 | + private final boolean includeComparatorInDescription; |
| 16 | + |
| 17 | + /** |
| 18 | + * Creates a matcher factory for matchers of {@code Comparable}s. |
| 19 | + * <p/> |
| 20 | + * For example: |
| 21 | + * <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingNaturalOrdering().lessThanOrEqualTo(1))</pre> |
| 22 | + */ |
| 23 | + public static <T extends Comparable<T>> ComparatorMatcherBuilder<T> usingNaturalOrdering() { |
| 24 | + return new ComparatorMatcherBuilder<T>(new Comparator<T>() { |
| 25 | + @Override |
| 26 | + public int compare(T o1, T o2) { |
| 27 | + return o1.compareTo(o2); |
| 28 | + } |
| 29 | + }, false); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Creates a matcher factory for matchers of {@code Comparators}s of {@code T}. |
| 34 | + * <p/> |
| 35 | + * For example: |
| 36 | + * <pre>assertThat(5, comparedBy(new Comparator<Integer>() { |
| 37 | + * public int compare(Integer o1, Integer o2) { |
| 38 | + * return -o1.compareTo(o2); |
| 39 | + * } |
| 40 | + * }).lessThan(4))</pre> |
| 41 | + */ |
| 42 | + public static <T> ComparatorMatcherBuilder<T> comparedBy(Comparator<T> comparator) { |
| 43 | + return new ComparatorMatcherBuilder<T>(comparator, true); |
| 44 | + } |
| 45 | + |
| 46 | + private ComparatorMatcherBuilder(Comparator<T> comparator, boolean includeComparatorInDescription) { |
| 47 | + this.comparator = comparator; |
| 48 | + this.includeComparatorInDescription = includeComparatorInDescription; |
| 49 | + } |
| 50 | + |
| 51 | + private static final class ComparatorMatcher<T> extends TypeSafeMatcher<T> { |
| 52 | + private static final int LESS_THAN = -1; |
| 53 | + private static final int GREATER_THAN = 1; |
| 54 | + private static final int EQUAL = 0; |
| 55 | + |
| 56 | + private final Comparator<T> comparator; |
| 57 | + private final T expected; |
| 58 | + private final int minCompare; |
| 59 | + private final int maxCompare; |
| 60 | + private final boolean includeComparatorInDescription; |
| 61 | + |
| 62 | + private static final String[] comparisonDescriptions = { |
| 63 | + "less than", |
| 64 | + "equal to", |
| 65 | + "greater than" |
| 66 | + }; |
| 67 | + |
| 68 | + private ComparatorMatcher(Comparator<T> comparator, T expected, int minCompare, int maxCompare, boolean includeComparatorInDescription) { |
| 69 | + this.comparator = comparator; |
| 70 | + this.expected = expected; |
| 71 | + this.minCompare = minCompare; |
| 72 | + this.maxCompare = maxCompare; |
| 73 | + this.includeComparatorInDescription = includeComparatorInDescription; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean matchesSafely(T actual) { |
| 78 | + try { |
| 79 | + int compare = signum(comparator.compare(actual, expected)); |
| 80 | + return minCompare <= compare && compare <= maxCompare; |
| 81 | + } catch (ClassCastException e) { |
| 82 | + return false; // type erasure means someone can shonk in a non-T :( |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void describeMismatchSafely(T actual, Description mismatchDescription) { |
| 88 | + mismatchDescription.appendValue(actual).appendText(" was ") |
| 89 | + .appendText(asText(comparator.compare(actual, expected))) |
| 90 | + .appendText(" ").appendValue(expected); |
| 91 | + if (includeComparatorInDescription) { |
| 92 | + mismatchDescription.appendText(" when compared by ").appendValue(comparator); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void describeTo(Description description) { |
| 98 | + description.appendText("a value ").appendText(asText(minCompare)); |
| 99 | + if (minCompare != maxCompare) { |
| 100 | + description.appendText(" or ").appendText(asText(maxCompare)); |
| 101 | + } |
| 102 | + description.appendText(" ").appendValue(expected); |
| 103 | + if (includeComparatorInDescription) { |
| 104 | + description.appendText(" when compared by ").appendValue(comparator); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static String asText(int comparison) { |
| 109 | + return comparisonDescriptions[signum(comparison) + 1]; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Creates a matcher of {@code T} object that matches when the examined object is |
| 115 | + * equal to the specified value, as reported by the {@code Comparator} used to |
| 116 | + * create this builder. |
| 117 | + * <p/> |
| 118 | + * For example: |
| 119 | + * <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingNaturalOrdering().comparesEqualTo(1))</pre> |
| 120 | + * |
| 121 | + * @param value the value which, when passed to the Comparator supplied to this builder, should return zero |
| 122 | + */ |
| 123 | + @Factory |
| 124 | + public Matcher<T> comparesEqualTo(T value) { |
| 125 | + return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.EQUAL, ComparatorMatcher.EQUAL, includeComparatorInDescription); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Creates a matcher of {@code T} object that matches when the examined object is |
| 130 | + * greater than the specified value, as reported by the {@code Comparator} used to |
| 131 | + * create this builder. |
| 132 | + * <p/> |
| 133 | + * For example: |
| 134 | + * <pre>assertThat(2, ComparatorMatcherBuilder.<Integer>usingNaturalOrdering().greaterThan(1))</pre> |
| 135 | + * |
| 136 | + * @param value the value which, when passed to the Comparator supplied to this builder, should return greater |
| 137 | + * than zero |
| 138 | + */ |
| 139 | + @Factory |
| 140 | + public Matcher<T> greaterThan(T value) { |
| 141 | + return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.GREATER_THAN, ComparatorMatcher.GREATER_THAN, includeComparatorInDescription); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Creates a matcher of {@code T} object that matches when the examined object is |
| 146 | + * greater than or equal to the specified value, as reported by the {@code Comparator} used to |
| 147 | + * create this builder. |
| 148 | + * <p/> |
| 149 | + * For example: |
| 150 | + * <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingNaturalOrdering().greaterThanOrEqualTo(1))</pre> |
| 151 | + * |
| 152 | + * @param value the value which, when passed to the Comparator supplied to this builder, should return greater |
| 153 | + * than or equal to zero |
| 154 | + */ |
| 155 | + @Factory |
| 156 | + public Matcher<T> greaterThanOrEqualTo(T value) { |
| 157 | + return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.EQUAL, ComparatorMatcher.GREATER_THAN, includeComparatorInDescription); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Creates a matcher of {@code T} object that matches when the examined object is |
| 162 | + * less than the specified value, as reported by the {@code Comparator} used to |
| 163 | + * create this builder. |
| 164 | + * <p/> |
| 165 | + * For example: |
| 166 | + * <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingNaturalOrdering().lessThan(2))</pre> |
| 167 | + * |
| 168 | + * @param value the value which, when passed to the Comparator supplied to this builder, should return less |
| 169 | + * than zero |
| 170 | + */ |
| 171 | + @Factory |
| 172 | + public Matcher<T> lessThan(T value) { |
| 173 | + return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.LESS_THAN, ComparatorMatcher.LESS_THAN, includeComparatorInDescription); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Creates a matcher of {@code T} object that matches when the examined object is |
| 178 | + * less than or equal to the specified value, as reported by the {@code Comparator} used to |
| 179 | + * create this builder. |
| 180 | + * <p/> |
| 181 | + * For example: |
| 182 | + * <pre>assertThat(1, ComparatorMatcherBuilder.<Integer>usingNaturalOrdering().lessThanOrEqualTo(1))</pre> |
| 183 | + * |
| 184 | + * @param value the value which, when passed to the Comparator supplied to this builder, should return less |
| 185 | + * than or equal to zero |
| 186 | + */ |
| 187 | + @Factory |
| 188 | + public Matcher<T> lessThanOrEqualTo(T value) { |
| 189 | + return new ComparatorMatcher<T>(comparator, value, ComparatorMatcher.LESS_THAN, ComparatorMatcher.EQUAL, includeComparatorInDescription); |
| 190 | + } |
| 191 | +} |
0 commit comments