Skip to content
Draft
Show file tree
Hide file tree
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 @@ -51,6 +51,11 @@ public final class KeyValues implements Iterable<KeyValue> {
*/
private final int length;

/**
* Cache the hash code of the {@code KeyValues}, 0 value means hash was not computed.
*/
private int hash;

/**
* A constructor that initializes a {@code KeyValues} object with a sorted set of
* key-values and its length.
Expand Down Expand Up @@ -291,9 +296,16 @@ public Stream<KeyValue> stream() {

@Override
public int hashCode() {
int result = 1;
for (int i = 0; i < length; i++) {
result = 31 * result + sortedSet[i].hashCode();
int result = hash;
if (result == 0) {
result = 1;
for (int i = 0; i < length; i++) {
result = 31 * result + sortedSet[i].hashCode();
}
if (result == 0) { // Re-map 0 hash code
result = 1;
}
hash = result;
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public final class Tags implements Iterable<Tag> {
*/
private final int length;

/**
* Cache the hash code of the {@code Tags}, 0 value means hash was not computed.
*/
private int hash;

/**
* A constructor that initializes a {@code Tags} object with a sorted set of tags and
* its length.
Expand Down Expand Up @@ -267,9 +272,16 @@ public Stream<Tag> stream() {

@Override
public int hashCode() {
int result = 1;
for (int i = 0; i < length; i++) {
result = 31 * result + sortedSet[i].hashCode();
int result = hash;
if (result == 0) {
result = 1;
for (int i = 0; i < length; i++) {
result = 31 * result + sortedSet[i].hashCode();
}
if (result == 0) { // Re-map 0 hash code
result = 1;
}
hash = result;
}
return result;
}
Expand Down