Builder-derived classes can be used as HashMap keys, which means they
can see their hashcode computed multiple times. Since this is a
recursive operation, the cost of it can be quite high, so dedicate two
fields for caching the hashcode result.
Change-Id: I0d95fd94187a421e4018b2f6992dfeaf2e126732
Signed-off-by: Robert Varga <rovarga@cisco.com>
*/
def protected generateHashCode() '''
«IF !properties.empty || augmentField != null»
+ private int hash = 0;
+ private volatile boolean hashValid = false;
+
@Override
public int hashCode() {
+ if (hashValid) {
+ return hash;
+ }
+
final int prime = 31;
int result = 1;
«FOR property : properties»
«IF augmentField != null»
result = prime * result + ((«augmentField.name» == null) ? 0 : «augmentField.name».hashCode());
«ENDIF»
+
+ hash = result;
+ hashValid = true;
return result;
}
«ENDIF»