36
36
import static io .github .pellse .assembler .QueryUtils .buildQueryFunction ;
37
37
import static io .github .pellse .assembler .RuleMapperSource .*;
38
38
import static io .github .pellse .assembler .caching .DefaultCache .cache ;
39
- import static io .github .pellse .assembler .caching .factory .CacheContext .OneToManyCacheContext .oneToManyCacheContext ;
40
- import static io .github .pellse .assembler .caching .factory .CacheContext .OneToOneCacheContext .oneToOneCacheContext ;
41
39
import static io .github .pellse .assembler .caching .factory .DeferCacheFactory .defer ;
42
40
import static io .github .pellse .assembler .caching .OneToManyCache .oneToManyCache ;
43
41
import static io .github .pellse .assembler .caching .OneToOneCache .oneToOneCache ;
@@ -167,7 +165,7 @@ static <T, TC extends Collection<T>, K, ID, R> RuleMapperSource<T, TC, K, ID, ID
167
165
CacheFactory <ID , R , R , OneToOneCacheContext <ID , R >> cacheFactory ,
168
166
Function <CacheFactory <ID , R , R , OneToOneCacheContext <ID , R >>, CacheFactory <ID , R , R , OneToOneCacheContext <ID , R >>>... delegateCacheFactories ) {
169
167
170
- return cached (OneToOneCacheContext :: oneToOneCacheContext , ruleMapperSource , oneToOneCacheFactory ( wrap ( cacheFactory )) , delegateCacheFactories );
168
+ return cached (( k , r1 , r2 ) -> r2 != null ? r2 : r1 , ruleMapperSource , cacheFactory , delegateCacheFactories );
171
169
}
172
170
173
171
@ SafeVarargs
@@ -177,7 +175,13 @@ static <T, TC extends Collection<T>, K, ID, R> RuleMapperSource<T, TC, K, ID, ID
177
175
CacheFactory <ID , R , R , OneToOneCacheContext <ID , R >> cacheFactory ,
178
176
Function <CacheFactory <ID , R , R , OneToOneCacheContext <ID , R >>, CacheFactory <ID , R , R , OneToOneCacheContext <ID , R >>>... delegateCacheFactories ) {
179
177
180
- return cached (ctx -> oneToOneCacheContext (ctx , mergeFunction ), ruleMapperSource , oneToOneCacheFactory (wrap (cacheFactory )), delegateCacheFactories );
178
+ final var wrappedCacheFactory = wrap (cacheFactory );
179
+
180
+ return cached (
181
+ OneToOneCacheContext ::oneToOneCacheContext ,
182
+ ruleMapperSource ,
183
+ ctx -> oneToOneCache (mergeFunction , wrappedCacheFactory .create (ctx )),
184
+ delegateCacheFactories );
181
185
}
182
186
183
187
@ SafeVarargs
@@ -209,7 +213,7 @@ static <T, TC extends Collection<T>, K, ID, EID, R, RC extends Collection<R>> Ru
209
213
CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >> cacheFactory ,
210
214
Function <CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >>, CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >>>... delegateCacheFactories ) {
211
215
212
- return cachedMany (mergeFunction , emptySource (), cacheFactory , delegateCacheFactories );
216
+ return cachedMany (ctx -> mergeFunction , emptySource (), cacheFactory , delegateCacheFactories );
213
217
}
214
218
215
219
@ SafeVarargs
@@ -243,7 +247,7 @@ static <T, TC extends Collection<T>, K, ID, EID, R, RC extends Collection<R>> Ru
243
247
RuleMapperSource <T , TC , K , ID , EID , R , RC , OneToManyContext <T , TC , K , ID , EID , R , RC >> ruleMapperSource ,
244
248
Function <CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >>, CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >>>... delegateCacheFactories ) {
245
249
246
- return cachedMany (mergeFunction , ruleMapperSource , cache (), delegateCacheFactories );
250
+ return cachedMany (ctx -> mergeFunction , ruleMapperSource , cache (), delegateCacheFactories );
247
251
}
248
252
249
253
@ SafeVarargs
@@ -262,7 +266,7 @@ static <T, TC extends Collection<T>, K, ID, EID, R, RC extends Collection<R>> Ru
262
266
CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >> cacheFactory ,
263
267
Function <CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >>, CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >>>... delegateCacheFactories ) {
264
268
265
- return cachedMany (mergeFunction , from (queryFunction ), cacheFactory , delegateCacheFactories );
269
+ return cachedMany (ctx -> mergeFunction , from (queryFunction ), cacheFactory , delegateCacheFactories );
266
270
}
267
271
268
272
@ SafeVarargs
@@ -271,29 +275,35 @@ static <T, TC extends Collection<T>, K, ID, EID, R, RC extends Collection<R>> Ru
271
275
CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >> cacheFactory ,
272
276
Function <CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >>, CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >>>... delegateCacheFactories ) {
273
277
274
- return cached ( OneToManyCacheContext :: oneToManyCacheContext , ruleMapperSource , oneToManyCacheFactory ( wrap ( cacheFactory )) , delegateCacheFactories );
278
+ return cachedMany ( CacheFactory :: removeDuplicate , ruleMapperSource , cacheFactory , delegateCacheFactories );
275
279
}
276
280
277
281
@ SafeVarargs
278
282
static <T , TC extends Collection <T >, K , ID , EID , R , RC extends Collection <R >> RuleMapperSource <T , TC , K , ID , EID , R , RC , OneToManyContext <T , TC , K , ID , EID , R , RC >> cachedMany (
279
- Function3 <ID , RC , RC , RC > mergeFunction ,
283
+ Function < OneToManyCacheContext < ID , EID , R , RC >, Function3 <ID , RC , RC , RC >> mergeFunctionProvider ,
280
284
RuleMapperSource <T , TC , K , ID , EID , R , RC , OneToManyContext <T , TC , K , ID , EID , R , RC >> ruleMapperSource ,
281
285
CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >> cacheFactory ,
282
286
Function <CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >>, CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >>>... delegateCacheFactories ) {
283
287
284
- return cached (ctx -> oneToManyCacheContext (ctx , mergeFunction ), ruleMapperSource , oneToManyCacheFactory (wrap (cacheFactory )), delegateCacheFactories );
288
+ final var wrappedCacheFactory = wrap (cacheFactory );
289
+
290
+ return cached (
291
+ OneToManyCacheContext ::oneToManyCacheContext ,
292
+ ruleMapperSource ,
293
+ ctx -> oneToManyCache (mergeFunctionProvider .apply (ctx ), ctx , wrappedCacheFactory .create (ctx )),
294
+ delegateCacheFactories );
285
295
}
286
296
287
297
static <ID , RRC > Function <Map <ID , RRC >, Mono <?>> toMono (Consumer <Map <ID , RRC >> consumer ) {
288
298
return map -> just (also (map , consumer ));
289
299
}
290
300
291
- private static <ID , R > CacheFactory <ID , R , R , OneToOneCacheContext <ID , R >> oneToOneCacheFactory (CacheFactory <ID , R , R , OneToOneCacheContext <ID , R >> cacheFactory ) {
292
- return cacheContext -> oneToOneCache (cacheContext , cacheFactory .create (cacheContext ));
301
+ private static <ID , R > CacheFactory <ID , R , R , OneToOneCacheContext <ID , R >> oneToOneCacheFactory (Function3 < ID , R , R , R > mergeFunction , CacheFactory <ID , R , R , OneToOneCacheContext <ID , R >> cacheFactory ) {
302
+ return ctx -> oneToOneCache (mergeFunction , cacheFactory .create (ctx ));
293
303
}
294
304
295
- private static <ID , EID , R , RC extends Collection <R >> CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >> oneToManyCacheFactory (CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >> cacheFactory ) {
296
- return cacheContext -> oneToManyCache (cacheContext , cacheFactory .create (cacheContext ));
305
+ private static <ID , EID , R , RC extends Collection <R >> CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >> oneToManyCacheFactory (Function3 < ID , RC , RC , RC > mergeFunction , CacheFactory <ID , R , RC , OneToManyCacheContext <ID , EID , R , RC >> cacheFactory ) {
306
+ return ctx -> oneToManyCache (mergeFunction , ctx , cacheFactory .create (ctx ));
297
307
}
298
308
299
309
@ SafeVarargs
@@ -375,4 +385,12 @@ private static <T, TC extends Collection<T>, K, ID, EID, R, RRC> Map<ID, RRC> bu
375
385
diff (ids , map .keySet ())
376
386
.forEach (id -> ifNotNull (ctx .defaultResultProvider ().apply (id ), value -> map .put (id , value ))));
377
387
}
388
+
389
+ private static <ID , EID , R , RC extends Collection <R >> Function3 <ID , RC , RC , RC > removeDuplicate (OneToManyCacheContext <ID , EID , R , RC > ctx ) {
390
+ return (id , coll1 , coll2 ) -> removeDuplicates (concat (coll1 , coll2 ), ctx .idResolver (), rc -> convert (rc , ctx ));
391
+ }
392
+
393
+ private static <ID , EID , R , RC extends Collection <R >> RC convert (Collection <R > collection , OneToManyCacheContext <ID , EID , R , RC > ctx ) {
394
+ return CollectionUtils .convert (collection , ctx .collectionType (), ctx .collectionFactory ());
395
+ }
378
396
}
0 commit comments