QName is usually involved in hash-based lookups, hence it is
useful to reuse a computed hash code so that constants do not
spend cycles on computing it more than once.
Add a field which will be lazily initiated to contain the result
of hashCode(). This has no impact on memory footprint.
Change-Id: I8a0b89599ed1029e2c572535826c52b4b837e265
Signed-off-by: Robert Varga <rovarga@cisco.com>
private static final Pattern QNAME_PATTERN_NO_NAMESPACE_NO_REVISION = Pattern.compile("^(.+)$");
private static final char[] ILLEGAL_CHARACTERS = new char[] { '?', '(', ')', '&' };
- // Mandatory
+ // Non-null
private final QNameModule module;
- // Mandatory
+ // Non-null
private final String localName;
+ private transient int hash;
private QName(final QNameModule module, final String localName) {
this.localName = checkLocalName(localName);
@Override
public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + Objects.hashCode(localName);
- result = prime * result + module.hashCode();
- return result;
+ if (hash == 0) {
+ hash = Objects.hash(module, localName);
+ }
+ return hash;
}
/**