Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit b6ab5cf

Browse files
authored
#414 JavaDoc/README for new IgnoredByEquals and NotInToString annotations
2 parents 008e1d7 + 3ae58a5 commit b6ab5cf

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,13 @@ abstract class Person {
506506

507507
Note that it is a compile error to leave hashCode abstract if equals is implemented, and vice versa, as FreeBuilder has no reasonable way to ensure the consistency of any implementation it might generate.
508508

509+
If you have a small set of properties you wish to exclude from equals or toString without losing the generated code entirely, you can annotate them `@IgnoredByEquals` and/or `@NotInToString`.
510+
509511
**Warning:**
510512
It is rarely a good idea to redefine equality on a value type, as it makes testing very hard.
511513
For instance, `assertEquals` in JUnit relies on equality; it will not know to check individual fields, and as a result, tests may be failing to catch bugs that, on the face of it, they looks like they should be.
512514
If you are only testing a subset of your fields for equality, consider separating your class in two, as you may have accidentally combined the key and the value of a map into a single object, and you may find your code becomes healthier after the separation.
515+
Alternatively, creating a custom [Comparator] will make it explicit that you are not using the natural definition of equality.
513516

514517
### Custom functional interfaces
515518

src/main/java/org/inferred/freebuilder/IgnoredByEquals.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,22 @@
44
import java.lang.annotation.Retention;
55
import java.lang.annotation.RetentionPolicy;
66
import java.lang.annotation.Target;
7+
import java.util.Comparator;
8+
import java.util.Map;
9+
import java.util.Set;
710

811
/**
9-
* If present, do not include this property in the generated
10-
* {@link Object#equals(Object)} and {@link Object#hashCode()}.
12+
* {@link FreeBuilder} will not check properties annotated {@code @IgnoredByEquals} when comparing
13+
* objects in its generated {@link Object#equals(Object)}.
14+
*
15+
* <p>To maintain the contract of {@link Object#hashCode()}), the value of these properties will
16+
* also be ignored in the generated implementation of that method.
17+
*
18+
* <p><b>Warning</b>: Dropping properties from equality checks makes it very easy to accidentally
19+
* write broken unit tests (and hard to write good ones). If you find yourself wanting to use
20+
* this annotation, consider first whether you actually want a different collection type
21+
* (typicaly a {@link Map} rather than a {@link Set}, for instance), or whether you can use an
22+
* explicit field-ignoring {@link Comparator} in the parts of the code that need it.
1123
*/
1224
@Target(ElementType.METHOD)
1325
@Retention(RetentionPolicy.SOURCE)

src/main/java/org/inferred/freebuilder/NotInToString.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import java.lang.annotation.Target;
77

88
/**
9-
* If present, do not include this property in the generated {@link Object#toString()}.
9+
* {@link FreeBuilder} will not include properties annotated {@code @NotInToString} in the output of
10+
* its generated {@link Object#toString()} implementation.
1011
*/
1112
@Target(ElementType.METHOD)
1213
@Retention(RetentionPolicy.SOURCE)

0 commit comments

Comments
 (0)