com.truthbean.common.mini.util.CollectionUtils Maven / Gradle / Ivy
/**
* Copyright (c) 2021 TruthBean(Rogar·Q)
* Debbie is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.truthbean.common.mini.util;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
/**
* @author TruthBean
* @since 0.5.2
*/
public class CollectionUtils {
private static final Set CH_UNORDERED_ID
= Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.UNORDERED,
Collector.Characteristics.IDENTITY_FINISH));
private static final Set CH_ID
= Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH));
public static Collector> toLinkedHashSet() {
return new CollectorImpl<>((Supplier>) LinkedHashSet::new, Set::add,
(left, right) -> {
left.addAll(right);
return left;
},
CH_UNORDERED_ID);
}
public static Collector> toLinkedList() {
return new CollectorImpl<>((Supplier>) LinkedList::new, List::add,
(left, right) -> {
left.addAll(right);
return left;
},
CH_ID);
}
@SuppressWarnings("unchecked")
private static Function castingIdentity() {
return i -> (R) i;
}
/**
* Simple implementation class for {@code Collector}.
*
* @param the type of elements to be collected
* @param the type of the result
*/
static class CollectorImpl implements Collector {
private final Supplier supplier;
private final BiConsumer accumulator;
private final BinaryOperator combiner;
private final Function finisher;
private final Set characteristics;
CollectorImpl(Supplier supplier,
BiConsumer accumulator,
BinaryOperator combiner,
Function finisher,
Set characteristics) {
this.supplier = supplier;
this.accumulator = accumulator;
this.combiner = combiner;
this.finisher = finisher;
this.characteristics = characteristics;
}
CollectorImpl(Supplier supplier,
BiConsumer accumulator,
BinaryOperator combiner,
Set characteristics) {
this(supplier, accumulator, combiner, castingIdentity(), characteristics);
}
@Override
public BiConsumer accumulator() {
return accumulator;
}
@Override
public Supplier supplier() {
return supplier;
}
@Override
public BinaryOperator combiner() {
return combiner;
}
@Override
public Function finisher() {
return finisher;
}
@Override
public Set characteristics() {
return characteristics;
}
}
/**
* @param list
* @param start 规定从何处开始选取。
* null 表示从0开始
* 如果是负数,那么它规定从数组尾部开始算起的位置。
* 如果该参数为负数,则表示从原数组中的倒数第几个元素开始提取,
* -2, null 表示提取原数组中的倒数第二个元素到最后一个元素(包含最后一个元素)。
* @param end 规定从何处结束选取。
* null 表示到最后一个
* 该参数是数组片断结束处的数组下标。
* 如果没有指定该参数,那么切分的数组包含从 start 到数组结束的所有元素。
* 如果该参数为负数, 则它表示在原数组中的倒数第几个元素结束抽取。
* -2,-1 表示抽取了原数组中的倒数第二个元素到最后一个元素(不包含最后一个元素,也就是只有倒数第二个元素)。
* @param
* @return
*/
public static List slice(List list, Integer start, Integer end) {
List result = new ArrayList<>();
int size = list.size();
if (size == 0) {
return result;
}
if (start == null) {
start = 0;
}
if (end == null) {
end = size - 1;
}
if (start >= 0 && end < size && start < end) {
for (int i = start; i <= end; i++) {
result.add(list.get(i));
}
} else if (start < 0 && end <= 0 && start < end) {
start = size + start;
end = size + end;
for (int i = start; i < end; i++) {
result.add(list.get(i));
}
} else if (start >= 0 && end >= size && start < end) {
for (int i = start; i < size; i++) {
result.add(list.get(i));
}
}
return result;
}
public static boolean include(Collection list, E ele) {
for (E e : list) {
if (e.equals(ele)) {
return true;
}
}
return false;
}
public static boolean include(E[] list, E ele) {
for (E e : list) {
if (e.equals(ele)) {
return true;
}
}
return false;
}
}