1
1
package dev .openfeature .sdk .providers .memory ;
2
2
3
- import java .util .ArrayList ;
4
- import java .util .Arrays ;
5
- import java .util .HashMap ;
6
- import java .util .HashSet ;
7
- import java .util .Map ;
8
- import java .util .Set ;
9
-
10
3
import dev .openfeature .sdk .EvaluationContext ;
11
4
import dev .openfeature .sdk .EventProvider ;
12
5
import dev .openfeature .sdk .Metadata ;
24
17
import lombok .SneakyThrows ;
25
18
import lombok .extern .slf4j .Slf4j ;
26
19
20
+ import java .util .ArrayList ;
21
+ import java .util .Collections ;
22
+ import java .util .HashSet ;
23
+ import java .util .Map ;
24
+ import java .util .Set ;
25
+ import java .util .concurrent .ConcurrentHashMap ;
26
+
27
27
/**
28
28
* In-memory provider.
29
29
*/
@@ -33,7 +33,7 @@ public class InMemoryProvider extends EventProvider {
33
33
@ Getter
34
34
private static final String NAME = "InMemoryProvider" ;
35
35
36
- private Map <String , Flag <?>> flags ;
36
+ private final Map <String , Flag <?>> flags ;
37
37
38
38
@ Getter
39
39
private ProviderState state = ProviderState .NOT_READY ;
@@ -44,11 +44,11 @@ public Metadata getMetadata() {
44
44
}
45
45
46
46
public InMemoryProvider (Map <String , Flag <?>> flags ) {
47
- this .flags = new HashMap <>(flags );
47
+ this .flags = new ConcurrentHashMap <>(flags );
48
48
}
49
49
50
50
/**
51
- * Initialize the provider.
51
+ * Initializes the provider.
52
52
* @param evaluationContext evaluation context
53
53
* @throws Exception on error
54
54
*/
@@ -60,14 +60,15 @@ public void initialize(EvaluationContext evaluationContext) throws Exception {
60
60
}
61
61
62
62
/**
63
- * Updating provider flags configuration, replacing existing flags.
64
- * @param flags the flags to use instead of the previous flags.
63
+ * Updates the provider flags configuration.
64
+ * For existing flags, the new configurations replace the old one.
65
+ * For new flags, they are added to the configuration.
66
+ * @param newFlags the new flag configurations
65
67
*/
66
- public void updateFlags (Map <String , Flag <?>> flags ) {
67
- Set <String > flagsChanged = new HashSet <>();
68
- flagsChanged .addAll (this .flags .keySet ());
69
- flagsChanged .addAll (flags .keySet ());
70
- this .flags = new HashMap <>(flags );
68
+ public void updateFlags (Map <String , Flag <?>> newFlags ) {
69
+ Set <String > flagsChanged = new HashSet <>(newFlags .keySet ());
70
+ this .flags .putAll (newFlags );
71
+
71
72
ProviderEventDetails details = ProviderEventDetails .builder ()
72
73
.flagsChanged (new ArrayList <>(flagsChanged ))
73
74
.message ("flags changed" )
@@ -76,13 +77,15 @@ public void updateFlags(Map<String, Flag<?>> flags) {
76
77
}
77
78
78
79
/**
79
- * Updating provider flags configuration with adding or updating a flag.
80
- * @param flag the flag to update. If a flag with this key already exists, new flag replaces it.
80
+ * Updates a single provider flag configuration.
81
+ * For existing flag, the new configuration replaces the old one.
82
+ * For new flag, they are added to the configuration.
83
+ * @param newFlag the flag to update
81
84
*/
82
- public void updateFlag (String flagKey , Flag <?> flag ) {
83
- this .flags .put (flagKey , flag );
85
+ public void updateFlag (String flagKey , Flag <?> newFlag ) {
86
+ this .flags .put (flagKey , newFlag );
84
87
ProviderEventDetails details = ProviderEventDetails .builder ()
85
- .flagsChanged (Arrays . asList (flagKey ))
88
+ .flagsChanged (Collections . singletonList (flagKey ))
86
89
.message ("flag added/updated" )
87
90
.build ();
88
91
emitProviderConfigurationChanged (details );
0 commit comments