Skip to content

Commit 5cdae24

Browse files
l46kokcopybara-github
authored andcommitted
Remove cached hashcode from MutableExpr
PiperOrigin-RevId: 623887941
1 parent b412d76 commit 5cdae24

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

common/src/main/java/dev/cel/common/ast/CelMutableExpr.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public final class CelMutableExpr {
3939
private long id;
4040
private ExprKind.Kind exprKind;
4141
private Object exprValue;
42-
private int hash = 0;
4342

4443
public long id() {
4544
return id;
@@ -1016,19 +1015,12 @@ public boolean equals(Object obj) {
10161015

10171016
@Override
10181017
public int hashCode() {
1019-
if (hash == 0) {
1020-
int h = 1;
1021-
h *= 1000003;
1022-
h ^= (int) ((id >>> 32) ^ id);
1023-
h *= 1000003;
1024-
h ^= this.exprValue().hashCode();
1025-
1026-
if (h == 0) {
1027-
h = 1;
1028-
}
1029-
hash = h;
1030-
}
1018+
int h = 1;
1019+
h *= 1000003;
1020+
h ^= (int) ((id >>> 32) ^ id);
1021+
h *= 1000003;
1022+
h ^= this.exprValue().hashCode();
10311023

1032-
return hash;
1024+
return h;
10331025
}
10341026
}

common/src/test/java/dev/cel/common/ast/CelMutableExprTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,4 +783,14 @@ public void hashCodeTest(@TestParameter HashCodeTestCase testCase) {
783783
// Run it twice to ensure cached value is stable
784784
assertThat(testCase.mutableExpr.hashCode()).isEqualTo(testCase.expectedHashCode);
785785
}
786+
787+
@Test
788+
public void propertyMutated_hashCodeChanged() {
789+
CelMutableExpr mutableExpr = CelMutableExpr.ofIdent("x");
790+
int originalHash = mutableExpr.hashCode();
791+
792+
mutableExpr.ident().setName("y");
793+
794+
assertThat(originalHash).isNotEqualTo(mutableExpr.hashCode());
795+
}
786796
}

0 commit comments

Comments
 (0)