net.sf.andromedaioc.bean.converter.ConverterFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of andromeda-ioc Show documentation
Show all versions of andromeda-ioc Show documentation
Inversion of Control Framework for Android
The newest version!
package net.sf.andromedaioc.bean.converter;
import net.sf.andromedaioc.bean.converter.fromboolean.FromBooleanConverterFactory;
import net.sf.andromedaioc.bean.converter.fromnumber.FromNumberConverterFactory;
import net.sf.andromedaioc.bean.converter.fromstring.FromStringConverterFactory;
import java.util.HashMap;
import java.util.Map;
/**
* Converter factory
*
* @author Alexey Mitrov
*/
public class ConverterFactory {
private final static TransparentConverter, ?> TRANSPARENT_CONVERTER = new TransparentConverter();
private final static Map, FromConverterFactory>> fromConverterFactories;
static {
fromConverterFactories = new HashMap, FromConverterFactory>>();
fromConverterFactories.put(Boolean.class, new FromBooleanConverterFactory());
FromNumberConverterFactory fromNumberConverterFactory = new FromNumberConverterFactory();
fromConverterFactories.put(Byte.class, fromNumberConverterFactory);
fromConverterFactories.put(byte.class, fromNumberConverterFactory);
fromConverterFactories.put(Short.class, fromNumberConverterFactory);
fromConverterFactories.put(short.class, fromNumberConverterFactory);
fromConverterFactories.put(Integer.class, fromNumberConverterFactory);
fromConverterFactories.put(int.class, fromNumberConverterFactory);
fromConverterFactories.put(Long.class, fromNumberConverterFactory);
fromConverterFactories.put(long.class, fromNumberConverterFactory);
fromConverterFactories.put(Float.class, fromNumberConverterFactory);
fromConverterFactories.put(float.class, fromNumberConverterFactory);
fromConverterFactories.put(Double.class, fromNumberConverterFactory);
fromConverterFactories.put(double.class, fromNumberConverterFactory);
fromConverterFactories.put(String.class, new FromStringConverterFactory());
}
/**
* Get converter to convert value of fromType to toType
* @param fromType
* @param toType
* @param
* @param
* @return converter to convert value of fromType to toType
*/
public static Converter getConverter(Class fromType, Class toType) {
if(fromType == null || toType == null || fromType.equals(toType)) {
return (Converter) TRANSPARENT_CONVERTER;
}
FromConverterFactory fromConverterFactory = fromConverterFactories.get(fromType);
if(fromConverterFactory == null) {
return (Converter) TRANSPARENT_CONVERTER;
}
Converter converter = fromConverterFactory.getConverter(toType);
if(converter == null) {
return (Converter) TRANSPARENT_CONVERTER;
}
return converter;
}
}