|
6 | 6 | import java.util.Optional;
|
7 | 7 | import lombok.extern.slf4j.Slf4j;
|
8 | 8 |
|
| 9 | +/** |
| 10 | + * Helper class to run hooks. Initialize {@link HookSupportData} by calling setHooks, setHookContexts |
| 11 | + * & updateEvaluationContext in this exact order. |
| 12 | + */ |
9 | 13 | @Slf4j
|
10 | 14 | class HookSupport {
|
11 | 15 |
|
12 | 16 | /**
|
13 |
| - * Updates the evaluation context in the given data object's eval context and each hooks eval context. |
| 17 | + * Sets the {@link Hook}-{@link HookContext}-{@link Pair} list in the given data object with {@link HookContext} |
| 18 | + * set to null. Filters hooks by supported {@link FlagValueType}. |
14 | 19 | *
|
15 | 20 | * @param hookSupportData the data object to modify
|
16 |
| - * @param evaluationContext the new context to set |
| 21 | + * @param hooks the hooks to set |
| 22 | + * @param type the flag value type to filter unsupported hooks |
17 | 23 | */
|
18 |
| - public void updateEvaluationContext(HookSupportData hookSupportData, EvaluationContext evaluationContext) { |
19 |
| - hookSupportData.evaluationContext = evaluationContext; |
20 |
| - if (hookSupportData.hooks != null) { |
21 |
| - for (Pair<Hook, HookContext> hookContextPair : hookSupportData.hooks) { |
22 |
| - hookContextPair.getValue().setCtx(evaluationContext); |
| 24 | + public void setHooks(HookSupportData hookSupportData, List<Hook> hooks, FlagValueType type) { |
| 25 | + List<Pair<Hook, HookContext>> hookContextPairs = new ArrayList<>(); |
| 26 | + for (Hook hook : hooks) { |
| 27 | + if (hook.supportsFlagValueType(type)) { |
| 28 | + hookContextPairs.add(Pair.of(hook, null)); |
23 | 29 | }
|
24 | 30 | }
|
| 31 | + hookSupportData.hooks = hookContextPairs; |
25 | 32 | }
|
26 | 33 |
|
27 | 34 | /**
|
28 |
| - * Sets the {@link Hook}-{@link HookContext}-{@link Pair} list in the given data object. |
| 35 | + * Creates & sets a {@link HookContext} for every {@link Hook}-{@link HookContext}-{@link Pair} |
| 36 | + * in the given data object with a new {@link HookData} instance. |
29 | 37 | *
|
30 | 38 | * @param hookSupportData the data object to modify
|
31 |
| - * @param hooks the new hooks to set |
32 |
| - * @param sharedContext the shared context across all hooks from which each hook's {@link HookContext} is |
33 |
| - * created |
34 |
| - * @param evaluationContext the evaluation context which is |
| 39 | + * @param sharedContext the shared context from which the new {@link HookContext} is created |
35 | 40 | */
|
36 |
| - public void setHookSupportDataHooks( |
37 |
| - HookSupportData hookSupportData, |
38 |
| - List<Hook> hooks, |
39 |
| - SharedHookContext sharedContext, |
40 |
| - EvaluationContext evaluationContext) { |
41 |
| - List<Pair<Hook, HookContext>> hookContextPairs = new ArrayList<>(); |
42 |
| - for (Hook hook : hooks) { |
43 |
| - if (hook.supportsFlagValueType(sharedContext.getType())) { |
44 |
| - HookContext curContext = sharedContext.hookContextFor(evaluationContext, new DefaultHookData()); |
45 |
| - hookContextPairs.add(Pair.of(hook, curContext)); |
| 41 | + public void setHookContexts(HookSupportData hookSupportData, SharedHookContext sharedContext) { |
| 42 | + for (int i = 0; i < hookSupportData.hooks.size(); i++) { |
| 43 | + Pair<Hook, HookContext> hookContextPair = hookSupportData.hooks.get(i); |
| 44 | + HookContext curHookContext = sharedContext.hookContextFor(null, new DefaultHookData()); |
| 45 | + Pair<Hook, HookContext> updatedPair = Pair.of(hookContextPair.getKey(), curHookContext); |
| 46 | + hookSupportData.hooks.set(i, updatedPair); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Updates the evaluation context in the given data object's eval context and each hooks eval context. |
| 52 | + * |
| 53 | + * @param hookSupportData the data object to modify |
| 54 | + * @param evaluationContext the new context to set |
| 55 | + */ |
| 56 | + public void updateEvaluationContext(HookSupportData hookSupportData, EvaluationContext evaluationContext) { |
| 57 | + hookSupportData.evaluationContext = evaluationContext; |
| 58 | + if (hookSupportData.hooks != null) { |
| 59 | + for (Pair<Hook, HookContext> hookContextPair : hookSupportData.hooks) { |
| 60 | + var curHookContext = hookContextPair.getValue(); |
| 61 | + if (curHookContext != null) { |
| 62 | + curHookContext.setCtx(evaluationContext); |
| 63 | + } |
46 | 64 | }
|
47 | 65 | }
|
48 |
| - hookSupportData.hooks = hookContextPairs; |
49 | 66 | }
|
50 | 67 |
|
51 | 68 | public void executeBeforeHooks(HookSupportData data) {
|
|
0 commit comments