package sexpression;

import static java.text.MessageFormat.format;

import java.math.BigInteger;

import error.LispException;

@DisplayName("number")
public class LispNumber extends Atom {

    public static final LispNumber ZERO = new LispNumber(BigInteger.ZERO);
    public static final LispNumber ONE = new LispNumber(BigInteger.ONE);

    private BigInteger value;

    public LispNumber(String text) {
        super(text.replaceFirst("^0+(?!$)", ""));

        try {
            this.value = new BigInteger(text);
        } catch (NumberFormatException e) {
            throw new InvalidNumberException(text);
        }
    }

    public LispNumber(BigInteger value) {
        super(value.toString());

        this.value = value;
    }

    @Override
    public boolean isNumber() {
        return true;
    }

    public BigInteger getValue() {
        return value;
    }

    public class InvalidNumberException extends LispException {

        private static final long serialVersionUID = 1L;
        private String text;

        public InvalidNumberException(String text) {
            this.text = text;
        }

        @Override
        public String getMessage() {
            return format("{0} is not a valid integer", text);
        }
    }

}
