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;
}
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;
}
Aggregations