com.v5analytics.webster.DefaultParameterValueConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webster Show documentation
Show all versions of webster Show documentation
Minimalist web framework, that has an express.js like API.
package com.v5analytics.webster;
import java.util.HashMap;
import java.util.Map;
public class DefaultParameterValueConverter implements ParameterValueConverter {
private static final Map valueConverters = new HashMap<>();
static {
registerValueConverter(Boolean.class, new BooleanConverter());
registerValueConverter(Boolean.TYPE, new BooleanConverter());
registerValueConverter(Integer.class, new IntegerConverter());
registerValueConverter(Integer.TYPE, new IntegerConverter());
registerValueConverter(Long.class, new LongConverter());
registerValueConverter(Long.TYPE, new LongConverter());
registerValueConverter(Double.class, new DoubleConverter());
registerValueConverter(Double.TYPE, new DoubleConverter());
registerValueConverter(Float.class, new FloatConverter());
registerValueConverter(Float.TYPE, new FloatConverter());
registerValueConverter(String.class, new StringConverter());
registerValueConverter(String[].class, new StringArrayConverter());
}
public static void registerValueConverter(Class clazz, Converter converter) {
valueConverters.put(clazz, converter);
}
@Override
public Object toValue(Class parameterType, String parameterName, String[] value) {
try {
if (value == null) {
return null;
}
Converter valueConverter = getValueConverterForType(parameterType);
if (valueConverter != null) {
return valueConverter.convert(parameterType, parameterName, value);
}
} catch (Exception ex) {
throw new WebsterException("Could not parse value \"" + toString(value) + "\" for parameter \"" + parameterName + "\"");
}
throw new WebsterException("Inconvertible parameter type for parameter \"" + parameterName + "\"");
}
private Converter getValueConverterForType(Class parameterType) {
Converter valueConverter = valueConverters.get(parameterType);
if (valueConverter != null) {
return valueConverter;
}
for (Map.Entry classConverterEntry : valueConverters.entrySet()) {
if (classConverterEntry.getKey().isAssignableFrom(parameterType)) {
return classConverterEntry.getValue();
}
}
return null;
}
private String toString(String[] value) {
StringBuilder result = new StringBuilder();
boolean first = true;
for (String v : value) {
if (!first) {
result.append(",");
}
result.append(v);
first = false;
}
return result.toString();
}
public interface Converter {
T convert(Class parameterType, String parameterName, String[] value);
}
public abstract static class SingleValueConverter implements Converter {
@Override
public T convert(Class parameterType, String parameterName, String[] value) {
if (value.length == 0) {
return null;
}
if (value.length > 1) {
throw new WebsterException("Too many " + parameterName + " found. Expected 1 found " + value.length);
}
return convert(parameterType, parameterName, value[0]);
}
public abstract T convert(Class parameterType, String parameterName, String value);
}
public static class BooleanConverter extends SingleValueConverter {
@Override
public Boolean convert(Class parameterType, String parameterName, String value) {
if (value == null) {
return null;
}
return value.length() == 0 || Boolean.parseBoolean(value);
}
}
public static class IntegerConverter extends SingleValueConverter {
@Override
public Integer convert(Class parameterType, String parameterName, String value) {
if (value == null || value.trim().length() == 0) {
return null;
}
return Integer.parseInt(value);
}
}
public static class LongConverter extends SingleValueConverter {
@Override
public Long convert(Class parameterType, String parameterName, String value) {
if (value == null || value.trim().length() == 0) {
return null;
}
return Long.parseLong(value);
}
}
public static class DoubleConverter extends SingleValueConverter {
@Override
public Double convert(Class parameterType, String parameterName, String value) {
if (value == null || value.trim().length() == 0) {
return null;
}
return Double.parseDouble(value);
}
}
public static class FloatConverter extends SingleValueConverter {
@Override
public Float convert(Class parameterType, String parameterName, String value) {
if (value == null || value.trim().length() == 0) {
return null;
}
return Float.parseFloat(value);
}
}
public static class StringConverter extends SingleValueConverter {
@Override
public String convert(Class parameterType, String parameterName, String value) {
return value;
}
}
public static class StringArrayConverter implements Converter {
@Override
public String[] convert(Class parameterType, String parameterName, String[] value) {
return value;
}
}
}