cucumber.runtime.converters.TimeConverter Maven / Gradle / Ivy
package cucumber.runtime.converters;
import cucumber.runtime.CucumberException;
import cucumber.runtime.ParameterType;
import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import static java.util.Arrays.asList;
public abstract class TimeConverter extends ConverterWithFormat {
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
protected final Locale locale;
private final List formats = new ArrayList();
private SimpleDateFormat onlyFormat;
public TimeConverter(Locale locale, Class[] convertibleTypes) {
super(convertibleTypes);
this.locale = locale;
// TODO - these are expensive to create. Cache by format+string, or use the XStream DF cache util thingy
addFormat(DateFormat.SHORT, locale);
addFormat(DateFormat.MEDIUM, locale);
addFormat(DateFormat.LONG, locale);
addFormat(DateFormat.FULL, locale);
}
protected void addFormat(int style, Locale locale) {
add(DateFormat.getDateInstance(style, locale));
}
protected void add(DateFormat dateFormat) {
dateFormat.setLenient(false);
dateFormat.setTimeZone(UTC);
formats.add(dateFormat);
}
public List extends Format> getFormats() {
return onlyFormat == null ? formats : asList(onlyFormat);
}
public void setOnlyFormat(String dateFormatString, Locale locale) {
onlyFormat = new SimpleDateFormat(dateFormatString, locale);
onlyFormat.setLenient(false);
onlyFormat.setTimeZone(UTC);
}
public void removeOnlyFormat() {
onlyFormat = null;
}
public static TimeConverter getInstance(ParameterType parameterType, Locale locale) {
if (Date.class.isAssignableFrom(parameterType.getParameterClass())) {
return new DateConverter(locale);
} else if (Calendar.class.isAssignableFrom(parameterType.getParameterClass())) {
return new CalendarConverter(locale);
} else {
throw new CucumberException("Unsupported time type: " + parameterType.getParameterClass());
}
}
public static List getTimeClasses() {
List classes = new ArrayList();
classes.add(Date.class);
classes.add(Calendar.class);
return classes;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy