多线程 工具区
@SafeVarargs
@SneakyThrows
public static <T> boolean waitingAllTaskEnd(CompletableFuture<T>... completableFuture) {
boolean waitingAllTask = true;
if (!ObjectUtils.isEmpty(completableFuture)) {
List<CompletableFuture<T>> completableFutureList = new ArrayList<>(Arrays.asList(completableFuture));
while (waitingAllTask) {
Iterator<CompletableFuture<T>> completableFutureIterator = completableFutureList.iterator();
while (completableFutureIterator.hasNext()) {
if (!completableFutureIterator.next().isDone()) {
waitingAllTask = true;
break;
} else {
completableFutureIterator.remove();
waitingAllTask = false;
}
}
Thread.sleep(1000);
}
}
return !waitingAllTask;
}
public static int getHaveThreadCount(){
int cpuCount = Runtime.getRuntime().availableProcessors();
return cpuCount > 4 ? 3 : 2;
}
@SneakyThrows
public static void flagCompletableFutureIsSleep(List<CompletableFuture<Void>> dealPersonFutureList) {
int threadCount = getHaveThreadCount();
boolean isDone = true;
int waitTime = 1000;
if (dealPersonFutureList.size() >= threadCount) {
while (isDone) {
Iterator<CompletableFuture<Void>> iterator = dealPersonFutureList.iterator();
while (iterator.hasNext()) {
if (iterator.next().isDone()) {
isDone = false;
iterator.remove();
break;
}
Thread.sleep(waitTime);
}
}
}
}
数据库 反射区
private static List<String> getFiledNameList(Class<?> daoClass, String daoMethod, Class<?>... parameterTypes) {
List<String> filedNameList = new ArrayList<>();
try {
Method method = daoClass.getDeclaredMethod(daoMethod, parameterTypes);
if (org.apache.commons.lang3.ObjectUtils.allNotNull(method)) {
Query query = method.getDeclaredAnnotation(Query.class);
if (!ObjectUtils.isEmpty(query)) {
String sql = query.value();
if (org.apache.commons.lang3.StringUtils.isNotEmpty(sql)) {
String sqlTemp = sql;
sql = sql.trim().toLowerCase();
int selectIndex = !sql.contains("select") ? sql.indexOf("SELECT") : sql.indexOf("select");
int fromIndex = !sql.contains("from") ? sql.indexOf("FROM") : sql.indexOf("from");
sql = sqlTemp;
String filedStr = sql.substring(selectIndex + 6, fromIndex);
String[] filedStrArr = filedStr.split(",");
for (String s : filedStrArr) {
String filedName = "";
String filedStrTemp = s.trim();
int asIndex = filedStrTemp.contains("as") ? filedStrTemp.indexOf("as") : filedStrTemp.contains("AS") ? filedStrTemp.indexOf("AS") : -1;
if (asIndex != -1) {
filedName = filedStrTemp.substring(asIndex + 2);
} else {
String[] filedStrArrTemp = filedStrTemp.split("\\.");
if (filedStrArrTemp.length > 1) {
filedName = filedStrArrTemp[1].trim();
filedStrArrTemp = filedName.split("\\s+");
if (filedStrArrTemp.length > 1) {
filedName = filedStrArrTemp[1].trim();
}
} else {
filedName = filedStrTemp;
}
}
filedName = filedName.trim();
if (StringUtils.isNotBlank(filedName)) {
filedNameList.add(filedName);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return filedNameList;
}
public static <T> List<T> objArrToListVo(List<?> listDataObj, Class<T> target, Class<?> daoClass, String daoMethod, Class<?>... parameterTypes) {
List<T> listData = new ArrayList<>();
try {
List<String> filedNameList = getFiledNameList(daoClass, daoMethod, parameterTypes);
listDataObj.forEach(row -> {
listData.add(objToVo((Object[]) row, filedNameList, target));
});
} catch (Exception e) {
e.printStackTrace();
}
return listData;
}
public static <T> T objToVo(Object[] data, List<String> filedNameList, Class<T> target) {
T o = null;
try {
o = target.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
int valueIndex = 0;
for (int i = 0; i < filedNameList.size(); i++) {
Object valueObj = data[valueIndex];
if (Objects.isNull(valueObj)) {
continue;
}
Field field = null;
try {
field = target.getDeclaredField(filedNameList.get(i));
} catch (NoSuchFieldException e) {
continue;
}
field.setAccessible(true);
Class<?> type = field.getType();
String simpleName1 = type.getSimpleName();
if ("String".equals(simpleName1)) {
ReflectionUtils.setField(field, o, String.valueOf(valueObj));
} else if ("Short".equals(simpleName1) || "short".equals(simpleName1)) {
ReflectionUtils.setField(field, o, Short.valueOf(String.valueOf(valueObj)));
} else if ("int".equals(simpleName1) || "Integer".equals(simpleName1)) {
ReflectionUtils.setField(field, o, Integer.parseInt(String.valueOf(valueObj)));
} else if ("long".equals(simpleName1) || "Long".equals(simpleName1)) {
ReflectionUtils.setField(field, o, Long.parseLong(String.valueOf(valueObj)));
} else if ("double".equals(simpleName1) || "Double".equals(simpleName1)) {
ReflectionUtils.setField(field, o, Double.parseDouble(String.valueOf(valueObj)));
} else if ("float".equals(simpleName1) || "Float".equals(simpleName1)) {
ReflectionUtils.setField(field, o, Float.parseFloat(String.valueOf(valueObj)));
} else if ("Date".equals(simpleName1) || "date".equals(simpleName1)) {
ReflectionUtils.setField(field, o, (Date) valueObj);
} else if ("LocalDateTime".equals(simpleName1)) {
ReflectionUtils.setField(field, o, (LocalDateTime) valueObj);
} else if ("boolean".equals(simpleName1) || "Boolean".equals(simpleName1)) {
ReflectionUtils.setField(field, o, (boolean) valueObj);
}
valueIndex++;
}
return o;
}
sql语句工具区
List列表转sql(‘in’) 语句
public static <T> String listToJoinSqlStr(List<T> strList, String join) {
StringBuilder joinStr = new StringBuilder();
if (!CollectionUtils.isEmpty(strList)) {
for (int i = 0; i < strList.size(); i++) {
if (i != 0) {
joinStr.append(join);
}
joinStr.append("'").append(strList.get(i)).append("'");
}
}
return joinStr.toString();
}