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

net.sf.andromedaioc.bean.converter.fromnumber.FromNumberConverterFactory Maven / Gradle / Ivy

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

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

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

/**
 * From number converter factory
 *
 * @author Alexey Mitrov
 */
public class FromNumberConverterFactory implements FromConverterFactory {

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

    public FromNumberConverterFactory() {
        converters = new HashMap, Converter>();
        FromNumberToBooleanConverter fromNumberToBooleanConverter = new FromNumberToBooleanConverter();
        converters.put(Boolean.class, fromNumberToBooleanConverter);
        converters.put(boolean.class, fromNumberToBooleanConverter);
        FromNumberToByteConverter fromNumberToByteConverter = new FromNumberToByteConverter();
        converters.put(Byte.class, fromNumberToByteConverter);
        converters.put(byte.class, fromNumberToByteConverter);
        FromNumberToShortConverter fromNumberToShortConverter = new FromNumberToShortConverter();
        converters.put(Short.class, fromNumberToShortConverter);
        converters.put(short.class, fromNumberToShortConverter);
        FromNumberToIntegerConverter fromNumberToIntegerConverter = new FromNumberToIntegerConverter();
        converters.put(Integer.class, fromNumberToIntegerConverter);
        converters.put(int.class, fromNumberToIntegerConverter);
        FromNumberToFloatConverter fromNumberToFloatConverter = new FromNumberToFloatConverter();
        converters.put(Float.class, fromNumberToFloatConverter);
        converters.put(float.class, fromNumberToFloatConverter);
        FromNumberToDoubleConverter fromNumberToDoubleConverter = new FromNumberToDoubleConverter();
        converters.put(Double.class, fromNumberToDoubleConverter);
        converters.put(double.class, fromNumberToDoubleConverter);
        converters.put(String.class, new FromNumberToStringConverter());
    }

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy