Skip to content

Commit 8bd19b4

Browse files
petegronshapiro
authored andcommitted
Change the javadoc of Correspondence and fuzzy.md to recommend factory methods over subclassing.
The empty constructor is explicitly added just so that we can add javadoc recommending against it. (We could deprecate it, but I don't think we want to go that far, and anyway I think it probably wouldn't do much good as folks shouldn't be explicitly calling it.) Also includes some minor improvements such as adding more links to fuzzy.md and reordering the code in the examples in javadoc more logically. RELNOTES=The officially recommended way of creating Correspondence instances is now to use the factory methods, rather than subclassing. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=238501597
1 parent d222360 commit 8bd19b4

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

core/src/main/java/com/google/common/truth/Correspondence.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,21 @@
5151
* should have any of the other properties of an equivalence relation (reflexivity, symmetry, or
5252
* transitivity).
5353
*
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.)
5663
*
5764
* <p>Instances of this are typically used via {@link IterableSubject#comparingElementsUsing},
5865
* {@link MapSubject#comparingValuesUsing}, or {@link MultimapSubject#comparingValuesUsing}.
5966
*
6067
* @author Pete Gillin
6168
*/
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.
6469
public abstract class Correspondence<A, E> {
6570

6671
/**
@@ -87,11 +92,11 @@ public abstract class Correspondence<A, E> {
8792
*
8893
* <pre>{@code
8994
* class MyRecordTestHelper {
95+
* static final Correspondence<MyRecord, MyRecord> EQUIVALENCE =
96+
* Correspondence.from(MyRecordTestHelper::recordsEquivalent, "is equivalent to");
9097
* static boolean recordsEquivalent(@Nullable MyRecord actual, @Nullable MyRecord expected) {
9198
* // code to check whether records should be considered equivalent for testing purposes
9299
* }
93-
* static final Correspondence<MyRecord, MyRecord> EQUIVALENCE =
94-
* Correspondence.from(MyRecordTestHelper::recordsEquivalent, "is equivalent to");
95100
* }
96101
* }</pre>
97102
*
@@ -308,8 +313,12 @@ public String toString() {
308313
}
309314
}
310315

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() {}
313322

314323
/**
315324
* Returns a new correspondence which is like this one, except that the given formatter may be
@@ -326,15 +335,15 @@ public String toString() {
326335
*
327336
* <pre>{@code
328337
* class MyRecordTestHelper {
338+
* static final Correspondence<MyRecord, MyRecord> EQUIVALENCE =
339+
* Correspondence.from(MyRecordTestHelper::recordsEquivalent, "is equivalent to")
340+
* .formattingDiffsUsing(MyRecordTestHelper::formatRecordDiff);
329341
* static boolean recordsEquivalent(@Nullable MyRecord actual, @Nullable MyRecord expected) {
330342
* // code to check whether records should be considered equivalent for testing purposes
331343
* }
332344
* static String formatRecordDiff(@Nullable MyRecord actual, @Nullable MyRecord expected) {
333345
* // code to format the diff between the records
334346
* }
335-
* static final Correspondence<MyRecord, MyRecord> EQUIVALENCE =
336-
* Correspondence.from(MyRecordTestHelper::recordsEquivalent, "is equivalent to")
337-
* .formattingDiffsUsing(MyRecordTestHelper::formatRecordDiff);
338347
* }
339348
* }</pre>
340349
*/
@@ -685,7 +694,8 @@ final boolean safeCompare(
685694
* expected} values, if possible, or {@code null} if not.
686695
*
687696
* <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).
689699
*
690700
* <p>Assertions should only invoke this with parameters for which {@link #compare} returns {@code
691701
* false}.

0 commit comments

Comments
 (0)