Skip to content

Commit 70193af

Browse files
author
pellse
committed
Moved back to using same generic types for mergeFunction
1 parent 2269d19 commit 70193af

File tree

8 files changed

+189
-87
lines changed

8 files changed

+189
-87
lines changed

assembler/src/main/java/io/github/pellse/assembler/caching/OneToManyCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface CacheUpdater<ID, RRC> {
2828
}
2929

3030
static <ID, EID, R, RC extends Collection<R>> Cache<ID, RC> oneToManyCache(
31-
OneToManyCacheContext<ID, EID, R, RC, R, RC> ctx,
31+
OneToManyCacheContext<ID, EID, R, RC> ctx,
3232
Cache<ID, RC> delegateCache) {
3333

3434
final var optimizedCache = optimizedCache(delegateCache);

assembler/src/main/java/io/github/pellse/assembler/caching/OneToOneCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public interface OneToOneCache {
99

1010
static <ID, R> Cache<ID, R> oneToOneCache(
11-
CacheContext.OneToOneCacheContext<ID, R, R> ctx,
11+
CacheContext.OneToOneCacheContext<ID, R> ctx,
1212
Cache<ID, R> delegateCache) {
1313

1414
final var optimizedCache = optimizedCache(delegateCache);

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

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@
1919
import io.github.pellse.assembler.RuleMapperContext.OneToManyContext;
2020
import io.github.pellse.assembler.RuleMapperContext.OneToOneContext;
2121
import io.github.pellse.assembler.caching.factory.CacheFactory.CacheTransformer;
22-
import io.github.pellse.util.collection.CollectionUtils;
2322
import io.github.pellse.util.function.Function3;
2423

25-
import java.util.Collection;
26-
import java.util.Comparator;
27-
import java.util.List;
28-
import java.util.Map;
24+
import java.util.*;
2925
import java.util.function.BiFunction;
3026
import java.util.function.Function;
3127
import java.util.function.IntFunction;
@@ -40,82 +36,71 @@ public sealed interface CacheContext<ID, R, RRC, CTX extends CacheContext<ID, R,
4036

4137
IntFunction<Collector<R, ?, Map<ID, RRC>>> mapCollector();
4238

43-
BiFunction<Map<ID, RRC>, Map<ID, RRC>, Map<ID, RRC>> mapMerger();
39+
Function3<ID, RRC, RRC, RRC> mergeFunction();
4440

4541
CacheTransformer<ID, R, RRC, CTX> cacheTransformer();
4642

47-
record OneToOneCacheContext<ID, R, U>(
43+
default BiFunction<Map<ID, RRC>, Map<ID, RRC>, Map<ID, RRC>> mapMerger() {
44+
return (existingMap, newMap) -> mergeMaps(existingMap, newMap, mergeFunction());
45+
}
46+
47+
record OneToOneCacheContext<ID, R>(
4848
IntFunction<Collector<R, ?, Map<ID, R>>> mapCollector,
49-
Function3<ID, R, U, R> mergeFunction,
50-
CacheTransformer<ID, R, R, OneToOneCacheContext<ID, R, U>> cacheTransformer) implements CacheContext<ID, R, R, OneToOneCacheContext<ID, R, U>> {
49+
Function3<ID, R, R, R> mergeFunction,
50+
CacheTransformer<ID, R, R, OneToOneCacheContext<ID, R>> cacheTransformer) implements CacheContext<ID, R, R, OneToOneCacheContext<ID, R>> {
5151

52-
static<ID, R> OneToOneCacheContext<ID, R, R> oneToOneCacheContext(OneToOneContext<?, ?, ?, ID, R> ctx) {
52+
static<ID, R> OneToOneCacheContext<ID, R> oneToOneCacheContext(OneToOneContext<?, ?, ?, ID, R> ctx) {
5353
return oneToOneCacheContext(ctx, (k, r1, r2) -> r2 != null ? r2 : r1);
5454
}
5555

56-
static<ID, R> OneToOneCacheContext<ID, R, R> oneToOneCacheContext(OneToOneContext<?, ?, ?, ID, R> ctx, Function3<ID, R, R, R> mergeFunction) {
56+
static<ID, R> OneToOneCacheContext<ID, R> oneToOneCacheContext(OneToOneContext<?, ?, ?, ID, R> ctx, Function3<ID, R, R, R> mergeFunction) {
5757
return new OneToOneCacheContext<>(ctx.mapCollector(),
5858
mergeFunction,
59-
concurrent());
60-
}
61-
62-
public BiFunction<Map<ID, R>, Map<ID, U>, Map<ID, R>> mapMerger(Function3<ID, R, U, R> mergeFunction) {
63-
return (existingMap, newMap) -> mergeMaps(existingMap, newMap, mergeFunction);
64-
}
65-
66-
public BiFunction<Map<ID, R>, Map<ID, R>, Map<ID, R>> mapMerger() {
67-
return CollectionUtils::mergeMaps;
59+
concurrent());
6860
}
6961
}
7062

71-
record OneToManyCacheContext<ID, EID, R, RC extends Collection<R>, U, UC extends Collection<U>> (
63+
record OneToManyCacheContext<ID, EID, R, RC extends Collection<R>> (
7264
Function<R, EID> idResolver,
7365
IntFunction<Collector<R, ?, Map<ID, RC>>> mapCollector,
7466
Comparator<R> idComparator,
7567
Supplier<RC> collectionFactory,
7668
Class<RC> collectionType,
77-
Function3<ID, RC, UC, RC> mergeFunction,
78-
CacheTransformer<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC, U, UC>> cacheTransformer) implements CacheContext<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC, U, UC>> {
69+
Function3<ID, RC, RC, RC> mergeFunction,
70+
CacheTransformer<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>> cacheTransformer) implements CacheContext<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>> {
7971

80-
static <ID, EID, R, RC extends Collection<R>> OneToManyCacheContext<ID, EID, R, RC, R, RC> oneToManyCacheContext(OneToManyContext<?, ?, ?, ID, EID, R, RC> ctx) {
81-
82-
return oneToManyCacheContext(
83-
ctx,
84-
(k, coll1, coll2) -> removeDuplicates(concat(coll1, coll2), ctx.idResolver(), rc -> convert(rc, ctx.collectionType(), ctx.collectionFactory())));
72+
static <ID, EID, R, RC extends Collection<R>> OneToManyCacheContext<ID, EID, R, RC> oneToManyCacheContext(OneToManyContext<?, ?, ?, ID, EID, R, RC> ctx) {
73+
return oneToManyCacheContext(ctx, removeDuplicate(ctx));
8574
}
8675

87-
static <ID, EID, R, RC extends Collection<R>, U, UC extends Collection<U>> OneToManyCacheContext<ID, EID, R, RC, U, UC> oneToManyCacheContext(
76+
static <ID, EID, R, RC extends Collection<R>> OneToManyCacheContext<ID, EID, R, RC> oneToManyCacheContext(
8877
OneToManyContext<?, ?, ?, ID, EID, R, RC> ctx,
89-
Function3<ID, RC, UC, RC> mergeFunction) {
78+
Function3<ID, RC, RC, RC> mergeFunction) {
9079

9180
return new OneToManyCacheContext<>(ctx.idResolver(),
9281
ctx.mapCollector(),
9382
ctx.idComparator(),
9483
ctx.collectionFactory(),
9584
ctx.collectionType(),
96-
mergeFunction,
85+
(id, coll1, coll2) ->
86+
isNotEmpty(coll1) || isNotEmpty(coll2) ? mergeFunction.apply(id, convert(coll1, ctx), convert(coll2, ctx)) : convert(List.of(), ctx),
9787
concurrent());
9888
}
9989

100-
BiFunction<Map<ID, RC>, Map<ID, Collection<U>>, Map<ID, RC>> mapMerger(Function3<ID, RC, Collection<U>, RC> mergeFunction) {
101-
102-
Function3<ID, RC, Collection<U>, RC> mappingFunction = (id, coll1, coll2) ->
103-
isNotEmpty(coll1) || isNotEmpty(coll2) ? mergeFunction.apply(id, convert(coll1), asCollection(coll2)) : convert(List.of());
104-
105-
return (existingMap, newMap) -> mergeMaps(existingMap, newMap, mappingFunction);
90+
private RC convert(Collection<R> collection) {
91+
return convert(collection, collectionType, collectionFactory);
10692
}
10793

108-
public BiFunction<Map<ID, RC>, Map<ID, RC>, Map<ID, RC>> mapMerger() {
109-
return (existingMap, newMap) -> mergeMaps(existingMap, newMap, idResolver(), this::convert);
94+
private static <ID, EID, R, RC extends Collection<R>> Function3<ID, RC, RC, RC> removeDuplicate(OneToManyContext<?, ?, ?, ID, EID, R, RC> ctx) {
95+
return (id, coll1, coll2) -> removeDuplicates(concat(coll1, coll2), ctx.idResolver(), rc -> convert(rc, ctx));
11096
}
11197

112-
@SuppressWarnings("unchecked")
113-
private RC convert(Collection<R> collection) {
114-
return collectionType().isInstance(collection) ? (RC) collection : translate(collection, collectionFactory());
98+
private static <ID, EID, R, RC extends Collection<R>> RC convert(Collection<R> collection, OneToManyContext<?, ?, ?, ID, EID, R, RC> ctx) {
99+
return convert(collection, ctx.collectionType(), ctx.collectionFactory());
115100
}
116101

117102
@SuppressWarnings("unchecked")
118-
public static <R, RC extends Collection<R>> RC convert(Collection<R> collection, Class<RC> collectionType, Supplier<RC> collectionFactory) {
103+
private static <R, RC extends Collection<R>> RC convert(Collection<R> collection, Class<RC> collectionType, Supplier<RC> collectionFactory) {
119104
return collectionType.isInstance(collection) ? (RC) collection : translate(collection, collectionFactory);
120105
}
121106
}

0 commit comments

Comments
 (0)