!692 用StreamUtil实现PredicateUtil,补全单测,抑制泛型参数警告

Merge pull request !692 from 阿超/v6-dev
This commit is contained in:
Looly 2022-07-16 15:13:13 +00:00 committed by Gitee
commit ced4e50334
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 45 additions and 88 deletions

View File

@ -1,16 +1,13 @@
package cn.hutool.core.lang.func;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.stream.StreamUtil;
import java.io.Serializable;
import java.util.List;
import java.util.function.Predicate;
/**
* 一些{@link Predicate}相关封装
*
* @author looly
* @author looly VampireAchao
* @since 6.0.0
*/
public class PredicateUtil {
@ -26,8 +23,8 @@ public class PredicateUtil {
* @param components 多个条件
* @return 复合条件
*/
public static <T> Predicate<T> and(final Iterable<? extends Predicate<? super T>> components) {
return new AndPredicate<>(ListUtil.of(components));
public static <T> Predicate<T> and(final Iterable<Predicate<T>> components) {
return StreamUtil.of(components, false).reduce(Predicate::and).orElseGet(() -> o -> true);
}
/**
@ -37,9 +34,9 @@ public class PredicateUtil {
* @param components 多个条件
* @return 复合条件
*/
@SuppressWarnings("unchecked")
public static <T> Predicate<T> and(final Predicate<? super T>... components) {
return new AndPredicate<>(components);
@SafeVarargs
public static <T> Predicate<T> and(final Predicate<T>... components) {
return StreamUtil.of(components).reduce(Predicate::and).orElseGet(() -> o -> true);
}
/**
@ -49,8 +46,8 @@ public class PredicateUtil {
* @param components 多个条件
* @return 复合条件
*/
public static <T> Predicate<T> or(final Iterable<? extends Predicate<? super T>> components) {
return new OrPredicate<>(ListUtil.of(components));
public static <T> Predicate<T> or(final Iterable<Predicate<T>> components) {
return StreamUtil.of(components, false).reduce(Predicate::or).orElseGet(() -> o -> false);
}
/**
@ -60,79 +57,8 @@ public class PredicateUtil {
* @param components 多个条件
* @return 复合条件
*/
@SuppressWarnings("unchecked")
public static <T> Predicate<T> or(final Predicate<? super T>... components) {
return new OrPredicate<>(components);
}
/**
* 多个{@link Predicate}的与只有所有条件满足才为true当任意一个条件的test为false返回false
*
* @param <T> 泛型类型
* @author Guava
*/
private static class AndPredicate<T> implements Predicate<T>, Serializable {
private static final long serialVersionUID = 1L;
private final List<? extends Predicate<? super T>> components;
@SafeVarargs
private AndPredicate(final Predicate<? super T>... components) {
this.components = ListUtil.of(components);
}
private AndPredicate(final List<? extends Predicate<? super T>> components) {
this.components = components;
}
@Override
public boolean test(final T t) {
// Avoid using the Iterator to avoid generating garbage
//noinspection ForLoopReplaceableByForEach
for (int i = 0; i < components.size(); i++) {
if (false == components.get(i).test(t)) {
return false;
}
}
return true;
}
@Override
public String toString() {
return "Predicates.and(" + StrUtil.join(",", this.components) + ")";
}
}
/**
* 多个{@link Predicate}的或操作即当一个条件满足则全部满足当任意一个条件的test为true返回true
*
* @param <T> 泛型类型
* @author Guava
*/
private static class OrPredicate<T> implements Predicate<T>, Serializable {
private static final long serialVersionUID = 1L;
private final List<? extends Predicate<? super T>> components;
@SafeVarargs
private OrPredicate(final Predicate<? super T>... components) {
this.components = ListUtil.of(components);
}
private OrPredicate(final List<? extends Predicate<? super T>> components) {
this.components = components;
}
@Override
public boolean test(final T t) {
// Avoid using the Iterator to avoid generating garbage
//noinspection ForLoopReplaceableByForEach
for (int i = 0; i < components.size(); i++) {
if (false == components.get(i).test(t)) {
return true;
}
}
return false;
}
@SafeVarargs
public static <T> Predicate<T> or(final Predicate<T>... components) {
return StreamUtil.of(components).reduce(Predicate::or).orElseGet(() -> o -> false);
}
}

View File

@ -5,21 +5,52 @@ import org.junit.Assert;
import org.junit.Test;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static cn.hutool.core.lang.func.PredicateUtil.*;
public class PredicateUtilTest {
@Test
public void notContainsTest(){
public void notContainsTest() {
final Set<String> sets = SetUtil.of("1", "2", "3");
final List<String> collect = Stream.of("3", "4", "5")
.filter(PredicateUtil.negate(sets::contains))
.filter(negate(sets::contains))
.collect(Collectors.toList());
Assert.assertEquals(2, collect.size());
Assert.assertEquals("4", collect.get(0));
Assert.assertEquals("5", collect.get(1));
}
@Test
public void andTest() {
boolean condition = Stream.of(1, 3, 5)
.allMatch(
and(
Objects::nonNull,
i -> i < 10,
i -> i % 2 == 1
)
);
Assert.assertTrue(condition);
}
@Test
public void orTest() {
boolean condition = Stream.of(1, 3, 5)
.anyMatch(
or(
Objects::isNull,
i -> i > 10,
i -> i % 2 == 0
)
);
Assert.assertFalse(condition);
}
}