diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2014-06-01 11:02:18 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2014-06-01 11:02:18 +0200 |
commit | f8b3056dc4f61251aba7adf627c942c85e8618ca (patch) | |
tree | 8f58fc4d57d53cb791d027881fb8b8dd9cef11a1 /website/features/EqualsAndHashCode.html | |
parent | 627de194c03af3afa3478149dc777d2af4e9654b (diff) | |
download | lombok-f8b3056dc4f61251aba7adf627c942c85e8618ca.tar.gz lombok-f8b3056dc4f61251aba7adf627c942c85e8618ca.tar.bz2 lombok-f8b3056dc4f61251aba7adf627c942c85e8618ca.zip |
Fixed up and extended Tolerate with support for constructors, and added docs.
Diffstat (limited to 'website/features/EqualsAndHashCode.html')
-rw-r--r-- | website/features/EqualsAndHashCode.html | 34 |
1 files changed, 8 insertions, 26 deletions
diff --git a/website/features/EqualsAndHashCode.html b/website/features/EqualsAndHashCode.html index 89d71502..32dcb6c3 100644 --- a/website/features/EqualsAndHashCode.html +++ b/website/features/EqualsAndHashCode.html @@ -15,22 +15,11 @@ <div class="overview"> <h3>Overview</h3> <p> - Any class definition may be annotated with <code>@EqualsAndHashCode</code> to let lombok generate implementations of the - <code>equals(Object other)</code> and <code>hashCode()</code> methods. By default, it'll use all non-static, non-transient - fields, but you can exclude more fields by naming them in the optional <code>exclude</code> parameter to the annotation. - Alternatively, you can specify exactly which fields you wish to be used by naming them in the <code>of</code> parameter. + Any class definition may be annotated with <code>@EqualsAndHashCode</code> to let lombok generate implementations of the <code>equals(Object other)</code> and <code>hashCode()</code> methods. By default, it'll use all non-static, non-transient fields, but you can exclude more fields by naming them in the optional <code>exclude</code> parameter to the annotation. Alternatively, you can specify exactly which fields you wish to be used by naming them in the <code>of</code> parameter. </p><p> - By setting <code>callSuper</code> to <em>true</em>, you can include the <code>equals</code> and <code>hashCode</code> methods of your superclass in the generated methods. - For <code>hashCode</code>, the result of <code>super.hashCode()</code> is included in the hash algorithm, and for <code>equals</code>, the generated method will return - false if the super implementation thinks it is not equal to the passed in object. Be aware that not all <code>equals</code> implementations handle this situation properly. - However, lombok-generated <code>equals</code> implementations <strong>do</strong> handle this situation properly, so you can safely call your superclass equals if it, too, - has a lombok-generated <code>equals</code> method.<br /> + By setting <code>callSuper</code> to <em>true</em>, you can include the <code>equals</code> and <code>hashCode</code> methods of your superclass in the generated methods. For <code>hashCode</code>, the result of <code>super.hashCode()</code> is included in the hash algorithm, and for <code>equals</code>, the generated method will return false if the super implementation thinks it is not equal to the passed in object. Be aware that not all <code>equals</code> implementations handle this situation properly. However, lombok-generated <code>equals</code> implementations <strong>do</strong> handle this situation properly, so you can safely call your superclass equals if it, too, has a lombok-generated <code>equals</code> method.<br /> </p><p> - Setting <code>callSuper</code> to <em>true</em> when you don't extend anything (you extend <code>java.lang.Object</code>) is a compile-time error, because it would turn - the generated <code>equals()</code> and <code>hashCode()</code> implementations into having the same behaviour as simply inheriting these methods from <code>java.lang.Object</code>: - only the same object will be equal to each other and will have the same hashCode. - Not setting <code>callSuper</code> to <em>true</em> when you extend another class generates a warning, because unless the superclass has no (equality-important) fields, lombok - cannot generate an implementation for you that takes into account the fields declared by your superclasses. You'll need to write your own implementations, or rely on the + Setting <code>callSuper</code> to <em>true</em> when you don't extend anything (you extend <code>java.lang.Object</code>) is a compile-time error, because it would turn the generated <code>equals()</code> and <code>hashCode()</code> implementations into having the same behaviour as simply inheriting these methods from <code>java.lang.Object</code>: only the same object will be equal to each other and will have the same hashCode. Not setting <code>callSuper</code> to <em>true</em> when you extend another class generates a warning, because unless the superclass has no (equality-important) fields, lombok cannot generate an implementation for you that takes into account the fields declared by your superclasses. You'll need to write your own implementations, or rely on the <code>callSuper</code> chaining facility. </p><p> <em>NEW in Lombok 0.10: </em>Unless your class is <code>final</code> and extends <code>java.lang.Object</code>, lombok generates a <code>canEqual</code> method @@ -66,22 +55,15 @@ <div class="overview"> <h3>Small print</h3><div class="smallprint"> <p> - Arrays are 'deep' compared/hashCoded, which means that arrays that contain themselves will result in <code>StackOverflowError</code>s. However, - this behaviour is no different from e.g. <code>ArrayList</code>. + Arrays are 'deep' compared/hashCoded, which means that arrays that contain themselves will result in <code>StackOverflowError</code>s. However, this behaviour is no different from e.g. <code>ArrayList</code>. </p><p> - You may safely presume that the hashCode implementation used will not change between versions of lombok, however this guarantee is not set in stone; - if there's a significant performance improvement to be gained from using an alternate hash algorithm, that will be substituted in a future version. + You may safely presume that the hashCode implementation used will not change between versions of lombok, however this guarantee is not set in stone; if there's a significant performance improvement to be gained from using an alternate hash algorithm, that will be substituted in a future version. </p><p> - For the purposes of equality, 2 <code>NaN</code> (not a number) values for floats and doubles are considered equal, eventhough 'NaN == NaN' would - return false. This is analogous to <code>java.lang.Double</code>'s equals method, and is in fact required to ensure that comparing an object - to an exact copy of itself returns <code>true</code> for equality. + For the purposes of equality, 2 <code>NaN</code> (not a number) values for floats and doubles are considered equal, eventhough 'NaN == NaN' would return false. This is analogous to <code>java.lang.Double</code>'s equals method, and is in fact required to ensure that comparing an object to an exact copy of itself returns <code>true</code> for equality. </p><p> - If there is <em>any</em> method named either <code>hashCode</code> or <code>equals</code>, regardless of return type, no methods will be generated, and a warning is emitted instead. These 2 methods need to be in sync with - each other, which lombok cannot guarantee unless it generates all the methods, hence you always get a warning if one <em>or</em> both - of the methods already exist. + If there is <em>any</em> method named either <code>hashCode</code> or <code>equals</code>, regardless of return type, no methods will be generated, and a warning is emitted instead. These 2 methods need to be in sync with each other, which lombok cannot guarantee unless it generates all the methods, hence you always get a warning if one <em>or</em> both of the methods already exist. You can mark any method with <code>@lombok.experimental.Tolerate</code> to hide them from lombok. </p><p> - Attempting to exclude fields that don't exist or would have been excluded anyway (because they are static or transient) results in warnings on the named fields. - You therefore don't have to worry about typos. + Attempting to exclude fields that don't exist or would have been excluded anyway (because they are static or transient) results in warnings on the named fields. You therefore don't have to worry about typos. </p><p> Having both <code>exclude</code> and <code>of</code> generates a warning; the <code>exclude</code> parameter will be ignored in that case. </p><p> |