This commit is contained in:
2023-10-27 00:11:38 +08:00
parent 31e08a2028
commit f7de0a9349
40 changed files with 431 additions and 194 deletions

View File

@@ -1,10 +1,11 @@
package net.tccn.base;
import net.tccn.base.dbq.jdbc.api.DbSource;
import net.tccn.base.dbq.jdbc.api.DbSourceMysql;
import java.lang.reflect.Array;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -112,4 +113,102 @@ public class Utils {
return false;
}
public static String fmt36(int n) {
return Integer.toString(n, 36);
}
public static String fmt36(long n) {
return Long.toString(n, 36);
}
public static <T, V> Map<T, V> toMap(Collection<V> list, Function<V, T> fun) {
Map<T, V> map = new HashMap<>(list.size());
for (V v : list) {
if (v == null) {
continue;
}
map.put(fun.apply(v), v);
}
return map;
}
public static <T, V, T2> Map<T, T2> toMap(Collection<V> list, Function<V, T> fun, Function<V, T2> fun2) {
Map<T, T2> map = new HashMap<>(list.size());
for (V v : list) {
if (v == null) {
continue;
}
map.put(fun.apply(v), fun2.apply(v));
}
return map;
}
public static <T, V> List<V> toList(Collection<T> list, Function<T, V> fun) {
if (list == null || list.isEmpty()) {
return new ArrayList<>();
}
List<V> list1 = new ArrayList<>();
list.forEach(x -> list1.add(fun.apply(x)));
return list1;
}
public static <T, V> Set<V> toSet(Collection<T> list, Function<T, V> fun) {
if (list == null || list.isEmpty()) {
return new HashSet<>();
}
Set<V> set = new HashSet<>(list.size());
list.forEach(x -> set.add(fun.apply(x)));
return set;
}
public static <T> List<T> filter(Collection<T> list, Predicate<T> predicate) {
if (list == null || list.isEmpty()) {
return new ArrayList<>();
}
List<T> list1 = new ArrayList<>(list.size());
list.forEach(x -> {
if (!predicate.test(x)) {
return;
}
list1.add(x);
});
return list1;
}
public static <T, V> List<V> filterToList(Collection<T> list, Predicate<T> predicate, Function<T, V> fun) {
if (list == null || list.isEmpty()) {
return new ArrayList<>();
}
List<V> list1 = new ArrayList<>(list.size());
list.forEach(x -> {
if (!predicate.test(x)) {
return;
}
list1.add(fun.apply(x));
});
return list1;
}
public static <T, V> Map<V, List<T>> group(Collection<T> list, Function<T, V> fun) {
if (list == null || list.isEmpty()) {
return new HashMap<>();
}
return list.stream().collect(Collectors.groupingBy(fun));
}
public static <T, V, K> Map<V, List<K>> group(Collection<T> list, Function<T, V> fun, Function<T, K> fun2) {
if (list == null || list.isEmpty()) {
return new HashMap<>();
}
Map<V, List<T>> group = group(list, fun);
Map<V, List<K>> _group = new HashMap<>();
group.forEach((k, v) -> _group.put(k, toList(v, fun2)));
return _group;
}
}