51
51
* should have any of the other properties of an equivalence relation (reflexivity, symmetry, or
52
52
* transitivity).
53
53
*
54
- * <p>Subclasses may optionally override {@link #formatDiff}. This results in failure messages
55
- * including formatted diffs between expected and actual elements, where possible.
54
+ * <p>Optionally, instances of this class can also provide functionality to format the difference
55
+ * between values which do not correspond. This results in failure messages including formatted
56
+ * diffs between expected and actual value, where possible.
57
+ *
58
+ * <p>The recommended approach for creating an instance of this class is to use one of the static
59
+ * factory methods. The most general of these is {@link #from}; the other methods are more
60
+ * convenient in specific cases. The optional diff-formatting functionality can be added using
61
+ * {@link #formattingDiffsUsing}. (Alternatively, you can subclass this class yourself, but that is
62
+ * generally not recommended.)
56
63
*
57
64
* <p>Instances of this are typically used via {@link IterableSubject#comparingElementsUsing},
58
65
* {@link MapSubject#comparingValuesUsing}, or {@link MultimapSubject#comparingValuesUsing}.
59
66
*
60
67
* @author Pete Gillin
61
68
*/
62
- // TODO(b/119038898): Once there is little reason to prefer subclassing to factory methods, change
63
- // the class-level javadoc to point folks at the factory methods and drop references to subclassing.
64
69
public abstract class Correspondence <A , E > {
65
70
66
71
/**
@@ -87,11 +92,11 @@ public abstract class Correspondence<A, E> {
87
92
*
88
93
* <pre>{@code
89
94
* class MyRecordTestHelper {
95
+ * static final Correspondence<MyRecord, MyRecord> EQUIVALENCE =
96
+ * Correspondence.from(MyRecordTestHelper::recordsEquivalent, "is equivalent to");
90
97
* static boolean recordsEquivalent(@Nullable MyRecord actual, @Nullable MyRecord expected) {
91
98
* // code to check whether records should be considered equivalent for testing purposes
92
99
* }
93
- * static final Correspondence<MyRecord, MyRecord> EQUIVALENCE =
94
- * Correspondence.from(MyRecordTestHelper::recordsEquivalent, "is equivalent to");
95
100
* }
96
101
* }</pre>
97
102
*
@@ -308,8 +313,12 @@ public String toString() {
308
313
}
309
314
}
310
315
311
- // TODO(b/119038898): Once all functionality is available via factory methods, consider explicitly
312
- // adding the no-arg constructor here, and use its javadoc to discourage subclassing.
316
+ /**
317
+ * Constructor. Creating subclasses (anonymous or otherwise) of this class is <i>not
318
+ * recommended</i>, but is possible via this constructor. The recommended approach is to use the
319
+ * factory methods instead (see {@linkplain Correspondence class-level documentation}).
320
+ */
321
+ protected Correspondence () {}
313
322
314
323
/**
315
324
* Returns a new correspondence which is like this one, except that the given formatter may be
@@ -326,15 +335,15 @@ public String toString() {
326
335
*
327
336
* <pre>{@code
328
337
* class MyRecordTestHelper {
338
+ * static final Correspondence<MyRecord, MyRecord> EQUIVALENCE =
339
+ * Correspondence.from(MyRecordTestHelper::recordsEquivalent, "is equivalent to")
340
+ * .formattingDiffsUsing(MyRecordTestHelper::formatRecordDiff);
329
341
* static boolean recordsEquivalent(@Nullable MyRecord actual, @Nullable MyRecord expected) {
330
342
* // code to check whether records should be considered equivalent for testing purposes
331
343
* }
332
344
* static String formatRecordDiff(@Nullable MyRecord actual, @Nullable MyRecord expected) {
333
345
* // code to format the diff between the records
334
346
* }
335
- * static final Correspondence<MyRecord, MyRecord> EQUIVALENCE =
336
- * Correspondence.from(MyRecordTestHelper::recordsEquivalent, "is equivalent to")
337
- * .formattingDiffsUsing(MyRecordTestHelper::formatRecordDiff);
338
347
* }
339
348
* }</pre>
340
349
*/
@@ -685,7 +694,8 @@ final boolean safeCompare(
685
694
* expected} values, if possible, or {@code null} if not.
686
695
*
687
696
* <p>The implementation on the {@link Correspondence} base class always returns {@code null}. To
688
- * enable diffing, subclasses should override this method.
697
+ * enable diffing, use {@link #formattingDiffsUsing} (or override this method in a subclass, but
698
+ * factory methods are recommended over subclassing).
689
699
*
690
700
* <p>Assertions should only invoke this with parameters for which {@link #compare} returns {@code
691
701
* false}.
0 commit comments