11package dev .openfeature .sdk .providers .memory ;
22
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-
103import dev .openfeature .sdk .EvaluationContext ;
114import dev .openfeature .sdk .EventProvider ;
125import dev .openfeature .sdk .Metadata ;
2417import lombok .SneakyThrows ;
2518import lombok .extern .slf4j .Slf4j ;
2619
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+
2727/**
2828 * In-memory provider.
2929 */
@@ -33,7 +33,7 @@ public class InMemoryProvider extends EventProvider {
3333 @ Getter
3434 private static final String NAME = "InMemoryProvider" ;
3535
36- private Map <String , Flag <?>> flags ;
36+ private final Map <String , Flag <?>> flags ;
3737
3838 @ Getter
3939 private ProviderState state = ProviderState .NOT_READY ;
@@ -44,11 +44,11 @@ public Metadata getMetadata() {
4444 }
4545
4646 public InMemoryProvider (Map <String , Flag <?>> flags ) {
47- this .flags = new HashMap <>(flags );
47+ this .flags = new ConcurrentHashMap <>(flags );
4848 }
4949
5050 /**
51- * Initialize the provider.
51+ * Initializes the provider.
5252 * @param evaluationContext evaluation context
5353 * @throws Exception on error
5454 */
@@ -60,14 +60,15 @@ public void initialize(EvaluationContext evaluationContext) throws Exception {
6060 }
6161
6262 /**
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
6567 */
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+
7172 ProviderEventDetails details = ProviderEventDetails .builder ()
7273 .flagsChanged (new ArrayList <>(flagsChanged ))
7374 .message ("flags changed" )
@@ -76,13 +77,15 @@ public void updateFlags(Map<String, Flag<?>> flags) {
7677 }
7778
7879 /**
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
8184 */
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 );
8487 ProviderEventDetails details = ProviderEventDetails .builder ()
85- .flagsChanged (Arrays . asList (flagKey ))
88+ .flagsChanged (Collections . singletonList (flagKey ))
8689 .message ("flag added/updated" )
8790 .build ();
8891 emitProviderConfigurationChanged (details );
0 commit comments