|
16 | 16 |
|
17 | 17 | package com.google.common.collect;
|
18 | 18 |
|
| 19 | +import static com.google.common.base.MoreObjects.toStringHelper; |
19 | 20 | import static com.google.common.truth.Truth.assertThat;
|
| 21 | +import static com.google.common.truth.Truth.assertWithMessage; |
| 22 | +import static java.lang.Integer.signum; |
| 23 | +import static java.util.Comparator.comparing; |
| 24 | +import static java.util.Comparator.naturalOrder; |
| 25 | +import static java.util.Comparator.nullsLast; |
20 | 26 |
|
21 | 27 | import com.google.common.annotations.GwtCompatible;
|
| 28 | +import com.google.common.primitives.Booleans; |
| 29 | +import java.util.Comparator; |
22 | 30 | import junit.framework.AssertionFailedError;
|
23 | 31 | import junit.framework.TestCase;
|
| 32 | +import org.checkerframework.checker.nullness.qual.Nullable; |
24 | 33 |
|
25 | 34 | /**
|
26 | 35 | * Unit test for {@link ComparisonChain}.
|
@@ -118,4 +127,95 @@ public void testCompareTrueFirst() {
|
118 | 127 | assertThat(ComparisonChain.start().compareTrueFirst(false, true).result()).isGreaterThan(0);
|
119 | 128 | assertThat(ComparisonChain.start().compareTrueFirst(false, false).result()).isEqualTo(0);
|
120 | 129 | }
|
| 130 | + |
| 131 | + enum TriState { |
| 132 | + FALSE, |
| 133 | + MAYBE, |
| 134 | + TRUE, |
| 135 | + } |
| 136 | + |
| 137 | + static class Foo { |
| 138 | + private final String aString; |
| 139 | + private final int anInt; |
| 140 | + private final @Nullable TriState anEnum; |
| 141 | + |
| 142 | + Foo(String aString, int anInt, @Nullable TriState anEnum) { |
| 143 | + this.aString = aString; |
| 144 | + this.anInt = anInt; |
| 145 | + this.anEnum = anEnum; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public String toString() { |
| 150 | + return toStringHelper(this) |
| 151 | + .add("aString", aString) |
| 152 | + .add("anInt", anInt) |
| 153 | + .add("anEnum", anEnum) |
| 154 | + .toString(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /** Validates that the Comparator equivalent we document is correct. */ |
| 159 | + public void testComparatorEquivalent() { |
| 160 | + Comparator<Foo> comparatorUsingComparisonChain = |
| 161 | + (a, b) -> |
| 162 | + ComparisonChain.start() |
| 163 | + .compare(a.aString, b.aString) |
| 164 | + .compare(a.anInt, b.anInt) |
| 165 | + .compare(a.anEnum, b.anEnum, Ordering.natural().nullsLast()) |
| 166 | + .result(); |
| 167 | + Comparator<Foo> comparatorUsingComparatorMethods = |
| 168 | + comparing((Foo foo) -> foo.aString) |
| 169 | + .thenComparing(foo -> foo.anInt) |
| 170 | + .thenComparing(foo -> foo.anEnum, nullsLast(naturalOrder())); |
| 171 | + ImmutableList<Foo> instances = |
| 172 | + ImmutableList.of( |
| 173 | + new Foo("a", 1, TriState.TRUE), |
| 174 | + new Foo("a", 2, TriState.TRUE), |
| 175 | + new Foo("b", 1, TriState.FALSE), |
| 176 | + new Foo("b", 1, TriState.TRUE), |
| 177 | + new Foo("b", 1, null)); |
| 178 | + for (Foo a : instances) { |
| 179 | + for (Foo b : instances) { |
| 180 | + int comparedUsingComparisonChain = signum(comparatorUsingComparisonChain.compare(a, b)); |
| 181 | + int comparedUsingComparatorMethods = signum(comparatorUsingComparatorMethods.compare(a, b)); |
| 182 | + assertWithMessage("%s vs %s", a, b) |
| 183 | + .that(comparedUsingComparatorMethods) |
| 184 | + .isEqualTo(comparedUsingComparisonChain); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + static class Bar { |
| 190 | + private final boolean isBaz; |
| 191 | + |
| 192 | + Bar(boolean isBaz) { |
| 193 | + this.isBaz = isBaz; |
| 194 | + } |
| 195 | + |
| 196 | + boolean isBaz() { |
| 197 | + return isBaz; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Validates that {@link Booleans#trueFirst()} and {@link Booleans#falseFirst()} can be used with |
| 203 | + * {@link Comparator} when replacing {@link ComparisonChain#compareTrueFirst} and {@link |
| 204 | + * ComparisonChain#compareFalseFirst}, as we document. |
| 205 | + */ |
| 206 | + public void testTrueFirstFalseFirst() { |
| 207 | + Bar trueBar = new Bar(true); |
| 208 | + Bar falseBar = new Bar(false); |
| 209 | + |
| 210 | + assertThat(ComparisonChain.start().compareTrueFirst(trueBar.isBaz(), falseBar.isBaz()).result()) |
| 211 | + .isLessThan(0); |
| 212 | + Comparator<Bar> trueFirstComparator = comparing(Bar::isBaz, Booleans.trueFirst()); |
| 213 | + assertThat(trueFirstComparator.compare(trueBar, falseBar)).isLessThan(0); |
| 214 | + |
| 215 | + assertThat( |
| 216 | + ComparisonChain.start().compareFalseFirst(falseBar.isBaz(), trueBar.isBaz()).result()) |
| 217 | + .isLessThan(0); |
| 218 | + Comparator<Bar> falseFirstComparator = comparing(Bar::isBaz, Booleans.falseFirst()); |
| 219 | + assertThat(falseFirstComparator.compare(falseBar, trueBar)).isLessThan(0); |
| 220 | + } |
121 | 221 | }
|
0 commit comments