package expression;

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

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

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

    private static int absInt(int n) {
        if (n == Integer.MIN_VALUE) throw new OverflowException("abs");
        return n < 0 ? -n : n;
    }

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

    @Override public String toString()     { return "‖" + operand + "‖"; }
    @Override public String toMiniString() { return "‖" + operand.toMiniString() + "‖"; }

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

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