org.hrodberaht.i18n.formatter.Formatter Maven / Gradle / Ivy
/*
*
*/
package org.hrodberaht.i18n.formatter;
import org.hrodberaht.i18n.formatter.types.CurrencyData;
import org.hrodberaht.i18n.formatter.types.PercentData;
import org.hrodberaht.i18n.locale.LocaleProvider;
import java.util.Date;
import java.util.Locale;
/**
* Simple Java Utils
*
* @author Robert Alexandersson
* @version 1.0
* @since 1.0
*/
public class Formatter {
protected Locale locale = LocaleProvider.getProfile().getLocale();
public static Formatter getFormatter(Class aType) {
return getFormatter(aType, null);
}
@SuppressWarnings(value = "unchecked")
public static Formatter getFormatter(Class aType, DateFormatter.DateConvert dateConvert) {
if (String.class.isAssignableFrom(aType)) {
return new Formatter();
} else if (CurrencyData.class.isAssignableFrom(aType)) {
return new CurrencyFormatter();
} else if (PercentData.class.isAssignableFrom(aType)) {
return new PercentageFormatter();
} else if (Date.class.isAssignableFrom(aType)) {
return dateConvert != null ? new DateFormatter(dateConvert) : new DateFormatter();
} else if (Integer.class.isAssignableFrom(aType)) {
return new IntegerFormatter();
} else if (int.class.isAssignableFrom(aType)) {
return new IntegerFormatter();
} else if (Long.class.isAssignableFrom(aType)) {
return new LongFormatter();
} else if (long.class.isAssignableFrom(aType)) {
return new LongFormatter();
} else if (Boolean.class.isAssignableFrom(aType)) {
return new BooleanFormatter();
} else if (Boolean.TYPE.isAssignableFrom(aType)) {
return new BooleanFormatter();
} else if(Double.class.isAssignableFrom(aType)){
return new DecimalFormatter();
}
return null;
}
/**
* Returns true
if the provided class is an array getType,
* implements one of the Formatter classes currently supported.
*
* @see #getFormatter(Class)
*/
public static boolean isSupportedType(Class type) {
return getFormatter(type) != null;
}
public String convertToString(T value) {
return value.toString();
}
public T convertToObject(String string) {
return string == null ? null : (T) string.trim();
}
}