All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cz.jalasoft.util.configuration.ConverterRegistry Maven / Gradle / Ivy

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy