cz.jalasoft.util.configuration.ConverterRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JalasoftUtils Show documentation
Show all versions of JalasoftUtils Show documentation
A collection of utility classes that might be useful.
The newest version!
package cz.jalasoft.util.configuration;
import cz.jalasoft.util.converter.string.StringConverter;
import cz.jalasoft.util.converter.string.StringConverters;
import java.util.HashMap;
import java.util.Map;
/**
* @author Honza Lastovicka ([email protected])
* @since 2016-07-26.
*/
final class ConverterRegistry {
static ConverterRegistry standardConverters() {
return new ConverterRegistry()
.addConverter(StringConverters.identity())
.addConverter(StringConverters.toInteger())
.addConverter(StringConverters.toLong())
.addConverter(StringConverters.toDouble())
.addConverter(StringConverters.toDoublePrimitive())
.addConverter(StringConverters.toIntPrimitive());
//TODO pridej dalsi
}
private final Map, StringConverter>> converters;
private ConverterRegistry() {
converters = new HashMap<>();
}
ConverterRegistry addConverter(StringConverter converter) {
converters.put(converter.targetType(), converter);
return this;
}
boolean hasConverter(Class> type) {
return converters.containsKey(type);
}
StringConverter converter(Class type) {
StringConverter> converter = converters.get(type);
if (converter == null) {
throw new IllegalArgumentException("There is no converter for type " + type);
}
return (StringConverter) converter;
}
}