66import java .util .Optional ;
77import lombok .extern .slf4j .Slf4j ;
88
9+ /**
10+ * Helper class to run hooks. Initialize {@link HookSupportData} by calling setHooks, setHookContexts
11+ * & updateEvaluationContext in this exact order.
12+ */
913@ Slf4j
1014class HookSupport {
1115
1216 /**
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}.
1419 *
1520 * @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
1723 */
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 ) );
2329 }
2430 }
31+ hookSupportData .hooks = hookContextPairs ;
2532 }
2633
2734 /**
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.
2937 *
3038 * @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
3540 */
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+ }
4664 }
4765 }
48- hookSupportData .hooks = hookContextPairs ;
4966 }
5067
5168 public void executeBeforeHooks (HookSupportData data ) {
@@ -57,9 +74,10 @@ public void executeBeforeHooks(HookSupportData data) {
5774 var hook = hookContextPair .getKey ();
5875 var hookContext = hookContextPair .getValue ();
5976
60- Optional <EvaluationContext > returnedEvalContext = Optional .ofNullable (
61- hook .before (hookContext , data .getHints ()))
62- .orElse (Optional .empty ());
77+ Optional <EvaluationContext > returnedEvalContext = hook .before (hookContext , data .getHints ());
78+ if (returnedEvalContext == null ) {
79+ returnedEvalContext = Optional .empty ();
80+ }
6381 if (returnedEvalContext .isPresent ()) {
6482 // update shared evaluation context for all hooks
6583 updateEvaluationContext (data , data .getEvaluationContext ().merge (returnedEvalContext .get ()));
0 commit comments