Search in sources :

Example 1 with TimeSpanOption

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

the class TimeSpanTypeConverter 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 TimeSpanOption)) {
        throw new UnsupportedOperationException("Time span options need to be annotated with @TimeSpanOption");
    }
    TimeSpanOption option = (TimeSpanOption) pOption;
    // find unit in input string
    @Var int i = valueStr.length() - 1;
    while (i >= 0 && LETTER_MATCHER.matches(valueStr.charAt(i))) {
        i--;
    }
    if (i < 0) {
        throw new InvalidConfigurationException("Option " + optionName + " contains no number");
    }
    // convert unit string to TimeUnit
    String userUnitStr = valueStr.substring(i + 1).trim();
    TimeUnit userUnit = userUnitStr.isEmpty() ? option.defaultUserUnit() : TIME_UNITS.get(userUnitStr);
    if (userUnit == null) {
        throw new InvalidConfigurationException("Option " + optionName + " contains invalid unit");
    }
    // Parse string without unit
    long rawValue;
    try {
        rawValue = Long.parseLong(valueStr.substring(0, i + 1).trim());
    } catch (NumberFormatException e) {
        throw new InvalidConfigurationException("Option " + optionName + " contains invalid number");
    }
    // convert value from user unit to code unit
    TimeUnit codeUnit = option.codeUnit();
    long value = codeUnit.convert(rawValue, userUnit);
    if (option.min() > value || value > option.max()) {
        String codeUnitStr = TIME_UNITS.inverse().get(codeUnit);
        throw new InvalidConfigurationException(String.format("Invalid value in configuration file: \"%s = %s (not in range [%d %s, %d %s])", optionName, value, option.min(), codeUnitStr, option.max(), codeUnitStr));
    }
    Object result;
    if (type.equals(Integer.class)) {
        if (value > Integer.MAX_VALUE) {
            throw new InvalidConfigurationException("Value for option " + optionName + " is larger than " + Integer.MAX_VALUE);
        }
        result = (int) value;
    } else if (type.equals(Long.class)) {
        result = value;
    } else {
        assert type.equals(TimeSpan.class);
        result = TimeSpan.of(rawValue, userUnit);
    }
    return result;
}
Also used : Var(com.google.errorprone.annotations.Var) InvalidConfigurationException(org.sosy_lab.common.configuration.InvalidConfigurationException) TimeSpan(org.sosy_lab.common.time.TimeSpan) TimeUnit(java.util.concurrent.TimeUnit) TimeSpanOption(org.sosy_lab.common.configuration.TimeSpanOption)

Aggregations

Var (com.google.errorprone.annotations.Var)1 TimeUnit (java.util.concurrent.TimeUnit)1 InvalidConfigurationException (org.sosy_lab.common.configuration.InvalidConfigurationException)1 TimeSpanOption (org.sosy_lab.common.configuration.TimeSpanOption)1 TimeSpan (org.sosy_lab.common.time.TimeSpan)1