Search in sources :

Example 1 with TypeConverter

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

the class Configuration method convertDefaultValue.

@Nullable
private <T> Object convertDefaultValue(String optionName, @Nullable T defaultValue, TypeToken<T> type, @Nullable Annotation secondaryOption) throws InvalidConfigurationException {
    @Var TypeToken<?> innerType;
    if (type.isArray()) {
        innerType = checkNotNull(type.getComponentType());
    } else if (COLLECTIONS.containsKey(type.getRawType())) {
        innerType = Classes.getSingleTypeArgument(type);
    } else {
        innerType = type;
    }
    innerType = innerType.wrap();
    checkApplicability(secondaryOption, innerType);
    if (type.equals(innerType)) {
        // If its not a collection, we try to pass the default value to the
        // type converter if there is any.
        // TODO: Also pass default values inside a collection.
        TypeConverter converter = getConverter(type, secondaryOption);
        return converter.convertDefaultValue(optionName, defaultValue, type, secondaryOption);
    }
    return defaultValue;
}
Also used : TypeConverter(org.sosy_lab.common.configuration.converters.TypeConverter) ClassTypeConverter(org.sosy_lab.common.configuration.converters.ClassTypeConverter) TimeSpanTypeConverter(org.sosy_lab.common.configuration.converters.TimeSpanTypeConverter) IntegerTypeConverter(org.sosy_lab.common.configuration.converters.IntegerTypeConverter) BaseTypeConverter(org.sosy_lab.common.configuration.converters.BaseTypeConverter) Var(com.google.errorprone.annotations.Var) Nullable(javax.annotation.Nullable)

Example 2 with TypeConverter

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

the class Configuration method convertSingleValue.

/**
 * This function takes a value (String) and a type and returns an Object of this type with the
 * value as content.
 *
 * <p>The type may not be an array or a collection type, and the value may only be a single value
 * (not multiple values).
 *
 * @param optionName name of option, only for error handling
 * @param valueStr new value of the option
 * @param type type of the object
 * @param secondaryOption the optional second annotation of the option (needs to fit to the type)
 */
@Nullable
private Object convertSingleValue(String optionName, @Var String valueStr, @Var TypeToken<?> type, @Nullable Annotation secondaryOption) throws InvalidConfigurationException {
    boolean isAnnotated = type.getRawType() == AnnotatedValue.class;
    @Var String annotation = null;
    if (isAnnotated) {
        type = Classes.getSingleTypeArgument(type);
        Iterator<String> parts = ANNOTATION_VALUE_SPLITTER.split(valueStr).iterator();
        valueStr = parts.next();
        annotation = Iterators.getNext(parts, null);
    }
    // try to find a type converter, either for the type of the annotation
    // or for the type of the field
    TypeConverter converter = getConverter(type, secondaryOption);
    @Var Object result = converter.convert(optionName, valueStr, type, secondaryOption, sources.get(optionName), firstNonNull(logger, LogManager.createNullLogManager()));
    if (result != null && isAnnotated) {
        result = AnnotatedValue.create(result, Optional.ofNullable(annotation));
    }
    return result;
}
Also used : TypeConverter(org.sosy_lab.common.configuration.converters.TypeConverter) ClassTypeConverter(org.sosy_lab.common.configuration.converters.ClassTypeConverter) TimeSpanTypeConverter(org.sosy_lab.common.configuration.converters.TimeSpanTypeConverter) IntegerTypeConverter(org.sosy_lab.common.configuration.converters.IntegerTypeConverter) BaseTypeConverter(org.sosy_lab.common.configuration.converters.BaseTypeConverter) Var(com.google.errorprone.annotations.Var) AccessibleObject(java.lang.reflect.AccessibleObject) Nullable(javax.annotation.Nullable)

Aggregations

Var (com.google.errorprone.annotations.Var)2 Nullable (javax.annotation.Nullable)2 BaseTypeConverter (org.sosy_lab.common.configuration.converters.BaseTypeConverter)2 ClassTypeConverter (org.sosy_lab.common.configuration.converters.ClassTypeConverter)2 IntegerTypeConverter (org.sosy_lab.common.configuration.converters.IntegerTypeConverter)2 TimeSpanTypeConverter (org.sosy_lab.common.configuration.converters.TimeSpanTypeConverter)2 TypeConverter (org.sosy_lab.common.configuration.converters.TypeConverter)2 AccessibleObject (java.lang.reflect.AccessibleObject)1