/** Our internal representation of a Is0Expr * in the P0 language. * See http://www.radford.edu/itec380/2009fall-ibarland/Hw06/hw06.html * * @author Ian Barland * @version 2014.Nov.04 */ public class WhenZExpr extends Expr { static final String TOKEN = "whenz"; Expr cond, thenPart, elsePart; WhenZExpr( Expr _cond, Expr _thenPart, Expr _elsePart) { this.cond = _cond; this.thenPart = _thenPart; this.elsePart = _elsePart; } public String toString() { return TOKEN + " " + cond.toString() + " then " + thenPart.toString() + " otherwise " + elsePart.toString() + "!"; } public Value eval() { if (((Num)(cond.eval())).doubleValue() == 0) { return thenPart.eval(); } else { return elsePart.eval(); } } @Override public boolean equals( /* WhenZExpr this, */ Object that) { if (this==that) { return true; } else if (that==null) { return false; } else if (this.getClass() != that.getClass()) { return false; } else { WhenZExpr thatt = (WhenZExpr) that; return this.cond.equals(thatt.cond) && this.thenPart.equals(thatt.thenPart) && this.elsePart.equals(thatt.elsePart); } } @Override public int hashCode() { if (cachedHash == null) { int hashSoFar = (int) 3141949305892159L; // fingerprint hashSoFar += this.cond.hashCode(); hashSoFar *= 31; hashSoFar += this.thenPart.hashCode(); hashSoFar *= 31; hashSoFar += this.elsePart.hashCode(); cachedHash = hashSoFar; } return cachedHash; } private Integer cachedHash = null; }