Issue 625: use (even) better primes for hashcodes

This commit is contained in:
Roel Spilker committed 2014-01-19 21:49:35 +01:00
1 parent 2ab6cc809d
commit 14cc545276
34 files changed
+82 -65

No files matched your search

+1
View File
@@ -3,6 +3,7 @@ Lombok Changelog
### v1.12.5 "Edgy Guinea Pig"
* DETAIL: {Delombok} Inside enum bodies the delombok formatter didn't respect the emptyLines directive [Issue #529](https://code.google.com/p/projectlombok/issues/detail?id=629).
* DETAIL: Use smaller primes (<127) for generating hashcodes [Issue #625](https://code.google.com/p/projectlombok/issues/detail?id=625)
### v1.12.4 (January 15th, 2014)
* BUGFIX: v1.12.2's delombok turns all operator+assignments into just assignment. Fixed. [Issue #598](https://code.google.com/p/projectlombok/issues/detail?id=598)
+11 -3
View File
@@ -27,9 +27,17 @@ import lombok.core.LombokNode;
public class HandlerUtil {
private HandlerUtil() {}
public static final int PRIME_FOR_HASHCODE = 277;
public static final int PRIME_FOR_TRUE = 2591;
public static final int PRIME_FOR_FALSE = 2609;
public static int primeForHashcode() {
return 59;
}
public static int primeForTrue() {
return 79;
}
public static int primeForFalse() {
return 97;
}
/** Checks if the given name is a valid identifier.
*
@@ -272,7 +272,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH
final boolean isEmpty = fields.isEmpty();
/* final int PRIME = 277; */ {
/* final int PRIME = X; */ {
/* Without fields, PRIME isn't used, and that would trigger a 'local variable not used' warning. */
if (!isEmpty || callSuper) {
LocalDeclaration primeDecl = new LocalDeclaration(PRIME, pS, pE);
@@ -281,7 +281,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH
primeDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0);
primeDecl.type.sourceStart = pS; primeDecl.type.sourceEnd = pE;
setGeneratedBy(primeDecl.type, source);
primeDecl.initialization = makeIntLiteral(String.valueOf(HandlerUtil.PRIME_FOR_HASHCODE).toCharArray(), source);
primeDecl.initialization = makeIntLiteral(String.valueOf(HandlerUtil.primeForHashcode()).toCharArray(), source);
statements.add(primeDecl);
}
}
@@ -313,10 +313,10 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH
Expression fieldAccessor = createFieldAccessor(field, fieldAccess, source);
if (fType.dimensions() == 0 && token != null) {
if (Arrays.equals(TypeConstants.BOOLEAN, token)) {
/* booleanField ? 2591 : 2609 */
IntLiteral intFalse = makeIntLiteral(String.valueOf(HandlerUtil.PRIME_FOR_FALSE).toCharArray(), source);
IntLiteral intTrue = makeIntLiteral(String.valueOf(HandlerUtil.PRIME_FOR_TRUE).toCharArray(), source);
ConditionalExpression intForBool = new ConditionalExpression(fieldAccessor, intFalse, intTrue);
/* booleanField ? X : Y */
IntLiteral intTrue = makeIntLiteral(String.valueOf(HandlerUtil.primeForTrue()).toCharArray(), source);
IntLiteral intFalse = makeIntLiteral(String.valueOf(HandlerUtil.primeForFalse()).toCharArray(), source);
ConditionalExpression intForBool = new ConditionalExpression(fieldAccessor, intTrue, intFalse);
setGeneratedBy(intForBool, source);
statements.add(createResultCalculation(source, intForBool));
} else if (Arrays.equals(TypeConstants.LONG, token)) {
@@ -231,9 +231,9 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas
Name resultName = typeNode.toName(RESULT_NAME);
long finalFlag = JavacHandlerUtil.addFinalIfNeeded(0L, typeNode.getContext());
/* final int PRIME = 277; */ {
/* final int PRIME = X; */ {
if (!fields.isEmpty() || callSuper) {
statements.append(maker.VarDef(maker.Modifiers(finalFlag), primeName, maker.TypeIdent(CTC_INT), maker.Literal(HandlerUtil.PRIME_FOR_HASHCODE)));
statements.append(maker.VarDef(maker.Modifiers(finalFlag), primeName, maker.TypeIdent(CTC_INT), maker.Literal(HandlerUtil.primeForHashcode())));
}
}
@@ -255,9 +255,9 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas
if (fType instanceof JCPrimitiveTypeTree) {
switch (((JCPrimitiveTypeTree)fType).getPrimitiveTypeKind()) {
case BOOLEAN:
/* this.fieldName ? 2591 : 2609 */
/* this.fieldName ? X : Y */
statements.append(createResultCalculation(typeNode, maker.Conditional(fieldAccessor,
maker.Literal(HandlerUtil.PRIME_FOR_FALSE), maker.Literal(HandlerUtil.PRIME_FOR_TRUE))));
maker.Literal(HandlerUtil.primeForTrue()), maker.Literal(HandlerUtil.primeForFalse()))));
break;
case LONG: {
Name dollarFieldName = dollar.append(((JCVariableDecl)fieldNode.get()).name);
@@ -84,7 +84,7 @@ class AccessorsPrefix3 {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
final java.lang.Object $fName = this.getName();
result = result * PRIME + ($fName == null ? 0 : $fName.hashCode());
@@ -28,7 +28,7 @@ class DataExtended {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getX();
return result;
@@ -27,7 +27,7 @@ class DataIgnore {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getX();
return result;
@@ -39,7 +39,7 @@ class DataOnLocalClass1 {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getX();
final java.lang.Object $name = this.getName();
@@ -99,7 +99,7 @@ class DataOnLocalClass2 {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
final java.lang.Object $name = this.getName();
result = result * PRIME + ($name == null ? 0 : $name.hashCode());
@@ -136,7 +136,7 @@ class DataOnLocalClass2 {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getX();
return result;
@@ -38,7 +38,7 @@ class Data1 {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getX();
final java.lang.Object $name = this.getName();
@@ -91,7 +91,7 @@ class Data2 {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getX();
final java.lang.Object $name = this.getName();
@@ -139,7 +139,7 @@ final class Data3 {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getX();
final java.lang.Object $name = this.getName();
@@ -187,7 +187,7 @@ final class Data4 extends java.util.Timer {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + super.hashCode();
result = result * PRIME + this.getX();
@@ -36,7 +36,7 @@ class DataWithGetter {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getX();
result = result * PRIME + this.getY();
@@ -36,7 +36,7 @@ class DataWithGetterNone {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.x;
result = result * PRIME + this.y;
@@ -29,7 +29,7 @@ class EqualsAndHashCode {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.x;
result = result * PRIME + java.util.Arrays.hashCode(this.y);
@@ -46,6 +46,7 @@ final class EqualsAndHashCode2 {
long y;
float f;
double d;
boolean b;
@java.lang.Override
@java.lang.SuppressWarnings("all")
public boolean equals(final java.lang.Object o) {
@@ -56,12 +57,13 @@ final class EqualsAndHashCode2 {
if (this.y != other.y) return false;
if (java.lang.Float.compare(this.f, other.f) != 0) return false;
if (java.lang.Double.compare(this.d, other.d) != 0) return false;
if (this.b != other.b) return false;
return true;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.x;
final long $y = this.y;
@@ -69,6 +71,7 @@ final class EqualsAndHashCode2 {
result = result * PRIME + java.lang.Float.floatToIntBits(this.f);
final long $d = java.lang.Double.doubleToLongBits(this.d);
result = result * PRIME + (int)($d >>> 32 ^ $d);
result = result * PRIME + (this.b ? 79 : 97);
return result;
}
}
@@ -111,7 +114,7 @@ class EqualsAndHashCode4 extends EqualsAndHashCode {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + super.hashCode();
return result;
@@ -69,7 +69,7 @@ class EqualsAndHashCodeWithNoExistingMethods {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.x;
return result;
@@ -21,9 +21,9 @@ class GetterLazyBoolean {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + (this.isBooleanValue() ? 2609 : 2591);
result = result * PRIME + (this.isBooleanValue() ? 79 : 97);
return result;
}
@java.lang.Override
@@ -27,7 +27,7 @@ class GetterLazyEahcToString {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
final java.lang.Object $value = this.getValue();
result = result * PRIME + ($value == null ? 0 : $value.hashCode());
@@ -41,7 +41,7 @@ class GetterSetterJavadoc1 {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getFieldName();
return result;
@@ -30,7 +30,7 @@ final class Value1 {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getX();
final java.lang.Object $name = this.getName();
@@ -80,7 +80,7 @@ class Value2 {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getX();
final java.lang.Object $name = this.getName();
@@ -123,7 +123,7 @@ final class Value3 {
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getX();
result = result * PRIME + this.getY();
@@ -90,7 +90,7 @@ class AccessorsChain {
return (other instanceof AccessorsPrefix3);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
final java.lang.Object $fName = this.getName();
result = ((result * PRIME) + (($fName == null) ? 0 : $fName.hashCode()));
@@ -22,7 +22,7 @@
return (other instanceof DataExtended);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.getX());
return result;
@@ -20,7 +20,7 @@
return (other instanceof DataIgnore);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.getX());
return result;
@@ -36,7 +36,7 @@ class DataOnLocalClass1 {
return (other instanceof Local);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.getX());
final java.lang.Object $name = this.getName();
@@ -86,7 +86,7 @@ class DataOnLocalClass2 {
return (other instanceof Local.InnerLocal);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
final java.lang.Object $name = this.getName();
result = ((result * PRIME) + (($name == null) ? 0 : $name.hashCode()));
@@ -124,7 +124,7 @@ class DataOnLocalClass2 {
return (other instanceof Local);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.getX());
return result;
@@ -31,7 +31,7 @@ import lombok.Data;
return (other instanceof Data1);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.getX());
final java.lang.Object $name = this.getName();
@@ -78,7 +78,7 @@ import lombok.Data;
return (other instanceof Data2);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.getX());
final java.lang.Object $name = this.getName();
@@ -120,7 +120,7 @@ final @Data class Data3 {
return true;
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.getX());
final java.lang.Object $name = this.getName();
@@ -167,7 +167,7 @@ final @Data @lombok.EqualsAndHashCode(callSuper = true) class Data4 extends java
return (other instanceof Data4);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + super.hashCode());
result = ((result * PRIME) + this.getX());
@@ -30,7 +30,7 @@
return (other instanceof DataWithGetter);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.getX());
result = ((result * PRIME) + this.getY());
@@ -30,7 +30,7 @@
return (other instanceof DataWithGetterNone);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.x);
result = ((result * PRIME) + this.y);
@@ -35,7 +35,7 @@
return (other instanceof EqualsAndHashCode);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.x);
result = ((result * PRIME) + java.util.Arrays.hashCode(this.y));
@@ -52,6 +52,7 @@ final @lombok.EqualsAndHashCode class EqualsAndHashCode2 {
long y;
float f;
double d;
boolean b;
EqualsAndHashCode2() {
super();
}
@@ -69,10 +70,12 @@ final @lombok.EqualsAndHashCode class EqualsAndHashCode2 {
return false;
if ((java.lang.Double.compare(this.d, other.d) != 0))
return false;
if ((this.b != other.b))
return false;
return true;
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.x);
final long $y = this.y;
@@ -80,6 +83,7 @@ final @lombok.EqualsAndHashCode class EqualsAndHashCode2 {
result = ((result * PRIME) + java.lang.Float.floatToIntBits(this.f));
final long $d = java.lang.Double.doubleToLongBits(this.d);
result = ((result * PRIME) + (int) ($d ^ ($d >>> 32)));
result = ((result * PRIME) + (this.b ? 79 : 97));
return result;
}
}
@@ -125,7 +129,7 @@ final @lombok.EqualsAndHashCode(callSuper = false) class EqualsAndHashCode3 exte
return (other instanceof EqualsAndHashCode4);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + super.hashCode());
return result;
@@ -57,7 +57,7 @@ import static lombok.AccessLevel.NONE;
return (other instanceof EqualsAndHashCodeWithNoExistingMethods);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.x);
return result;
@@ -57,9 +57,9 @@
return (other instanceof GetterLazyBoolean);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + (this.isBooleanValue() ? 2609 : 2591));
result = ((result * PRIME) + (this.isBooleanValue() ? 79 : 97));
return result;
}
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
@@ -46,7 +46,7 @@
return (other instanceof GetterLazyEahcToString);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
final java.lang.Object $value = this.getValue();
result = ((result * PRIME) + (($value == null) ? 0 : $value.hashCode()));
@@ -22,7 +22,7 @@
return (other instanceof GetterSetterJavadoc1);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.getFieldName());
return result;
@@ -23,7 +23,7 @@ final @lombok.Value class Value1 {
return true;
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.getX());
final java.lang.Object $name = this.getName();
@@ -68,7 +68,7 @@ final @lombok.Value class Value1 {
return (other instanceof Value2);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.getX());
final java.lang.Object $name = this.getName();
@@ -106,7 +106,7 @@ final @Value class Value3 {
return true;
}
public @java.lang.Override @java.lang.SuppressWarnings("all") int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = ((result * PRIME) + this.getX());
result = ((result * PRIME) + this.getY());
@@ -13,6 +13,7 @@ final class EqualsAndHashCode2 {
long y;
float f;
double d;
boolean b;
}
@lombok.EqualsAndHashCode(callSuper=false)
+2 -2
View File
@@ -55,7 +55,7 @@ public class DataExample {
}
@Override public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
final long temp1 = Double.doubleToLongBits(this.getScore());
result = (result*PRIME) + (this.getName() == null ? 0 : this.getName().hashCode());
@@ -101,7 +101,7 @@ public class DataExample {
}
@Override public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = (result*PRIME) + (this.getName() == null ? 0 : this.getName().hashCode());
result = (result*PRIME) + (this.getValue() == null ? 0 : this.getValue().hashCode());
@@ -24,7 +24,7 @@ public class EqualsAndHashCodeExample {
}
@Override public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
final long temp1 = Double.doubleToLongBits(this.score);
result = (result*PRIME) + (this.name == null ? 0 : this.name.hashCode());
@@ -57,7 +57,7 @@ public class EqualsAndHashCodeExample {
}
@Override public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
result = (result*PRIME) + super.hashCode();
result = (result*PRIME) + this.width;
+2 -2
View File
@@ -46,7 +46,7 @@ public final class ValueExample {
@java.lang.Override
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
final Object $name = this.getName();
result = result * PRIME + ($name == null ? 0 : $name.hashCode());
@@ -103,7 +103,7 @@ public final class ValueExample {
@java.lang.Override
public int hashCode() {
final int PRIME = 277;
final int PRIME = 59;
int result = 1;
final Object $name = this.getName();
result = result * PRIME + ($name == null ? 0 : $name.hashCode());