Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ public IDeviceID create(String[] segments) {
// or we can just use a tuple like Relational DB.
private final String[] segments;

/** Cache the hash code */
private int hash; // Default to 0

/**
* Cache if the hash has been calculated as actually being zero, enabling us to avoid
* recalculating this.
*/
private boolean hashIsZero; // Default to false;

public StringArrayDeviceID(String... deviceIdSegments) {
this.segments = formalize(deviceIdSegments);
}
Expand Down Expand Up @@ -306,7 +315,16 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Arrays.hashCode(segments);
int h = hash;
if (h == 0 && !hashIsZero) {
h = Arrays.hashCode(segments);
if (h == 0) {
hashIsZero = true;
} else {
hash = h;
}
}
return h;
}

@Override
Expand Down
Loading