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

net.sf.andromedaioc.bean.converter.fromstring.FromStringConverterFactory Maven / Gradle / Ivy

The newest version!
package net.sf.andromedaioc.bean.converter.fromstring;

import net.sf.andromedaioc.bean.converter.Converter;
import net.sf.andromedaioc.bean.converter.FromConverterFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * From String converter factory
 *
 * @author Alexey Mitrov
 */
public class FromStringConverterFactory implements FromConverterFactory {

    private final Map, Converter> converters;
    private final Map, FromStringToEnumConverter> enumConverters = new HashMap, FromStringToEnumConverter>();

    public FromStringConverterFactory () {
        converters = new HashMap, Converter>();
        FromStringToBooleanConverter fromStringToBooleanConverter = new FromStringToBooleanConverter();
        converters.put(Boolean.class, fromStringToBooleanConverter);
        converters.put(boolean.class, fromStringToBooleanConverter);
        FromStringToByteConverter fromStringToByteConverter = new FromStringToByteConverter();
        converters.put(Byte.class, fromStringToByteConverter);
        converters.put(byte.class, fromStringToByteConverter);
        FromStringToShortConverter fromStringToShortConverter = new FromStringToShortConverter();
        converters.put(Short.class, fromStringToShortConverter);
        converters.put(short.class, fromStringToShortConverter);
        FromStringToIntegerConverter fromStringToIntegerConverter = new FromStringToIntegerConverter();
        converters.put(Integer.class, fromStringToIntegerConverter);
        converters.put(int.class, fromStringToIntegerConverter);
        FromStringToFloatConverter fromStringToFloatConverter = new FromStringToFloatConverter();
        converters.put(Float.class, fromStringToFloatConverter);
        converters.put(float.class, fromStringToFloatConverter);
        FromStringToDoubleConverter fromStringToDoubleConverter = new FromStringToDoubleConverter();
        converters.put(Double.class, fromStringToDoubleConverter);
        converters.put(double.class, fromStringToDoubleConverter);
        converters.put(String.class, new FromStringToStringConverter());
    }

    public  Converter getConverter(Class toClass) {
        return (Converter) (toClass.isEnum() ? getEnumConverter(toClass.asSubclass(Enum.class)) : converters.get(toClass));
    }

    private synchronized  Converter getEnumConverter(Class toClass) {
        FromStringToEnumConverter converter = (FromStringToEnumConverter) enumConverters.get(toClass);
        if(converter != null) {
            return converter;
        } else {
            converter = new FromStringToEnumConverter(toClass);
            enumConverters.put(toClass, converter);
            return converter;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy