cn.kduck.core.utils.ValueMapUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kduck-core Show documentation
Show all versions of kduck-core Show documentation
The core of the K-Duck development framework encompasses all the featured components of the framework.
package cn.kduck.core.utils;
import cn.kduck.core.service.ValueMap;
import cn.kduck.core.service.ValueMapList;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
/**
* LiuHG
*/
public final class ValueMapUtils {
private ValueMapUtils(){}
public static String getValueAsString(Map valueMap,String name) {
Object v = valueMap.get(name);
return getValue(v,String.class);
}
public static Integer getValueAsInteger(Map valueMap,String name) {
Object v = valueMap.get(name);
return getValue(v,Integer.class);
}
public static Long getValueAsLong(Map valueMap,String name) {
Object v = valueMap.get(name);
return getValue(v,Long.class);
}
public static int getValueAsInt(Map valueMap,String name) {
Integer v = getValueAsInteger(valueMap,name);
return v == null ? 0 : v.intValue();
}
public static Date getValueAsDate(Map valueMap,String name) {
Object v = valueMap.get(name);
return getValue(v,Date.class);
}
public static Double getValueAsDouble(Map valueMap,String name) {
Object v = valueMap.get(name);
return getValue(v,Double.class);
}
public static boolean getValueAsBool(Map valueMap,String name) {
Boolean v = getValueAsBoolean(valueMap,name);
return v == null ? false : v.booleanValue();
}
public static Float getValueAsFloat(Map valueMap,String name) {
Object v = valueMap.get(name);
return getValue(v,Float.class);
}
public static Boolean getValueAsBoolean(Map valueMap,String name) {
Object v = valueMap.get(name);
return getValue(v,Boolean.class,false);
}
public static List getValueAsList(Map valueMap,String name) {
Object v = valueMap.get(name);
if(v == null){
return Collections.emptyList();
}
if(v instanceof List){
return (List)v;
}
return Collections.singletonList(v);
}
public static T[] getValueAsArray(Map valueMap,String name,Class clazz){
Object v = valueMap.get(name);
if(v != null){
if(v.getClass().isArray()) {
return (T[])v;
}if(v instanceof Collection) {
Collection collection = (Collection) v;
T[] vArray = (T[]) Array.newInstance(clazz,collection.size());
int i = 0;
for (Object o : collection) {
vArray[i++] = ConversionUtils.convert(o,clazz);
}
return vArray;
}else{
T[] vArray = (T[]) Array.newInstance(clazz,1);
vArray[0] = ConversionUtils.convert(v,clazz);
return vArray;
}
}
return (T[]) Array.newInstance(clazz,0);
}
public static Map getValueAsMap(Map valueMap,String name) {
Object v = valueMap.get(name);
if(v == null){
return Collections.emptyMap();
}
if(v instanceof Map){
return (Map)v;
}
HashMap hashMap = new HashMap();
hashMap.put(name,v);
return hashMap;
}
public static R convert(Function bean, T valueMap){
if(valueMap == null){
return null;
}
return bean.apply(valueMap);
}
public static Map