package expression.generic;

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

    public static final ShortType INSTANCE = new ShortType();

    private ShortType() {}

    @Override
    public Short fromInt(int value) {
        return (short) value;
    }

    @Override
    public Short add(Short a, Short b) {
        return (short) (a + b);
    }

    @Override
    public Short subtract(Short a, Short b) {
        return (short) (a - b);
    }

    @Override
    public Short multiply(Short a, Short b) {
        return (short) (a * b);
    }

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

    @Override
    public Short negate(Short a) {
        return (short) (-a);
    }

    @Override
    public Short count(Short a) {
        return (short) Integer.bitCount(a & 0xffff);
    }

    @Override
    public Short min(Short a, Short b) {
        return (short) Math.min(a, b);
    }

    @Override
    public Short max(Short a, Short b) {
        return (short) Math.max(a, b);
    }
}
