-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvalHooksRunner.java
More file actions
129 lines (117 loc) · 4.09 KB
/
EvalHooksRunner.java
File metadata and controls
129 lines (117 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.devcycle.sdk.server.common.model;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import com.devcycle.sdk.server.common.exception.AfterHookError;
import com.devcycle.sdk.server.common.exception.BeforeHookError;
import com.devcycle.sdk.server.common.logging.DevCycleLogger;
import com.devcycle.sdk.server.local.model.VariableMetadata;
/**
* A class that manages evaluation hooks for the DevCycle SDK.
* Provides functionality to add and clear hooks, storing them in an array.
*/
public class EvalHooksRunner<T> {
private List<EvalHook<T>> hooks;
/**
* Default constructor initializes an empty list of hooks.
*/
public EvalHooksRunner(List<EvalHook<T>> hooks) {
if (hooks == null) {
this.hooks = new ArrayList<>();
} else {
this.hooks = hooks;
}
}
public EvalHooksRunner() {
this.hooks = new ArrayList<>();
}
/**
* Adds a single hook to the collection.
*
* @param hook The hook to add
*/
public void addHook(EvalHook<T> hook) {
if (hook != null) {
hooks.add(hook);
}
}
/**
* Clears all hooks from the collection.
*/
public void clearHooks() {
hooks.clear();
}
public List<EvalHook<T>> getHooks() {
return this.hooks;
}
/**
* Runs all before hooks in order.
*
* @param context The context to pass to the hooks
* @param <T> The type of the variable value
* @return The potentially modified context
*/
public <T> HookContext<T> executeBefore(ArrayList<EvalHook<T>> hooks, HookContext<T> context) {
HookContext<T> beforeContext = context;
for (EvalHook<T> hook : hooks) {
try {
Optional<HookContext<T>> newContext = hook.before(beforeContext);
if (newContext.isPresent()) {
beforeContext = beforeContext.merge(newContext.get());
}
} catch (Exception e) {
throw new BeforeHookError("Before hook failed", e);
}
}
return beforeContext;
}
/**
* Runs all after hooks in reverse order.
*
* @param context The context to pass to the hooks
* @param variable The variable result to pass to the hooks
* @param <T> The type of the variable value
*/
public void executeAfter(ArrayList<EvalHook<T>> hooks, HookContext<T> context, Variable<T> variable, VariableMetadata variableMetadata) {
for (EvalHook<T> hook : hooks) {
try {
hook.after(context, variable, variableMetadata);
} catch (Exception e) {
throw new AfterHookError("After hook failed", e);
}
}
}
/**
* Runs all error hooks in reverse order.
*
* @param context The context to pass to the hooks
* @param error The error that occurred
* @param <T> The type of the variable value
*/
public void executeError(ArrayList<EvalHook<T>> hooks, HookContext<T> context, Throwable error) {
for (EvalHook<T> hook : hooks) {
try {
hook.error(context, error);
} catch (Exception hookError) {
// Log hook error but don't throw to avoid masking the original error
DevCycleLogger.error("Error hook failed: " + hookError.getMessage(), hookError);
}
}
}
/**
* Runs all finally hooks in reverse order.
*
* @param context The context to pass to the hooks
* @param variable The variable result to pass to the hooks (may be null)
*/
public void executeFinally(ArrayList<EvalHook<T>> hooks, HookContext<T> context, Optional<Variable<T>> variable, VariableMetadata variableMetadata) {
for (EvalHook<T> hook : hooks) {
try {
hook.onFinally(context, variable, variableMetadata);
} catch (Exception e) {
// Log finally hook error but don't throw
DevCycleLogger.error("Finally hook failed: " + e.getMessage(), e);
}
}
}
}