package org.monitor.util;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* 对两个list做copy工作
* 对MybatisPlus里面的元素封装
**/
public class ListUtil<S,T> {
public static <S,T> void copyList(Collection<S> sCollection,Collection<T> tCollection,Class<T> tClass) {
if(sCollection==null||tCollection==null||sCollection.size()==0){
return;
}
try {
for(S s:sCollection){
T t = tClass.newInstance();
BeanUtils.copyProperties(s,t);
tCollection.add(t);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static <S,T> void copyIPage(IPage<S> siPage,IPage<T> tiPage,Class<T> tClass){
if(siPage==null||tiPage==null){
return;
}
tiPage.setCurrent(siPage.getCurrent());
tiPage.setPages(siPage.getPages());
tiPage.setSize(siPage.getSize());
tiPage.setTotal(siPage.getTotal());
List<S> sList= siPage.getRecords();
List<T> tList=new ArrayList<>();
copyList(sList,tList,tClass);
tiPage.setRecords(tList);
}
/**
* 将list以某个符号分隔成字符串
* @param list 字符串列表
* @param symbol 符号
* @return
*/
public static String getStringByList(List<String> list,String symbol){
if(Func.isEmpty(list)){
return "";
}
StringBuilder stringBuilder=new StringBuilder();
for(int i=0;i<list.size();i++){
if(i!=list.size()-1){
stringBuilder.append(list.get(i)).append(symbol);
}else{
stringBuilder.append(list.get(i));
}
}
return stringBuilder.toString();
}
/**
* 将set以某个符号分隔成字符串
* @param set set
* @param symbol 符号
* @return
*/
public static String getStringBySet(Set<String> set, String symbol){
List<String> stringList=new ArrayList<>(set);
return getStringByList(stringList,symbol);
}
public static List<String> getListByString(String s){
List<String> list=new ArrayList<>();
if(Func.isEmpty(s)){
return list;
}
String[] array=s.split(",");
list=Arrays.asList(array);
return list;
}
public static List<String> getListByString(String s,String symbol){
List<String> list=new ArrayList<>();
if(Func.isEmpty(s)){
return list;
}
String[] array=s.split(symbol);
list=Arrays.asList(array);
return list;
}
public static <S,T> void copyList(Collection<S> sCollection, Collection<T> tCollection, Class<T> tClass) {
if(sCollection==null||tCollection==null||sCollection.size()==0){
return;
}
try {
for(S s:sCollection){
T t = tClass.newInstance();
BeanUtils.copyProperties(s,t);
tCollection.add(t);
}
} catch (Exception e) {
log.error("列表转换失败",e);
}
}
public static <S,T> List<T> copyList(Collection<S> sCollection,Class<T> tClass) {
List<T> list=new ArrayList<>();
if(sCollection==null||sCollection.size()==0){
return list;
}
try {
for(S s:sCollection){
T t = tClass.newInstance();
BeanUtils.copyProperties(s,t);
list.add(t);
}
} catch (Exception e) {
log.error("列表转换失败",e);
}
return list;
}
}
List泛型封装实现两个不同实体类属性list之间的复制,同时实现了对mybatis plus IPage对象的复制
于 2021-04-28 17:25:39 首次发布