memoize StateExpr hashCode and parameters (for equals).
package org.batfish.z3.expr;
+import com.google.common.base.Suppliers;
+import java.util.List;
import java.util.Objects;
+import java.util.function.Supplier;
import org.batfish.z3.expr.visitors.ExprVisitor;
+import org.batfish.z3.state.StateParameter;
import org.batfish.z3.state.visitors.GenericStateExprVisitor;
import org.batfish.z3.state.visitors.Parameterizer;
import org.batfish.z3.state.visitors.StateVisitor;
/** An expression representing parameterized state. */
public abstract class StateExpr extends Expr {
+ private final Supplier<Integer> _hashCode = Suppliers.memoize(this::computeHashCode);
+ private final Supplier<List<StateParameter>> _params = Suppliers.memoize(this::computeParameters);
public abstract static class State {
public abstract void accept(StateVisitor visitor);
@@ -20,15 +26,23 @@ public abstract class StateExpr extends Expr {
public abstract <R> R accept(GenericStateExprVisitor<R> visitor);
+ private int computeHashCode() {
+ return Objects.hash(getClass().getCanonicalName(), _params.get());
+ }
+
+ private List<StateParameter> computeParameters() {
+ return Parameterizer.getParameters(this);
+ }
+
@Override
protected final boolean exprEquals(Expr e) {
- return Parameterizer.getParameters(this).equals(Parameterizer.getParameters((StateExpr) e));
+ return _params.get().equals(((StateExpr) e)._params.get());
}
public abstract State getState();
@Override
public int hashCode() {
- return Objects.hash(getClass(), Parameterizer.getParameters(this));
+ return _hashCode.get();
}
}
