mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
73ee346bb6
commit
31e5eead89
@ -7,7 +7,6 @@ import cn.hutool.core.stream.CollectorUtil;
|
||||
import cn.hutool.core.stream.StreamUtil;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@ -22,7 +21,7 @@ import java.util.stream.Collectors;
|
||||
/**
|
||||
* 集合的stream操作封装
|
||||
*
|
||||
* @author 528910437@QQ.COM, VampireAchao<achao1441470436@gmail.com>
|
||||
* @author 528910437@QQ.COM, VampireAchao<achao1441470436@gmail.com>;Lion Li>
|
||||
* @since 5.5.2
|
||||
*/
|
||||
public class CollStreamUtil {
|
||||
@ -55,7 +54,7 @@ public class CollStreamUtil {
|
||||
*/
|
||||
public static <V, K> Map<K, V> toIdentityMap(final Collection<V> collection, final Function<V, K> key, final boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
return MapUtil.zero();
|
||||
}
|
||||
return toMap(collection, (v) -> Opt.ofNullable(v).map(key).get(), Function.identity(), isParallel);
|
||||
}
|
||||
@ -88,7 +87,7 @@ public class CollStreamUtil {
|
||||
*/
|
||||
public static <E, K, V> Map<K, V> toMap(final Collection<E> collection, final Function<E, K> key, final Function<E, V> value, final boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
return MapUtil.zero();
|
||||
}
|
||||
return StreamUtil.of(collection, isParallel)
|
||||
.collect(HashMap::new, (m, v) -> m.put(key.apply(v), value.apply(v)), HashMap::putAll);
|
||||
@ -122,7 +121,7 @@ public class CollStreamUtil {
|
||||
*/
|
||||
public static <E, K> Map<K, List<E>> groupByKey(final Collection<E> collection, final Function<E, K> key, final boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
return MapUtil.zero();
|
||||
}
|
||||
return groupBy(collection, key, Collectors.toList(), isParallel);
|
||||
}
|
||||
@ -160,7 +159,7 @@ public class CollStreamUtil {
|
||||
public static <E, K, U> Map<K, Map<U, List<E>>> groupBy2Key(final Collection<E> collection, final Function<E, K> key1,
|
||||
final Function<E, U> key2, final boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
return MapUtil.zero();
|
||||
}
|
||||
return groupBy(collection, key1, CollectorUtil.groupingBy(key2, Collectors.toList()), isParallel);
|
||||
}
|
||||
@ -197,7 +196,7 @@ public class CollStreamUtil {
|
||||
public static <E, T, U> Map<T, Map<U, E>> group2Map(final Collection<E> collection,
|
||||
final Function<E, T> key1, final Function<E, U> key2, final boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection) || key1 == null || key2 == null) {
|
||||
return Collections.emptyMap();
|
||||
return MapUtil.zero();
|
||||
}
|
||||
return groupBy(collection, key1, CollectorUtil.toMap(key2, Function.identity(), (l, r) -> l), isParallel);
|
||||
}
|
||||
@ -235,7 +234,7 @@ public class CollStreamUtil {
|
||||
public static <E, K, V> Map<K, List<V>> groupKeyValue(final Collection<E> collection, final Function<E, K> key,
|
||||
final Function<E, V> value, final boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
return MapUtil.zero();
|
||||
}
|
||||
return groupBy(collection, key, Collectors.mapping(v -> Opt.ofNullable(v).map(value).orElse(null), Collectors.toList()), isParallel);
|
||||
}
|
||||
@ -254,7 +253,7 @@ public class CollStreamUtil {
|
||||
*/
|
||||
public static <E, K, D> Map<K, D> groupBy(final Collection<E> collection, final Function<E, K> key, final Collector<E, ?, D> downstream) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
return MapUtil.zero();
|
||||
}
|
||||
return groupBy(collection, key, downstream, false);
|
||||
}
|
||||
@ -275,7 +274,7 @@ public class CollStreamUtil {
|
||||
*/
|
||||
public static <E, K, D> Map<K, D> groupBy(final Collection<E> collection, final Function<E, K> key, final Collector<E, ?, D> downstream, final boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
return MapUtil.zero();
|
||||
}
|
||||
return StreamUtil.of(collection, isParallel).collect(CollectorUtil.groupingBy(key, downstream));
|
||||
}
|
||||
@ -307,7 +306,7 @@ public class CollStreamUtil {
|
||||
*/
|
||||
public static <E, T> List<T> toList(final Collection<E> collection, final Function<E, T> function, final boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyList();
|
||||
return ListUtil.zero();
|
||||
}
|
||||
return StreamUtil.of(collection, isParallel)
|
||||
.map(function)
|
||||
@ -342,7 +341,7 @@ public class CollStreamUtil {
|
||||
*/
|
||||
public static <E, T> Set<T> toSet(final Collection<E> collection, final Function<E, T> function, final boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptySet();
|
||||
return SetUtil.zero();
|
||||
}
|
||||
return StreamUtil.of(collection, isParallel)
|
||||
.map(function)
|
||||
@ -365,11 +364,11 @@ public class CollStreamUtil {
|
||||
*/
|
||||
public static <K, X, Y, V> Map<K, V> merge(Map<K, X> map1, Map<K, Y> map2, final BiFunction<X, Y, V> merge) {
|
||||
if (MapUtil.isEmpty(map1) && MapUtil.isEmpty(map2)) {
|
||||
return Collections.emptyMap();
|
||||
return MapUtil.zero();
|
||||
} else if (MapUtil.isEmpty(map1)) {
|
||||
map1 = Collections.emptyMap();
|
||||
map1 = MapUtil.empty();
|
||||
} else if (MapUtil.isEmpty(map2)) {
|
||||
map2 = Collections.emptyMap();
|
||||
map2 = MapUtil.empty();
|
||||
}
|
||||
final Set<K> key = new HashSet<>();
|
||||
key.addAll(map1.keySet());
|
||||
|
@ -214,6 +214,17 @@ public class ListUtil {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个初始大小为0的List,这个空List可变
|
||||
*
|
||||
* @param <T> 元素类型
|
||||
* @return 空的List
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static <T> List<T> zero() {
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新建一个CopyOnWriteArrayList
|
||||
*
|
||||
|
@ -175,6 +175,17 @@ public class SetUtil {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个初始大小为0的Set,这个空Set可变
|
||||
*
|
||||
* @param <T> 元素类型
|
||||
* @return 空的List
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static <T> Set<T> zero() {
|
||||
return new HashSet<>(0, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为只读Set
|
||||
*
|
||||
|
@ -1351,6 +1351,17 @@ public class MapUtil {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回一个初始大小为0的HashMap(初始为0,可加入元素)
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @return 初始大小为0的HashMap
|
||||
*/
|
||||
public static <K, V> Map<K, V> zero() {
|
||||
return new HashMap<>(0, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据传入的Map类型不同,返回对应类型的空Map,支持类型包括:
|
||||
*
|
||||
|
@ -140,7 +140,7 @@ public class PatternPool {
|
||||
public final static Pattern PLATE_NUMBER = Pattern.compile(RegexPool.PLATE_NUMBER);
|
||||
|
||||
/**
|
||||
* 社会统一信用代码
|
||||
* 统一社会信用代码
|
||||
* <pre>
|
||||
* 第一部分:登记管理部门代码1位 (数字或大写英文字母)
|
||||
* 第二部分:机构类别代码1位 (数字或大写英文字母)
|
||||
@ -197,7 +197,7 @@ public class PatternPool {
|
||||
*/
|
||||
public static Pattern get(final String regex, final int flags) {
|
||||
final RegexWithFlag regexWithFlag = new RegexWithFlag(regex, flags);
|
||||
return POOL.computeIfAbsent(regexWithFlag, (key)-> Pattern.compile(regex, flags));
|
||||
return POOL.computeIfAbsent(regexWithFlag, (key) -> Pattern.compile(regex, flags));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -150,7 +150,7 @@ public interface RegexPool {
|
||||
"([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$";
|
||||
|
||||
/**
|
||||
* 社会统一信用代码
|
||||
* 统一社会信用代码
|
||||
* <pre>
|
||||
* 第一部分:登记管理部门代码1位 (数字或大写英文字母)
|
||||
* 第二部分:机构类别代码1位 (数字或大写英文字母)
|
||||
|
@ -722,6 +722,26 @@ public class CharSequenceUtil extends StrChecker{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查指定字符串中是否含给定的所有字符串
|
||||
*
|
||||
* @param str 字符串
|
||||
* @param testChars 检查的字符
|
||||
* @return 字符串含有非检查的字符,返回false
|
||||
* @since 4.4.1
|
||||
*/
|
||||
public static boolean containsAll(CharSequence str, CharSequence... testChars) {
|
||||
if (isBlank(str) || ArrayUtil.isEmpty(testChars)) {
|
||||
return false;
|
||||
}
|
||||
for (CharSequence testChar : testChars) {
|
||||
if (false == contains(str, testChar)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------ indexOf
|
||||
|
||||
/**
|
||||
|
@ -160,4 +160,10 @@ public class CharSequenceUtilTest {
|
||||
a = null;
|
||||
Assert.assertNull(CharSequenceUtil.trimToNull(a));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void containsAllTest() {
|
||||
final String a = "2142342422423423";
|
||||
Assert.assertTrue(StrUtil.containsAll(a, "214", "234"));
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ import java.nio.charset.Charset;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* http请求类<br>
|
||||
@ -1009,8 +1010,22 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
* @since 5.7.8
|
||||
*/
|
||||
public void then(final Consumer<HttpResponse> consumer) {
|
||||
thenFunction(httpResponse -> {
|
||||
consumer.accept(httpResponse);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行Request请求后,对响应内容后续处理<br>
|
||||
* 处理结束后关闭连接
|
||||
*
|
||||
* @param function 响应内容处理函数
|
||||
* @since 5.8.5
|
||||
*/
|
||||
public <T> T thenFunction(final Function<HttpResponse, T> function) {
|
||||
try (final HttpResponse response = execute(true)) {
|
||||
consumer.accept(response);
|
||||
return function.apply(response);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user