This commit is contained in:
2019-04-16 22:26:23 +08:00
parent 52f5214bbd
commit 7c3c24dcb4
5 changed files with 48 additions and 2 deletions

View File

@@ -10,6 +10,7 @@ import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
/**
* Created by liangxianyou@eversec.cn at 2018/3/12 14:17.
@@ -28,6 +29,32 @@ public class Kv<K,V> extends LinkedHashMap<K,V> {
return this;
}
// 将obj 属性映射到Kv 中
public static Kv toKv(Object m, String ... fields) {
Kv kv = Kv.of();
Stream.of(fields).forEach(field -> {
String filedT = field;
String filedS = field;
try {
if (field.contains("=")) {
String[] arr = field.split("=");
filedT = arr[0];
filedS= arr[1];
}
Method method = m.getClass().getDeclaredMethod("get" + Liangs.toUpperCaseFirst(filedS));
if (method != null) {
kv.set(filedT, method.invoke(m));
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
new IllegalArgumentException(String.format("Kv.toKv获取 获取参数[]失败", field), e);
}
});
return kv;
}
public static Kv toKv(Object m) {
return toKv(m, Kv.of(), m.getClass());
}

View File

@@ -1,5 +1,6 @@
package net.tccn.base;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -7,7 +8,7 @@ import java.util.stream.Stream;
/**
* Created by liangxianyou at 2019/3/19 18:01.
*/
public class UtilityExt {
public class Liangs {
public static <T> Set<T> streamConcat(Stream<T> ... streams) {
Stream<T> stream = Stream.empty();
@@ -19,5 +20,9 @@ public class UtilityExt {
return stream.collect(Collectors.toSet());
}
public static String toUpperCaseFirst(String str) {
Objects.requireNonNull(str);
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
}

View File

@@ -433,7 +433,7 @@ public class MetaKit {
Set<String> allAlias;
if (!all) {
allAlias = UtilityExt.streamConcat(
allAlias = Liangs.streamConcat(
metaService.getFilters().stream().map(f -> {
String col = (String) f.get("name");
String alias = col.split("[.]")[0];

View File

@@ -167,8 +167,10 @@ public class RunTest<T> {
map.put("abx", 123);
UserBean user = Kv.toBean(map, UserBean.class);
Kv kv = Kv.toKv(user, "name", "abxx=age");
System.out.println(user);
System.out.println(kv);
}
@Test

View File

@@ -18,6 +18,18 @@ public class UserBean {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public float getAttr() {
return attr;
}
@Override
public String toString() {
return "User{" +