package expression;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * @author Doschennikov Nikita (me@fymio.us)
 */
public class Pow2 extends AbstractExpression {
    private final AbstractExpression operand;

    public Pow2(AbstractExpression operand) {
        this.operand = operand;
    }

    private static int pow2(int n) {
        if (n < 0) throw new ArithmeticException("pow₂ of negative number: " + n);
        if (n >= 31) throw new OverflowException("pow₂");
        return 1 << n;
    }

    @Override public int        evaluate(int x)               { return pow2(operand.evaluate(x)); }
    @Override public int        evaluate(int x, int y, int z) { return pow2(operand.evaluate(x,y,z)); }
    @Override public int        evaluate(List<Integer> vars)  { return pow2(operand.evaluate(vars)); }
    @Override public BigInteger evaluateBi(List<BigInteger> vars) { throw new UnsupportedOperationException(); }
    @Override public BigDecimal evaluateBd(List<BigDecimal> vars) { throw new UnsupportedOperationException(); }

    @Override public String toString()     { return "pow₂(" + operand + ")"; }

    @Override
    public String toMiniString() {
        if (operand instanceof AbstractBinaryOperation) {
            return "pow₂(" + operand.toMiniString() + ")";
        }
        return "pow₂ " + operand.toMiniString();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (!(obj instanceof Pow2)) return false;
        return operand.equals(((Pow2) obj).operand);
    }

    @Override public int hashCode() { return operand.hashCode() ^ 0x504F5732; }
}