Skip to content

Commit

Permalink
Merge pull request #1 from BaskBuLL/feature_revise_instance_handle
Browse files Browse the repository at this point in the history
feature_revise_instance_handle 优化实例执行结果处理
  • Loading branch information
iMouseWu authored Sep 5, 2024
2 parents 04afcc6 + 4d32de4 commit c93eccd
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 74 deletions.
28 changes: 14 additions & 14 deletions kbf-core/src/main/java/com/kuaishou/business/core/reduce/All.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
* Created on 2023-03-17 下午6:36
* 全部
*/
class All<T> extends Reducer<T, List<T>>{
class All<T> implements Reducer<T, List<T>>{

public All() {
public All() {

}
}

@Override
public List<T> reduce(Collection<T> results) {
if (CollectionUtils.isEmpty(results)) {
return Lists.newArrayList();
}
return Lists.newArrayList(results);
}
@Override
public List<T> reduce(Collection<T> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Created on 2023-03-17 下午6:58
* 全部匹配
*/
class AllMatch<T> extends Reducer<T, Boolean> {
class AllMatch<T> implements Reducer<T, Boolean> {

private final Predicate<T> predicate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> extends Reducer<T, Boolean> {
class AnyMatch<T> implements Reducer<T, Boolean> {

@Getter
private final Predicate<T> predicate;

public AnyMatch(Predicate<T> predicate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> extends Reducer<T, List<T>> {
class Collect<T> implements Reducer<T, List<T>> {

@Getter
private final Predicate<T> predicate;

public Collect(Predicate<T> predicate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,45 @@
* Created on 2023-03-17 下午6:29
* 第一个
*/
class FirstOf<T> extends Reducer<T, T> {

private Predicate<T> predicate;

public FirstOf(Predicate<T> predicate) {
this.predicate = predicate;
}

public FirstOf() {

}

@Override
public T reduce(Collection<T> 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<T> implements Reducer<T, T> {

private Predicate<T> predicate;

public FirstOf(Predicate<T> 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<T> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* 1@author liuzhuo
* Created on 2023-03-20 下午7:45
*/
class FlatList<T> extends Reducer<List<T>, List<T>> {
class FlatList<T> implements Reducer<List<T>, List<T>> {

private final Predicate<List<T>> predicate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* 1@author liuzhuo
* Created on 2023-03-19 下午8:57
*/
class FlatMap<K, V> extends Reducer<Map<K, V>, Map<K, V>> {
class FlatMap<K, V> implements Reducer<Map<K, V>, Map<K, V>> {

private final Predicate<Map<K, V>> predicate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @author liuzhuo
* Created on 2023-03-30 下午10:20
*/
class FlatSet<T> extends Reducer<List<T>, Set<T>> {
class FlatSet<T> implements Reducer<List<T>, Set<T>> {

private final Predicate<List<T>> predicate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
* @author liuzhuo
* Created on 2023-03-16 下午7:57
*/
public abstract class Reducer<T, R> {
public interface Reducer<T, R> {

public abstract R reduce(Collection<T> results);
R reduce(Collection<T> results);

public abstract ReduceType reduceType();
default boolean predicate(T result) {
return true;
}

ReduceType reduceType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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> K
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @author liuzhuo
* Created on 2023-04-03 下午7:44
*/
abstract class VoidReducer<T> extends Reducer<T, Void> {
abstract class VoidReducer<T> implements Reducer<T, Void> {

@Override
public Void reduce(Collection<T> results) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -31,21 +31,29 @@ public <Ext extends ExtPoint, T, R, P> R execute(Class<Ext> extClz, String metho

List<Ext> instanceList = SimpleKExtPointManger.getInstance(kbfRealizeItemList, extClz, methodName);

return execAndHandlerResults(extMethod, defaultMethod, reducer, instanceList);
}

private static <Ext extends ExtPoint, T, R> R execAndHandlerResults(ExtCallback<Ext, T> extMethod,
Supplier<T> defaultMethod, Reducer<T, R> reducer, List<Ext> 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<T> results = instanceList.stream().map(extMethod).collect(Collectors.toList());
T defaultMethodResult = defaultMethod.get();
results.add(defaultMethodResult);
return reducer.reduce(results);
default:
return null;
List<T> 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 <P> Set<NormalProductItem> recognize(P request);
Expand Down
6 changes: 6 additions & 0 deletions kbf-samples/kbf-common-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,11 @@
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.5.15</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
106 changes: 106 additions & 0 deletions kbf-samples/kbf-common-sample/src/test/java/ReduceSampleTest.java
Original file line number Diff line number Diff line change
@@ -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<Integer, List<Integer>> all = Reducers.all();
List<Integer> result = all.reduce(Lists.newArrayList(1, 2, 3));
Assert.assertEquals(result, Lists.newArrayList(1, 2, 3));
}

@Test
public void allMatch() {
Reducer<Boolean, Boolean> allMatch1 = Reducers.allMatch();
Boolean result1 = allMatch1.reduce(Lists.newArrayList(true, false, true));
Assert.assertEquals(result1, false);

Reducer<Integer, Boolean> 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<Boolean, Boolean> anyMatch1 = Reducers.anyMatch();
Boolean result1 = anyMatch1.reduce(Lists.newArrayList(true, false, false));
Assert.assertEquals(result1, true);

Reducer<Integer, Boolean> 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<Integer, List<Integer>> collect = Reducers.collect(i -> i % 2 == 0);
List<Integer> result = collect.reduce(Lists.newArrayList(1, 2, 3));

Assert.assertEquals(result, Lists.newArrayList(2));
}

@Test
public void firstOf() {
Reducer<Integer, Integer> 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<Integer>, List<Integer>> flatList = Reducers.flatList(Objects::nonNull);
List<Integer> array1 = Lists.newArrayList(1, 2, 3);
List<Integer> array2 = null;
List<Integer> array3 = Lists.newArrayList(3, 4, 5);
List<Integer> 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<String, String>, Map<String, String>> flatMap = Reducers.flatMap(Objects::nonNull);

Map<String, String> map1 = ImmutableMap.of("data1", "a");
Map<String, String> map2 = null;
Map<String, String> map3 = ImmutableMap.of("data2", "b");
Map<String, String> result = flatMap.reduce(Lists.newArrayList(map1, map2, map3));

Assert.assertEquals(result, ImmutableMap.of("data1", "a", "data2", "b"));
}

@Test
public void flatSet() {
Reducer<List<Integer>, Set<Integer>> flatSet = Reducers.flatSet(Objects::nonNull);
List<Integer> array1 = Lists.newArrayList(1, 2, 3);
List<Integer> array2 = null;
List<Integer> array3 = Lists.newArrayList(3, 4, 5);
Set<Integer> result = flatSet.reduce(Lists.newArrayList(array1, array2, array3));

Assert.assertEquals(result, Sets.newHashSet(1, 2, 3, 4, 5));
}
}

0 comments on commit c93eccd

Please sign in to comment.