diff --git a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/All.java b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/All.java index 9456f65..4f1d0ca 100644 --- a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/All.java +++ b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/All.java @@ -12,22 +12,22 @@ * Created on 2023-03-17 下午6:36 * 全部 */ -class All extends Reducer>{ +class All implements Reducer>{ - public All() { + public All() { - } + } - @Override - public List reduce(Collection results) { - if (CollectionUtils.isEmpty(results)) { - return Lists.newArrayList(); - } - return Lists.newArrayList(results); - } + @Override + public List reduce(Collection results) { + if (CollectionUtils.isEmpty(results)) { + return Lists.newArrayList(); + } + return Lists.newArrayList(results); + } - @Override - public ReduceType reduceType() { - return ReduceType.ALL; - } + @Override + public ReduceType reduceType() { + return ReduceType.ALL; + } } diff --git a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/AllMatch.java b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/AllMatch.java index 423693a..32563b1 100644 --- a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/AllMatch.java +++ b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/AllMatch.java @@ -8,7 +8,7 @@ * Created on 2023-03-17 下午6:58 * 全部匹配 */ -class AllMatch extends Reducer { +class AllMatch implements Reducer { private final Predicate predicate; diff --git a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/AnyMatch.java b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/AnyMatch.java index 0f00e35..719e427 100644 --- a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/AnyMatch.java +++ b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/AnyMatch.java @@ -3,16 +3,13 @@ import java.util.Collection; import java.util.function.Predicate; -import lombok.Getter; - /** * 1@author liuzhuo * Created on 2023-03-23 下午9:59 * 任意匹配 */ -class AnyMatch extends Reducer { +class AnyMatch implements Reducer { - @Getter private final Predicate predicate; public AnyMatch(Predicate predicate) { diff --git a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/Collect.java b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/Collect.java index 830161b..c0beee9 100644 --- a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/Collect.java +++ b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/Collect.java @@ -8,16 +8,13 @@ import com.google.common.collect.Lists; -import lombok.Getter; - /** * 1@author liuzhuo * Created on 2023-03-23 下午10:04 * 全部满足条件的结果 */ -class Collect extends Reducer> { +class Collect implements Reducer> { - @Getter private final Predicate predicate; public Collect(Predicate predicate) { diff --git a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FirstOf.java b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FirstOf.java index d093ea9..1c54d17 100644 --- a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FirstOf.java +++ b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FirstOf.java @@ -10,36 +10,45 @@ * Created on 2023-03-17 下午6:29 * 第一个 */ -class FirstOf extends Reducer { - - private Predicate predicate; - - public FirstOf(Predicate predicate) { - this.predicate = predicate; - } - - public FirstOf() { - - } - - @Override - public T reduce(Collection results) { - if (CollectionUtils.isEmpty(results)) { - return null; - } - - for (T result : results) { - if (null == predicate) { - return result; - } else if (predicate.test(result)) { - return result; - } - } - return null; - } - - @Override - public ReduceType reduceType() { - return ReduceType.FIRST; - } +class FirstOf implements Reducer { + + private Predicate predicate; + + public FirstOf(Predicate predicate) { + this.predicate = predicate; + } + + public FirstOf() { + + } + + @Override + public boolean predicate(T result) { + if (null == predicate) { + return true; + } else { + return predicate.test(result); + } + } + + @Override + public T reduce(Collection results) { + if (CollectionUtils.isEmpty(results)) { + return null; + } + + for (T result : results) { + if (null == predicate) { + return result; + } else if (predicate.test(result)) { + return result; + } + } + return null; + } + + @Override + public ReduceType reduceType() { + return ReduceType.FIRST; + } } diff --git a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FlatList.java b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FlatList.java index 186a4e2..b4b19aa 100644 --- a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FlatList.java +++ b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FlatList.java @@ -12,7 +12,7 @@ * 1@author liuzhuo * Created on 2023-03-20 下午7:45 */ -class FlatList extends Reducer, List> { +class FlatList implements Reducer, List> { private final Predicate> predicate; diff --git a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FlatMap.java b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FlatMap.java index be2bd8b..6f8fd45 100644 --- a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FlatMap.java +++ b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FlatMap.java @@ -12,7 +12,7 @@ * 1@author liuzhuo * Created on 2023-03-19 下午8:57 */ -class FlatMap extends Reducer, Map> { +class FlatMap implements Reducer, Map> { private final Predicate> predicate; diff --git a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FlatSet.java b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FlatSet.java index 3ea4c18..437f02d 100644 --- a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FlatSet.java +++ b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/FlatSet.java @@ -13,7 +13,7 @@ * @author liuzhuo * Created on 2023-03-30 下午10:20 */ -class FlatSet extends Reducer, Set> { +class FlatSet implements Reducer, Set> { private final Predicate> predicate; diff --git a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/Reducer.java b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/Reducer.java index 679b992..69a4198 100644 --- a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/Reducer.java +++ b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/Reducer.java @@ -6,9 +6,13 @@ * @author liuzhuo * Created on 2023-03-16 下午7:57 */ -public abstract class Reducer { +public interface Reducer { - public abstract R reduce(Collection results); + R reduce(Collection results); - public abstract ReduceType reduceType(); + default boolean predicate(T result) { + return true; + } + + ReduceType reduceType(); } diff --git a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/Reducers.java b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/Reducers.java index 05a4bdf..d3bcb24 100644 --- a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/Reducers.java +++ b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/Reducers.java @@ -14,6 +14,7 @@ public class Reducers { /** * { "data1" : "a" } , { "data2" : "b" } -{ "data1" : "a" , "data2" : "b" } + * predicate should exclude null value at least * * @param predicate predicate * @param K diff --git a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/VoidReducer.java b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/VoidReducer.java index c6bb75e..d834639 100644 --- a/kbf-core/src/main/java/com/kuaishou/business/core/reduce/VoidReducer.java +++ b/kbf-core/src/main/java/com/kuaishou/business/core/reduce/VoidReducer.java @@ -6,7 +6,7 @@ * @author liuzhuo * Created on 2023-04-03 下午7:44 */ -abstract class VoidReducer extends Reducer { +abstract class VoidReducer implements Reducer { @Override public Void reduce(Collection results) { diff --git a/kbf-engine/src/main/java/com/kuaishou/business/extension/engine/Executor.java b/kbf-engine/src/main/java/com/kuaishou/business/extension/engine/Executor.java index f3801ff..6b59d19 100644 --- a/kbf-engine/src/main/java/com/kuaishou/business/extension/engine/Executor.java +++ b/kbf-engine/src/main/java/com/kuaishou/business/extension/engine/Executor.java @@ -3,7 +3,6 @@ import java.util.List; import java.util.Set; import java.util.function.Supplier; -import java.util.stream.Collectors; import org.apache.commons.collections4.CollectionUtils; @@ -12,6 +11,7 @@ import com.kuaishou.business.core.function.ExtCallback; import com.kuaishou.business.core.identity.manage.KbfRealizeItem; import com.kuaishou.business.core.identity.manage.NormalProductItem; +import com.kuaishou.business.core.reduce.ReduceType; import com.kuaishou.business.core.reduce.Reducer; import com.kuaishou.business.core.session.KSessionScope; @@ -31,21 +31,29 @@ public R execute(Class extClz, String metho List instanceList = SimpleKExtPointManger.getInstance(kbfRealizeItemList, extClz, methodName); + return execAndHandlerResults(extMethod, defaultMethod, reducer, instanceList); + } + + private static R execAndHandlerResults(ExtCallback extMethod, + Supplier defaultMethod, Reducer reducer, List instanceList) { if (CollectionUtils.isEmpty(instanceList)) { return reducer.reduce(Lists.newArrayList(defaultMethod.get())); } - switch (reducer.reduceType()) { - case FIRST: - //以扩展点优先 - return reducer.reduce(Lists.newArrayList(extMethod.apply(instanceList.get(0)))); - case ALL: - List results = instanceList.stream().map(extMethod).collect(Collectors.toList()); - T defaultMethodResult = defaultMethod.get(); - results.add(defaultMethodResult); - return reducer.reduce(results); - default: - return null; + List results = Lists.newArrayList(); + boolean reduceFirst = ReduceType.FIRST.equals(reducer.reduceType()); + for (Ext instance : instanceList) { + T result = extMethod.apply(instance); + if (reduceFirst) { + if (reducer.predicate(result)) { + return reducer.reduce(Lists.newArrayList(result)); + } + } else { + results.add(result); + } } + T defaultMethodResult = defaultMethod.get(); + results.add(defaultMethodResult); + return reducer.reduce(results); } public abstract

Set recognize(P request); diff --git a/kbf-samples/kbf-common-sample/pom.xml b/kbf-samples/kbf-common-sample/pom.xml index 9f15891..4ddcb8d 100644 --- a/kbf-samples/kbf-common-sample/pom.xml +++ b/kbf-samples/kbf-common-sample/pom.xml @@ -54,5 +54,11 @@ spring-boot-autoconfigure 2.5.15 + + junit + junit + 4.13.1 + test + \ No newline at end of file diff --git a/kbf-samples/kbf-common-sample/src/test/java/ReduceSampleTest.java b/kbf-samples/kbf-common-sample/src/test/java/ReduceSampleTest.java new file mode 100644 index 0000000..6794b53 --- /dev/null +++ b/kbf-samples/kbf-common-sample/src/test/java/ReduceSampleTest.java @@ -0,0 +1,106 @@ +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +import org.junit.Assert; +import org.junit.Test; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import com.kuaishou.business.core.reduce.Reducer; +import com.kuaishou.business.core.reduce.Reducers; + +/** + * @author liuzhuo07 + * Created on 2024-09-05 + */ +public class ReduceSampleTest { + + @Test + public void all() { + Reducer> all = Reducers.all(); + List result = all.reduce(Lists.newArrayList(1, 2, 3)); + Assert.assertEquals(result, Lists.newArrayList(1, 2, 3)); + } + + @Test + public void allMatch() { + Reducer allMatch1 = Reducers.allMatch(); + Boolean result1 = allMatch1.reduce(Lists.newArrayList(true, false, true)); + Assert.assertEquals(result1, false); + + Reducer allMatch2 = Reducers.allMatch(i -> i % 2 == 0); + Boolean result2 = allMatch2.reduce(Lists.newArrayList(1, 2, 3)); + Assert.assertEquals(result2, false); + + Boolean result3 = allMatch2.reduce(Lists.newArrayList(2, 4, 6)); + Assert.assertEquals(result3, true); + } + + @Test + public void anyMatch() { + Reducer anyMatch1 = Reducers.anyMatch(); + Boolean result1 = anyMatch1.reduce(Lists.newArrayList(true, false, false)); + Assert.assertEquals(result1, true); + + Reducer anyMatch2 = Reducers.anyMatch(i -> i % 2 == 0); + Boolean result2 = anyMatch2.reduce(Lists.newArrayList(1, 2, 3)); + Assert.assertEquals(result2, true); + + Boolean result3 = anyMatch2.reduce(Lists.newArrayList(1, 3, 5)); + Assert.assertEquals(result3, false); + } + + @Test + public void collect() { + Reducer> collect = Reducers.collect(i -> i % 2 == 0); + List result = collect.reduce(Lists.newArrayList(1, 2, 3)); + + Assert.assertEquals(result, Lists.newArrayList(2)); + } + + @Test + public void firstOf() { + Reducer first = Reducers.first(i -> i % 2 == 0); + Integer result = first.reduce(Lists.newArrayList(1, 2, 3)); + + Integer expectResult = 2; + Assert.assertEquals(result, expectResult); + } + + @Test + public void flatList() { + Reducer, List> flatList = Reducers.flatList(Objects::nonNull); + List array1 = Lists.newArrayList(1, 2, 3); + List array2 = null; + List array3 = Lists.newArrayList(3, 4, 5); + List result = flatList.reduce(Lists.newArrayList(array1, array2, array3)); + + Assert.assertEquals(result, Lists.newArrayList(1, 2, 3, 3, 4, 5)); + } + + @Test + public void flatMap() { + Reducer, Map> flatMap = Reducers.flatMap(Objects::nonNull); + + Map map1 = ImmutableMap.of("data1", "a"); + Map map2 = null; + Map map3 = ImmutableMap.of("data2", "b"); + Map result = flatMap.reduce(Lists.newArrayList(map1, map2, map3)); + + Assert.assertEquals(result, ImmutableMap.of("data1", "a", "data2", "b")); + } + + @Test + public void flatSet() { + Reducer, Set> flatSet = Reducers.flatSet(Objects::nonNull); + List array1 = Lists.newArrayList(1, 2, 3); + List array2 = null; + List array3 = Lists.newArrayList(3, 4, 5); + Set result = flatSet.reduce(Lists.newArrayList(array1, array2, array3)); + + Assert.assertEquals(result, Sets.newHashSet(1, 2, 3, 4, 5)); + } +}