import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;
import java.io.File;
import java.lang.reflect.Method;
import java.util.*;
/**
* 通用工具类
*/
public class CommonUtils {
public static Boolean checkFileExist(String filePath){
return new File(filePath).exists();
}
/**
* 读取文件,获取文件中字符串
*
* @param filePath 文件路径,相对,绝对都行
* @return
*/
public static String readFile(String filePath) {
FileReader fileReader = new FileReader(filePath);
return fileReader.readString();
}
/**
* 全量list分页
*
* @param list
* @param page
* @param pSize
* @param <E>
* @return
*/
public static <E> List<E> safeGetPagination(List<E> list, int page, int pSize) {
int start = (page - 1) * pSize;
int offset = page * pSize;
int size = list.size();
if (size == 0 || start >= size || offset <= 0) {
return new ArrayList<E>(0);
}
start = Math.max(start, 0);
List<E> returnList = new ArrayList<E>();
returnList.addAll(list.subList(start, Math.min(size, offset)));
return returnList;
}
public static <E> List<E> mapLikeKey(Map<String, E> map, String k) {
List<E> list = new ArrayList<>();
for (Map.Entry entry : map.entrySet()) {
String key = (String) entry.getKey();
if (key.contains(k)) {
list.add((E) entry.getValue());
}
}
return list;
}
/**
* 对forfind结构进行遍历,propertyname值为value的元素列表并返回,假设找不到,那么返回空集合
* 不支持target的fieldname为null的情况。
*
* @param forFind
* @return
*/
public static <T, S> List<T> findMatchingItemsByProperty(Iterable<T> forFind, S value, String propertyName, Class<S> clazz) {
Object compare1, compare2;
try {
compare1 = value;
} catch (Exception e) {
throw new IllegalArgumentException("please check all arguments, especially fieldName and its getter method.");
}
List<T> returnList = new ArrayList<T>();
for (Iterator iterator = forFind.iterator(); iterator.hasNext(); ) {
T temp = (T) iterator.next();
try {
compare2 = PropertyUtils.getProperty(temp, propertyName);
if ((compare1 == null && compare2 == null) || compare1.equals(compare2)) {
returnList.add(temp);
} else {
continue;
}
} catch (Exception e) {
}
}
//can not find
return returnList;
}
/***
* 根据bean的某个非空属性propertyName获取数据列表, 其中propertyName首字母必须大写
*
* @param list
* @param propertyName
* @return
*/
@SuppressWarnings("unchecked")
public static <T> List<T> safeGetByPropertyValue(List<? extends Object> list, String propertyName) {
if (CollectionUtils.isEmpty(list)) {
return new ArrayList<T>();
}
List<T> retList = new ArrayList<T>(list.size());
Set<T> set = new HashSet<T>(list.size());
Class<?> clz = list.get(0).getClass();
Method mth = getPropertyMethod(clz, propertyName);
for (Object item : list) {
Object value = null;
try {
value = mth.invoke(item);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (value == null) {
continue;
}
set.add((T) value);
}
retList.addAll(set);
return retList;
}
private static Method getPropertyMethod(Class clz, String propertyName) {
Method mth = null;
propertyName = upperFirstChar(propertyName);
try {
mth = clz.getMethod("get" + propertyName);
} catch (Exception var6) {
try {
mth = clz.getMethod("is" + propertyName);
} catch (Exception var5) {
throw new RuntimeException(var5);
}
}
return mth;
}
/**
* 第一个首字母大写
*
* @param source
* @return
*/
private static String upperFirstChar(String source) {
if (StrUtil.isBlank(source)) {
return "";
} else {
Character firstChar = source.charAt(0);
return Character.isLowerCase(firstChar) ? Character.toUpperCase(firstChar) + source.substring(1) : source;
}
}
}
网友评论