2
2
3
3
import java .util .*;
4
4
import java .util .function .Consumer ;
5
+ import java .util .stream .Stream ;
5
6
6
7
import com .google .common .collect .Lists ;
7
- import lombok .* ;
8
+ import lombok .RequiredArgsConstructor ;
8
9
import org .slf4j .Logger ;
9
10
10
11
@ RequiredArgsConstructor
11
12
class HookSupport {
12
13
13
14
private final Logger log ;
14
15
16
+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
15
17
public void errorHooks (FlagValueType flagValueType , HookContext hookCtx , Exception e , List <Hook > hooks , Map <String , Object > hints ) {
16
18
executeHooks (flagValueType , hooks , "error" , hook -> hook .error (hookCtx , e , hints ));
17
19
}
18
20
21
+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
19
22
public void afterAllHooks (FlagValueType flagValueType , HookContext hookCtx , List <Hook > hooks , Map <String , Object > hints ) {
20
23
executeHooks (flagValueType , hooks , "finally" , hook -> hook .finallyAfter (hookCtx , hints ));
21
24
}
22
25
26
+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
23
27
public void afterHooks (FlagValueType flagValueType , HookContext hookContext , FlagEvaluationDetails details , List <Hook > hooks , Map <String , Object > hints ) {
24
- executeHooksUnsafe (flagValueType , hooks , hook -> hook .after (hookContext , details , hints ));
28
+ executeHooksUnchecked (flagValueType , hooks , hook -> hook .after (hookContext , details , hints ));
25
29
}
26
30
31
+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
27
32
private <T > void executeHooks (
28
33
FlagValueType flagValueType , List <Hook > hooks ,
29
34
String hookMethod ,
@@ -35,7 +40,8 @@ private <T> void executeHooks(
35
40
.forEach (hook -> executeChecked (hook , hookCode , hookMethod ));
36
41
}
37
42
38
- private <T > void executeHooksUnsafe (
43
+ @ SuppressWarnings ("rawtypes" )
44
+ private <T > void executeHooksUnchecked (
39
45
FlagValueType flagValueType , List <Hook > hooks ,
40
46
Consumer <Hook <T >> hookCode
41
47
) {
@@ -53,17 +59,28 @@ private <T> void executeChecked(Hook<T> hook, Consumer<Hook<T>> hookCode, String
53
59
}
54
60
}
55
61
56
- public EvaluationContext beforeHooks (HookContext hookCtx , List <Hook > hooks , Map <String , Object > hints ) {
62
+ @ SuppressWarnings ("rawtypes" )
63
+ public EvaluationContext beforeHooks (FlagValueType flagValueType , HookContext hookCtx , List <Hook > hooks , Map <String , Object > hints ) {
64
+ var result = callBeforeHooks (flagValueType , hookCtx , hooks , hints );
65
+ return EvaluationContext .merge (hookCtx .getCtx (), collectContexts (result ));
66
+ }
67
+
68
+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
69
+ private Stream <EvaluationContext > callBeforeHooks (FlagValueType flagValueType , HookContext hookCtx , List <Hook > hooks , Map <String , Object > hints ) {
57
70
// These traverse backwards from normal.
58
- EvaluationContext ctx = hookCtx .getCtx ();
59
- for (Hook hook : Lists .reverse (hooks )) {
60
- Optional <EvaluationContext > newCtx = hook .before (hookCtx , hints );
61
- if (newCtx != null && newCtx .isPresent ()) {
62
- ctx = EvaluationContext .merge (ctx , newCtx .get ());
63
- hookCtx = hookCtx .withCtx (ctx );
64
- }
65
- }
66
- return ctx ;
71
+ return Lists
72
+ .reverse (hooks )
73
+ .stream ()
74
+ .filter (hook -> hook .supportsFlagValueType () == flagValueType )
75
+ .map (hook -> hook .before (hookCtx , hints ))
76
+ .filter (Objects ::nonNull )
77
+ .flatMap (Optional ::stream );
67
78
}
68
79
80
+ //for whatever reason, an error `error: incompatible types: invalid method reference` is thrown on compilation with javac
81
+ //when the reduce call is appended directly as stream call chain above. moving it to its own method works however...
82
+ private EvaluationContext collectContexts (Stream <EvaluationContext > result ) {
83
+ return result
84
+ .reduce (new EvaluationContext (), EvaluationContext ::merge , EvaluationContext ::merge );
85
+ }
69
86
}
0 commit comments