2025-08-27 00:09:20 +08:00

202 lines
6.1 KiB
Java

package com.heyu.api.common;
import com.alibaba.fastjson.JSON;
import com.heyu.api.common.test.AriseUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ClassUtils;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class SanUtils {
public static <T, R, V> Map<R, V> list2Map(List<V> values, LBiFunction0<T, R> function) {
try {
if (CollectionUtils.isEmpty(values)) {
return new HashMap<>();
}
Map<R, V> map = new HashMap<>();
Method method = getMethod(function);
for (V value : values) {
map.put((R) method.invoke(value), value);
}
return map;
} catch (Exception e) {
e.printStackTrace();
}
return new HashMap<>();
}
public static <T, R, V> List<R> list2listFilterNull(List<V> values, LBiFunction0<T, R> function) {
List<R> list = list2list(values, function);
List<R> result = new ArrayList<>();
if(CollectionUtils.isEmpty(list)){
for (R r : list) {
if(r!=null){
result.add(r);
}
}
}
return result;
}
public static <T, R, V> List<R> list2list(List<V> values, LBiFunction0<T, R> function) {
List<R> list = new ArrayList<>();
try {
if (CollectionUtils.isEmpty(values)) {
return list;
}
Map<R, V> map = new HashMap<>();
Method method = getMethod(function);
for (V value : values) {
list.add((R) method.invoke(value));
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public static <T, R, V> Map<R, List<V>> list2GroupBy(List<V> values, LBiFunction0<T, R> function) {
Map<R, List<V>> listmap = new HashMap<>();
Method method = getMethod(function);
try {
for (int i = 0; i < values.size(); i++) {
V v = values.get(i);
R r = (R) method.invoke(v);
List<V> list = listmap.get(r);
if (list == null) {
list = new ArrayList<>();
}
list.add(v);
listmap.put(r, list);
}
} catch (Exception e) {
e.printStackTrace();
}
return listmap;
}
private static <T, R> Method getMethod(LBiFunction0<T, R> function) {
try {
Method method = function.getClass().getDeclaredMethods()[1];
method.setAccessible(true);
SerializedLambda serializedLambda = (SerializedLambda) method.invoke(function);
String methodName = serializedLambda.getImplMethodName();
String className = serializedLambda.getImplClass().replace("/", ".");
Class<?> clazz = Class.forName(className);
Method methods[] = clazz.getDeclaredMethods();
for (Method m : methods) {
if (m.getName().equals(methodName)) {
return m;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static <T, R> List<Method> getMethods(LBiFunction0<T, R> function) {
try {
Method method = function.getClass().getDeclaredMethods()[1];
method.setAccessible(true);
SerializedLambda serializedLambda = (SerializedLambda) method.invoke(function);
String methodName = serializedLambda.getImplMethodName();
String className = serializedLambda.getImplClass().replace("/", ".");
Class<?> clazz = Class.forName(className);
List<Method> list = new ArrayList<>();
Method methods[] = clazz.getDeclaredMethods();
for (Method m : methods) {
if (m.getName().equals(methodName)) {
list.add(m);
}
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static Method getRealMethod(List<Method> methods, Class paramClazz) {
for (Method method : methods) {
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes != null
&& parameterTypes.length == 1
&& ClassUtils.isAssignable(paramClazz, parameterTypes[0])) {
return method;
}
}
return null;
}
public static <T, R, V> List<R> convert(List<V> list, LBiFunction0<T, R> function) {
List<Method> methods = getMethods(function);
List result = new ArrayList<>();
for (V v : list) {
try {
Class clazz = v.getClass();
Method method = getRealMethod(methods, clazz);
int modifiers = method.getModifiers();
Object object = null;
if (Modifier.isStatic(modifiers)) {
object = method.invoke(null, v);
} else {
object = method.invoke(v);
}
result.add(object);
} catch (Exception e) {
log.error("convert exception", e);
}
}
return result;
}
public static void main(String[] args) {
AriseUser ai = new AriseUser();
ai.setName("张三");
ai.setAge(13);
AriseUser ai2 = new AriseUser();
ai2.setName("李四");
ai2.setAge(23);
AriseUser ai3 = new AriseUser();
ai3.setName("李四");
ai3.setAge(25);
List<AriseUser> list = new ArrayList<>();
list.add(ai);
list.add(ai2);
list.add(ai3);
Map<String, List<AriseUser>> map = list2GroupBy(list, AriseUser::getName);
System.out.println(JSON.toJSONString(map));
}
}