public class BeanPlusUtils extends BeanUtils {
public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
return copyListProperties(sources, target, null);
}
public static <S, T> T copySingleProperties(S source, Supplier<T> target) {
return copySingleProperties(source, target, null);
}
public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target,
ColaBeanUtilsCallBack<S, T> callBack) {
if (CollectionUtils.isEmpty(sources)){
return new ArrayList<>();
}
List<T> list = new ArrayList<>(sources.size());
for (S source : sources) {
T t = target.get();
copyProperties(source, t);
list.add(t);
if (callBack != null) {
callBack.callBack(source, t);
}
}
return list;
}
public static <S, T> T copySingleProperties(S source, Supplier<T> target, ColaBeanUtilsCallBack<S, T> callBack) {
T t = target.get();
copyProperties(source, t);
if (callBack != null) {
callBack.callBack(source, t);
}
return t;
}
@FunctionalInterface
public interface ColaBeanUtilsCallBack<S, T> {
void callBack(S t, T s);
}
public static <T> Collection<T> covertObject(Collection<?> collection, Class<T> clazz) {
Collection<T> newCollection = collection.stream().map(oldObject -> {
T instance = null;
try {
instance = clazz.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
BeanUtils.copyProperties(oldObject, instance);
return instance;
}).collect(Collectors.toList());
return newCollection;
};
}