package expression.generic;

/**
 * @author Doschennikov Nikita (me@fymio.us)
 */
public class IntTruncType implements ArithmeticType<Integer> {

    public static final IntTruncType INSTANCE = new IntTruncType();

    private IntTruncType() {}

    private static int t(int v) {
        return v - (v % 10);
    }

    @Override
    public Integer fromInt(int value) {
        return t(value);
    }

    @Override
    public Integer add(Integer a, Integer b) {
        return t(a + b);
    }

    @Override
    public Integer subtract(Integer a, Integer b) {
        return t(a - b);
    }

    @Override
    public Integer multiply(Integer a, Integer b) {
        return t(a * b);
    }

    @Override
    public Integer divide(Integer a, Integer b) {
        if (b == 0) throw new ArithmeticException("Division by zero");
        return t(a / b);
    }

    @Override
    public Integer negate(Integer a) {
        return t(-a);
    }

    @Override
    public Integer count(Integer a) {
        return t(Integer.bitCount(a));
    }

    @Override
    public Integer min(Integer a, Integer b) {
        return t(Math.min(a, b));
    }

    @Override
    public Integer max(Integer a, Integer b) {
        return t(Math.max(a, b));
    }
}
