Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
shz.core.ToList Maven / Gradle / Ivy
package shz.core;
import shz.core.function.ActionRunner;
import shz.core.tag.ixx.ILTag;
import java.lang.reflect.Array;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import java.util.function.*;
import java.util.stream.Collector;
import java.util.stream.Stream;
@SuppressWarnings("unchecked")
public final class ToList {
private final List list;
private ToList(Supplier extends List> supplier) {
list = supplier.get();
}
private static int reduce(int cap, int idx) {
if (idx <= 0 || cap < 3) return cap;
int newCap = cap;
for (int i = idx; i > 0; --i) newCap = (newCap << 1) / 3;
while (calc(newCap, idx) < cap) ++newCap;
return newCap;
}
private static int calc(int newCap, int idx) {
while (idx > 0) {
newCap = newCap + (newCap >> 1);
--idx;
}
return newCap;
}
public static ToList get(Supplier extends List> supplier) {
return new ToList<>(supplier);
}
public static ToList get(int initialCapacity, int idx) {
return get(() -> {
if (initialCapacity <= 0) return new ArrayList<>();
return new ArrayList<>(reduce(initialCapacity, idx));
});
}
public static ToList get(int initialCapacity) {
return get(initialCapacity, 0);
}
public static ToList get() {
return get(0, 0);
}
public > T build() {
return (T) list;
}
public ToList add(Collection extends E> c) {
list.addAll(c);
return this;
}
public ToList add(EE e) {
list.add(e);
return this;
}
public static > Collector collector(Supplier supplier) {
return Collector.of(
supplier,
List::add,
(left, right) -> {
left.addAll(right);
return left;
}
);
}
/**
* 大致确定元素个数时的收集器
*
* @param initialCapacity 大致确定的初始容量
* @param idx 初始容量衰减次数
*/
public static Collector> collector(int initialCapacity, int idx) {
//提供初始化容量的ArraysList
return collector(() -> get(initialCapacity, idx).build());
}
public static Collector> collector(int initialCapacity) {
return collector(initialCapacity, 0);
}
/**
* 大致确定元素个数时的流收集
*/
public static List explicitCollect(Stream extends E> stream, int initialCapacity, int idx) {
List list = stream.collect(collector(initialCapacity, idx));
return list.isEmpty() ? Collections.emptyList() : list;
}
public static List explicitCollect(Stream extends E> stream, int initialCapacity) {
return explicitCollect(stream, initialCapacity, 0);
}
/**
* 无法确定元素个数时的收集器
*/
public static Collector> collector() {
return collector(LinkedList::new);
}
/**
* 无法确定元素个数时的流收集
*
* @param extra 是否需要添加额外元素
*/
public static List collect(Stream extends E> stream, boolean extra) {
List list = stream.collect(collector());
return list.isEmpty()
? extra ? new LinkedList<>() : Collections.emptyList()
: list;
}
public static List collect(Stream extends E> stream) {
return collect(stream, false);
}
/**
* 无法确定元素个数时的流收集(需要频繁的进行索引查询时选择此方法收集流)
*
* @param extra 申请额外空间
*/
public static List collectArray(Stream extends E> stream, int extra) {
int size = Math.max(extra, 0);
List list = stream.collect(collector());
if (list.isEmpty()) return size == 0 ? Collections.emptyList() : new ArrayList<>(size);
if (size == 0) return new ArrayList<>(list);
//转ArrayList 提供更快的索引查询
List result = new ArrayList<>(size + list.size());
result.addAll(list);
return result;
}
public static List collectArray(Stream extends E> stream) {
return collectArray(stream, 0);
}
@SafeVarargs
public static List asList(E... es) {
if (es == null || es.length == 0) return Collections.emptyList();
return Arrays.stream(es).collect(collector(es.length));
}
public static List asList(Object obj) {
if (obj == null) return Collections.emptyList();
if (obj instanceof List) return (List) obj;
if (obj instanceof Collection) {
Collection collections = (Collection) obj;
if (collections.isEmpty()) return Collections.emptyList();
return explicitCollect(collections.stream(), collections.size());
}
if (obj instanceof CharSequence) return (List) asList(obj.toString().split(","));
if (obj.getClass().isArray()) {
int len;
if ((len = Array.getLength(obj)) == 0) return Collections.emptyList();
List result = new ArrayList<>(len);
for (int i = 0; i < len; ++i) result.add((E) Array.get(obj, i));
return result;
}
return get().add(obj).build();
}
public static List collect(ActionRunner runner, Supplier extends List> supplier) {
List list = supplier.get();
runner.accept(list::add);
return list;
}
public static List collect(ActionRunner runner, boolean concurrent) {
return collect(runner, concurrent ? CopyOnWriteArrayList::new : LinkedList::new);
}
public static List collect(ActionRunner runner) {
return collect(runner, false);
}
public static List collect(Collection> sources, Executor executor) {
if (NullHelp.isEmpty(sources)) return Collections.emptyList();
List list = new ArrayList<>(sources.size());
Runner.run(sources, (e, f) -> list.add(e), executor);
return list;
}
public static List collect(Collection> sources) {
return collect(sources, null);
}
public static List topN(ActionRunner runner, int n, Predicate super E> filter, Comparator super E> comparator, boolean concurrent) {
Map> map = ToMap.topN(runner, e -> new ILTag<>(n, 1), filter, comparator, concurrent);
return map.isEmpty() ? Collections.emptyList() : map.values().iterator().next();
}
public static List topN(ActionRunner runner, int n, Predicate super E> filter, Comparator super E> comparator) {
return topN(runner, n, filter, comparator, false);
}
public static List topN(Supplier extends E> supplier, int n, Predicate super E> filter, Comparator super E> comparator) {
return topN(Runner.runner(supplier), n, filter, comparator, false);
}
public static List topN(Collection extends E> dataset, int n, Predicate super E> filter, Comparator super E> comparator) {
return topN(Runner.runner(dataset), n, filter, comparator, false);
}
public static E topOne(ActionRunner runner, Predicate super E> filter, Comparator super E> comparator, boolean concurrent) {
List list = topN(runner, 1, filter, comparator, concurrent);
return list.isEmpty() ? null : list.get(0);
}
public static E topOne(ActionRunner runner, Predicate super E> filter, Comparator super E> comparator) {
return topOne(runner, filter, comparator, false);
}
public static E topOne(Supplier extends E> supplier, Predicate super E> filter, Comparator super E> comparator) {
return topOne(Runner.runner(supplier), filter, comparator, false);
}
public static E topOne(Collection extends E> dataset, Predicate super E> filter, Comparator super E> comparator) {
return topOne(Runner.runner(dataset), filter, comparator, false);
}
public static R merge(ActionRunner runner, Predicate super E> filter, BinaryOperator merger, Function, ? extends R> mapper, boolean concurrent) {
Map map = ToMap.merge(runner, e -> 1, filter, merger, mapper, concurrent);
return map.isEmpty() ? null : map.values().iterator().next();
}
public static R merge(ActionRunner runner, Predicate super E> filter, BinaryOperator merger, Function, ? extends R> mapper) {
return merge(runner, filter, merger, mapper, false);
}
public static R merge(Supplier extends E> supplier, Predicate super E> filter, BinaryOperator merger, Function, ? extends R> mapper) {
return merge(Runner.runner(supplier), filter, merger, mapper, false);
}
public static R merge(Collection extends E> dataset, Predicate super E> filter, BinaryOperator merger, Function, ? extends R> mapper) {
return merge(Runner.runner(dataset), filter, merger, mapper, false);
}
}