com.terapico.utils.RequestUtil Maven / Gradle / Ivy
package com.terapico.utils;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class RequestUtil {
public static Object getByName(Map requestParameters, String fieldName) {
if (requestParameters == null) {
return null;
}
Object value = requestParameters.get(fieldName);
if (value == null) {
return null;
}
if (value.getClass().isArray()) {
String[] vals = (String[]) value;
if (vals.length < 1) {
return null;
}
}
return value;
}
public static Object getSingleByName(Map requestParameters, String fieldName) {
Object obj = getByName(requestParameters, fieldName);
if (obj == null) {
return obj;
}
if (obj.getClass().isArray()) {
Object[] vals = (Object[]) obj;
if (vals.length < 1) {
return null;
}
return vals[0];
}
if (obj instanceof List) {
if (((List) obj).size() < 1) {
return null;
}
return ((List) obj).get(0);
}
return obj;
}
public static String getStringInput(Map requestParameters, String fieldName, String defaultValue) {
Object value = getSingleByName(requestParameters, fieldName);
if (value == null) {
return defaultValue;
}
return String.valueOf(value).trim();
}
public static Date getDateInput(Map requestParameters, String fieldName, Date defaultValue) {
Object value = getSingleByName(requestParameters, fieldName);
if (value == null) {
return defaultValue;
}
if (value instanceof Number) {
return new Date(((Number) value).longValue());
}
if (value instanceof String && ((String)value).matches("\\d{0,14}")) {
try {
long tsMs = Long.parseLong((String) value);
return new Date(tsMs);
}catch (Exception e) {
// 不行就算了
}
}
Date date = DateTimeUtil.parseInputDateTime(String.valueOf(value));
if (date == null) {
return defaultValue;
}
return date;
}
public static BigDecimal getBigDecimalInput(Map requestParameters, String fieldName,
Number defaultValue) {
Object value = getSingleByName(requestParameters, fieldName);
if (value == null || TextUtil.isBlank(String.valueOf(value))) {
if (defaultValue == null) {
return null;
}
return new BigDecimal(defaultValue.toString());
}
try {
return new BigDecimal(TextUtil.onlyNumber(String.valueOf(value)));
} catch (Exception e) {
if (defaultValue == null) {
return null;
}
if (defaultValue instanceof BigDecimal) {
return (BigDecimal) defaultValue;
}
return new BigDecimal(defaultValue.toString());
}
}
public static boolean getBooleanInput(Map requestParameters, String fieldName,
boolean defaultValue) {
Object value = getSingleByName(requestParameters, fieldName);
if (value == null) {
return defaultValue;
}
try {
return Boolean.parseBoolean(String.valueOf(value));
} catch (Exception e) {
return defaultValue;
}
}
public static Double getDoubleInput(Map requestParameters, String fieldName, Double defaultValue) {
Object value = getSingleByName(requestParameters, fieldName);
if (value == null) {
return defaultValue;
}
try {
return Double.parseDouble(String.valueOf(value));
} catch (Exception e) {
return defaultValue;
}
}
public static Integer getIntegerInput(Map requestParameters, String fieldName, Integer defaultValue) {
Object value = getSingleByName(requestParameters, fieldName);
if (value == null) {
return defaultValue;
}
try {
return Integer.parseInt(String.valueOf(value));
} catch (Exception e) {
// return defaultValue;
}
try {
return (int) Double.parseDouble(String.valueOf(value));
} catch (Exception e) {
return defaultValue;
}
}
public static String pickImageByIndex(String[] images, int idx) {
if(images == null || idx >= images.length) {
return null;
}
return images[idx];
}
public static String[] getImagesInput(Map params, String keyName, Object defaultValue) {
Object obj = getByName(params, keyName);
if (obj == null) {
return null;
}
if (obj instanceof List) {
return ((List) obj).toArray(new String[] {});
}
if (obj.getClass().isArray()) {
return (String[]) obj;
}
return new String[] {String.valueOf(obj)};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy