Skip to content

Commit e76f701

Browse files
gsmetgunnarmorling
authored andcommitted
HV-1480 Avoid computing the hashCode if not necessary
A lot of NodeImpls are created during the build of the path and some of them are simply discarded as they are "modified" (NodeImpl are immutable so we create a new) further away. Thus we better avoid building the hashCode each time.
1 parent 780af71 commit e76f701

File tree

1 file changed

+5
-2
lines changed
  • engine/src/main/java/org/hibernate/validator/internal/engine/path

1 file changed

+5
-2
lines changed

engine/src/main/java/org/hibernate/validator/internal/engine/path/NodeImpl.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public class NodeImpl
6060
private final Integer index;
6161
private final Object key;
6262
private final ElementKind kind;
63-
private final int hashCode;
6463

6564
//type-specific attributes
6665
private final Class<?>[] parameterTypes;
@@ -69,6 +68,7 @@ public class NodeImpl
6968
private final Class<?> containerClass;
7069
private final Integer typeArgumentIndex;
7170

71+
private int hashCode = -1;
7272
private String asString;
7373

7474
private NodeImpl(String name, NodeImpl parent, boolean isIterable, Integer index, Object key, ElementKind kind, Class<?>[] parameterTypes,
@@ -84,7 +84,6 @@ private NodeImpl(String name, NodeImpl parent, boolean isIterable, Integer index
8484
this.parameterIndex = parameterIndex;
8585
this.containerClass = containerClass;
8686
this.typeArgumentIndex = typeArgumentIndex;
87-
this.hashCode = buildHashCode();
8887
}
8988

9089
//TODO It would be nicer if we could return PropertyNode
@@ -448,6 +447,10 @@ public final int buildHashCode() {
448447

449448
@Override
450449
public int hashCode() {
450+
if ( hashCode == -1 ) {
451+
hashCode = buildHashCode();
452+
}
453+
451454
return hashCode;
452455
}
453456

0 commit comments

Comments
 (0)