Skip to content

Commit b324631

Browse files
author
pellse
committed
New static methods for removeDuplicates, removeAllDuplicates and replace
1 parent 67608df commit b324631

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

assembler/src/main/java/io/github/pellse/assembler/caching/factory/CacheContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.github.pellse.assembler.RuleMapperContext.OneToManyContext;
2020
import io.github.pellse.assembler.RuleMapperContext.OneToOneContext;
2121
import io.github.pellse.assembler.caching.merge.MergeFunction;
22+
import io.github.pellse.assembler.caching.merge.MergeFunctions;
2223

2324
import java.util.*;
2425
import java.util.function.BiFunction;
@@ -27,7 +28,6 @@
2728
import java.util.stream.Collector;
2829

2930
import static io.github.pellse.assembler.caching.factory.ConcurrentCacheFactory.concurrent;
30-
import static io.github.pellse.assembler.caching.merge.MergeFunctions.replace;
3131
import static io.github.pellse.util.collection.CollectionUtils.*;
3232
import static java.util.Objects.requireNonNullElse;
3333

@@ -49,7 +49,7 @@ record OneToOneCacheContext<ID, R>(
4949
CacheTransformer<ID, R, R, OneToOneCacheContext<ID, R>> cacheTransformer) implements CacheContext<ID, R, R, OneToOneCacheContext<ID, R>> {
5050

5151
public OneToOneCacheContext {
52-
mergeFunction = requireNonNullElse(mergeFunction, replace());
52+
mergeFunction = requireNonNullElse(mergeFunction, MergeFunctions::replace);
5353
}
5454

5555
static <ID, R> OneToOneCacheContext<ID, R> oneToOneCacheContext(OneToOneContext<?, ?, ID, R> ctx) {

assembler/src/main/java/io/github/pellse/assembler/caching/merge/MergeFunctions.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,23 @@
1414
public interface MergeFunctions {
1515

1616
static <ID, EID, R> MergeFunctionFactory<ID, EID, R> removeDuplicates() {
17-
return ctx -> removeDuplicates(ctx.idResolver());
17+
return MergeFunctions::removeDuplicates;
18+
}
19+
20+
static <ID, EID, R> MergeFunction<ID, List<R>> removeDuplicates(MergeFunctionContext<EID, R> ctx) {
21+
return removeDuplicates(ctx.idResolver());
1822
}
1923

2024
static <ID, EID, R> MergeFunction<ID, List<R>> removeDuplicates(Function<R, EID> idResolver) {
2125
return (id, existingList, newList) -> CollectionUtils.removeDuplicates(concat(existingList, newList), idResolver);
2226
}
2327

2428
static <EID, R> Function<MergeFunctionContext<EID, R>, Function<List<R>, List<R>>> removeAllDuplicates() {
25-
return ctx -> list -> CollectionUtils.removeDuplicates(list, ctx.idResolver());
29+
return MergeFunctions::removeAllDuplicates;
30+
}
31+
32+
static <EID, R> Function<List<R>, List<R>> removeAllDuplicates(MergeFunctionContext<EID, R> ctx) {
33+
return list -> CollectionUtils.removeDuplicates(list, ctx.idResolver());
2634
}
2735

2836
static <ID, R> MergeFunction<ID, List<R>> keepFirst(int nbElements) {
@@ -50,7 +58,11 @@ static <R> Function<List<R>, List<R>> keepLastN(int nbElements) {
5058
}
5159

5260
static <ID, R> MergeFunction<ID, R> replace() {
53-
return (k, r1, r2) -> r2 != null ? r2 : r1;
61+
return MergeFunctions::replace;
62+
}
63+
64+
static <ID, R> R replace(ID id, R r1, R r2) {
65+
return r2 != null ? r2 : r1;
5466
}
5567

5668
private static <R> Function<List<R>, List<R>> keep(int nbElements, Function<List<R>, List<R>> subListFunction) {

assembler/src/test/java/io/github/pellse/assembler/test/CacheTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import io.github.pellse.assembler.caching.factory.CacheContext.OneToOneCacheContext;
2323
import io.github.pellse.assembler.caching.factory.CacheFactory;
2424
import io.github.pellse.assembler.caching.factory.CacheTransformer;
25+
import io.github.pellse.assembler.caching.merge.MergeFunctionFactory;
26+
import io.github.pellse.assembler.caching.merge.MergeFunctions;
2527
import io.github.pellse.assembler.util.*;
2628
import org.junit.jupiter.api.BeforeEach;
2729
import org.junit.jupiter.api.Test;
@@ -269,11 +271,16 @@ public void testReusableAssemblerBuilderWithMergeFunctions() {
269271
.filter(billingInfo -> customerIds.contains(billingInfo.customerId()))
270272
.doOnComplete(billingInvocationCount::incrementAndGet);
271273

274+
MergeFunctionFactory<Long, String, OrderItem> mff = pipe(MergeFunctions::removeDuplicates, keepLastN(20));
275+
var mff2 = MergeFunctions.<Long, String, OrderItem>removeDuplicates().pipe(keepLastN(20));
276+
var mff3 = MergeFunctionFactory.<Long, String, OrderItem>from(MergeFunctions::removeDuplicates).pipe(keepLastN(20));
277+
272278
var assembler = assemblerOf(Transaction.class)
273279
.withCorrelationIdResolver(Customer::customerId)
274280
.withRules(
275-
rule(BillingInfo::customerId, oneToOne(cached(call(getBillingInfo), replace()), BillingInfo::new)),
276-
rule(OrderItem::customerId, oneToMany(OrderItem::id, cachedMany(this::getAllOrders, pipe(removeDuplicates(), keepLastN(20))))),
281+
rule(BillingInfo::customerId, oneToOne(cached(call(getBillingInfo), MergeFunctions::replace), BillingInfo::new)),
282+
rule(OrderItem::customerId, oneToMany(OrderItem::id, cachedMany(this::getAllOrders, mff))),
283+
// rule(OrderItem::customerId, oneToMany(OrderItem::id, cachedMany(this::getAllOrders, pipe(removeDuplicates(), keepLastN(20))))),
277284
// rule(OrderItem::customerId, oneToMany(OrderItem::id, cachedMany(this::getAllOrders, pipeWith(keepLast(20), removeAllDuplicates())))),
278285
Transaction::new)
279286
.build();

0 commit comments

Comments
 (0)