
com.github.gkutiel.flip.web.ParseParamService Maven / Gradle / Ivy
package com.github.gkutiel.flip.web;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class ParseParamService {
interface RequestParamParser {
public T from(String[] strs);
}
private static final Map, RequestParamParser>> REQUEST_PARAMS_PARSERS = new HashMap, RequestParamParser>>();
static {
REQUEST_PARAMS_PARSERS.put(String.class, new RequestParamParser() {
@Override
public String from(final String[] strs) {
return strs[0];
}
});
final RequestParamParser intValue = new RequestParamParser() {
@Override
public Integer from(final String[] strs) {
return Integer.valueOf(strs[0]);
}
};
REQUEST_PARAMS_PARSERS.put(int.class, intValue);
REQUEST_PARAMS_PARSERS.put(Integer.class, intValue);
final RequestParamParser longValue = new RequestParamParser() {
@Override
public Long from(final String[] strs) {
return Long.valueOf(strs[0]);
}
};
REQUEST_PARAMS_PARSERS.put(long.class, longValue);
REQUEST_PARAMS_PARSERS.put(Long.class, longValue);
final RequestParamParser doubleValue = new RequestParamParser() {
@Override
public Double from(final String[] strs) {
return Double.valueOf(strs[0]);
}
};
REQUEST_PARAMS_PARSERS.put(double.class, doubleValue);
REQUEST_PARAMS_PARSERS.put(Double.class, doubleValue);
final RequestParamParser boolValue = new RequestParamParser() {
@Override
public Boolean from(final String[] strs) {
return Boolean.valueOf(strs[0]);
}
};
REQUEST_PARAMS_PARSERS.put(boolean.class, boolValue);
REQUEST_PARAMS_PARSERS.put(Boolean.class, boolValue);
REQUEST_PARAMS_PARSERS.put(List.class, new RequestParamParser>() {
@Override
public List from(final String[] strs) {
return Arrays.asList(strs);
}
});
}
@SuppressWarnings("unchecked")
static T parse(final Class> type, final String value) {
if (value == null) return null;
return (T) parse(type, new String[] { value });
}
@SuppressWarnings("unchecked")
static T parse(final Class type, final String[] value) {
if (value == null) return null;
return (T) REQUEST_PARAMS_PARSERS.get(type).from(value);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy