Search in sources :

Example 1 with IntegerOption

use of org.sosy_lab.common.configuration.IntegerOption in project java-common-lib by sosy-lab.

the class IntegerTypeConverter method convert.

@Override
public Object convert(String optionName, String valueStr, TypeToken<?> pType, Annotation pOption, Path pSource, LogManager logger) throws InvalidConfigurationException {
    Class<?> type = pType.getRawType();
    if (!(pOption instanceof IntegerOption)) {
        throw new UnsupportedOperationException("IntegerTypeConverter needs options annotated with @IntegerOption");
    }
    IntegerOption option = (IntegerOption) pOption;
    assert type.equals(Integer.class) || type.equals(Long.class);
    Object value = BaseTypeConverter.valueOf(type, optionName, valueStr);
    long n = ((Number) value).longValue();
    if (option.min() > n || n > option.max()) {
        throw new InvalidConfigurationException(String.format("Invalid value in configuration file: \"%s = %s\" (not in range [%d, %d]).", optionName, value, option.min(), option.max()));
    }
    return value;
}
Also used : IntegerOption(org.sosy_lab.common.configuration.IntegerOption) InvalidConfigurationException(org.sosy_lab.common.configuration.InvalidConfigurationException)

Aggregations

IntegerOption (org.sosy_lab.common.configuration.IntegerOption)1 InvalidConfigurationException (org.sosy_lab.common.configuration.InvalidConfigurationException)1