Skip to content

Commit 2843298

Browse files
committed
Split HookData interface and implementation
Signed-off-by: Guido Breitenhuber <[email protected]>
1 parent ceedffd commit 2843298

File tree

3 files changed

+55
-63
lines changed

3 files changed

+55
-63
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package dev.openfeature.sdk;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Objects;
6+
7+
/**
8+
* Default implementation of HookData.
9+
*/
10+
public class DefaultHookData implements HookData {
11+
private Map<String, Object> data;
12+
13+
@Override
14+
public void set(String key, Object value) {
15+
if (data == null) {
16+
data = new HashMap<>();
17+
}
18+
data.put(key, value);
19+
}
20+
21+
@Override
22+
public Object get(String key) {
23+
if (data == null) {
24+
return null;
25+
}
26+
return data.get(key);
27+
}
28+
29+
@Override
30+
public <T> T get(String key, Class<T> type) {
31+
Object value = get(key);
32+
if (value == null) {
33+
return null;
34+
}
35+
if (!type.isInstance(value)) {
36+
throw new ClassCastException("Value for key '" + key + "' is not of type " + type.getName());
37+
}
38+
return type.cast(value);
39+
}
40+
41+
@Override
42+
public boolean equals(Object o) {
43+
if (o == null || getClass() != o.getClass()) {
44+
return false;
45+
}
46+
dev.openfeature.sdk.DefaultHookData that = (dev.openfeature.sdk.DefaultHookData) o;
47+
return Objects.equals(data, that.data);
48+
}
49+
50+
@Override
51+
public int hashCode() {
52+
return Objects.hashCode(data);
53+
}
54+
}
Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
package dev.openfeature.sdk;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
5-
import java.util.Objects;
6-
73
/**
84
* Hook data provides a way for hooks to maintain state across their execution stages.
95
* Each hook instance gets its own isolated data store that persists only for the duration
106
* of a single flag evaluation.
117
*/
128
public interface HookData {
13-
149
/**
1510
* Sets a value for the given key.
1611
*
@@ -37,60 +32,4 @@ public interface HookData {
3732
* @throws ClassCastException if the value cannot be cast to the specified type
3833
*/
3934
<T> T get(String key, Class<T> type);
40-
41-
/**
42-
* Default implementation uses a HashMap.
43-
*/
44-
static HookData create() {
45-
return new DefaultHookData();
46-
}
47-
48-
/**
49-
* Default implementation of HookData.
50-
*/
51-
class DefaultHookData implements HookData {
52-
private Map<String, Object> data;
53-
54-
@Override
55-
public void set(String key, Object value) {
56-
if (data == null) {
57-
data = new HashMap<>();
58-
}
59-
data.put(key, value);
60-
}
61-
62-
@Override
63-
public Object get(String key) {
64-
if (data == null) {
65-
return null;
66-
}
67-
return data.get(key);
68-
}
69-
70-
@Override
71-
public <T> T get(String key, Class<T> type) {
72-
Object value = get(key);
73-
if (value == null) {
74-
return null;
75-
}
76-
if (!type.isInstance(value)) {
77-
throw new ClassCastException("Value for key '" + key + "' is not of type " + type.getName());
78-
}
79-
return type.cast(value);
80-
}
81-
82-
@Override
83-
public boolean equals(Object o) {
84-
if (o == null || getClass() != o.getClass()) {
85-
return false;
86-
}
87-
DefaultHookData that = (DefaultHookData) o;
88-
return Objects.equals(data, that.data);
89-
}
90-
91-
@Override
92-
public int hashCode() {
93-
return Objects.hashCode(data);
94-
}
95-
}
9635
}

src/main/java/dev/openfeature/sdk/HookExecutor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
@Slf4j
1111
class HookExecutor {
12-
1312
private List<Pair<Hook, HookContext>> hooks;
1413
private EvaluationContext evaluationContext;
1514
private final Map<String, Object> hints;
@@ -29,7 +28,7 @@ public static HookExecutor create(
2928
List<Pair<Hook, HookContext>> hookContextPairs = new ArrayList<>();
3029
for (Hook hook : hooks) {
3130
if (hook.supportsFlagValueType(sharedContext.getType())) {
32-
HookContext curContext = sharedContext.hookContextFor(evaluationContext, HookData.create());
31+
HookContext curContext = sharedContext.hookContextFor(evaluationContext, new DefaultHookData());
3332
hookContextPairs.add(Pair.of(hook, curContext));
3433
}
3534
}

0 commit comments

Comments
 (0)