net.sf.aguacate.util.formatter.impl.OutputFormaterImpl Maven / Gradle / Ivy
package net.sf.aguacate.util.formatter.impl;
import java.text.Format;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.time.FastDateFormat;
import net.sf.aguacate.model.FieldType;
import net.sf.aguacate.util.formatter.OutputFormater;
import net.sf.aguacate.util.formatter.impl.spi.DateFormater;
import net.sf.aguacate.util.formatter.impl.spi.DefaultFormatter;
public class OutputFormaterImpl implements OutputFormater {
private static final String SFMT_DATE = "yyyy-MM-dd";
private static final String SFMT_TIME = "HH:mm:ss.SSS";
private static final String SFMT_DATETIME = SFMT_DATE + "'T'" + SFMT_TIME;
private static final Format FMT_DATE;
private static final Format FMT_TIME;
private static final Format FMT_DATETIME;
private final Formater def;
private final Map formaters;
static {
FMT_DATE = FastDateFormat.getInstance(SFMT_DATE);
FMT_TIME = FastDateFormat.getInstance(SFMT_TIME);
FMT_DATETIME = FastDateFormat.getInstance(SFMT_DATETIME);
}
public OutputFormaterImpl() {
def = new DefaultFormatter();
Map temp = new HashMap<>();
temp.put(FieldType.DATE, new DateFormater(FMT_DATE));
temp.put(FieldType.DATETIME, new DateFormater(FMT_DATETIME));
temp.put(FieldType.TIME, new DateFormater(FMT_TIME));
formaters = temp;
}
@Override
public String toString(String type, Object value) {
return toString(FieldType.valueOf(type), value);
}
@Override
public String toString(FieldType type, Object value) {
Formater formater = formaters.get(type);
if (formater == null) {
formater = def;
}
return formater.format(value);
}
}