package cz.moneta.test.harness.support.messaging;

/**
 * Wrapper for extracted JSON path value.
 */
public class JsonPathValue {

    private final String value;

    public JsonPathValue(String value) {
        this.value = value;
    }

    /**
     * Returns the value as String.
     */
    public String asText() {
        return value;
    }

    /**
     * Returns the value as Boolean.
     */
    public Boolean asBoolean() {
        if (value == null) {
            return null;
        }
        return Boolean.parseBoolean(value);
    }

    /**
     * Returns the value as Integer.
     */
    public Integer asInteger() {
        if (value == null) {
            return null;
        }
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Cannot parse '" + value + "' as Integer", e);
        }
    }

    /**
     * Returns the value as Long.
     */
    public Long asLong() {
        if (value == null) {
            return null;
        }
        try {
            return Long.parseLong(value);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Cannot parse '" + value + "' as Long", e);
        }
    }

    /**
     * Returns the underlying value.
     */
    public String getValue() {
        return value;
    }
}
