Skip to content

Commit 898c4a7

Browse files
committed
fixup: split into packages
Signed-off-by: Simon Schrottner <[email protected]>
1 parent d29c42d commit 898c4a7

File tree

148 files changed

+1208
-828
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+1208
-828
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package dev.openfeature.api;
2+
3+
import dev.openfeature.api.evaluation.EvaluationContext;
4+
import dev.openfeature.api.events.EventEmitter;
5+
import dev.openfeature.api.events.EventProvider;
6+
import dev.openfeature.api.events.ProviderEventDetails;
7+
import dev.openfeature.api.internal.TriConsumer;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.concurrent.ExecutorService;
11+
import java.util.concurrent.Executors;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
/**
16+
* Abstract EventProvider. Providers must extend this class to support events.
17+
* Emit events with {@link #emit(ProviderEvent, ProviderEventDetails)}. Please
18+
* note that the SDK will automatically emit
19+
* {@link ProviderEvent#PROVIDER_READY } or
20+
* {@link ProviderEvent#PROVIDER_ERROR } accordingly when
21+
* {@link Provider#initialize(EvaluationContext)} completes successfully
22+
* or with error, so these events need not be emitted manually during
23+
* initialization.
24+
*
25+
* @see Provider
26+
*/
27+
public abstract class AbstractEventProvider implements EventProvider {
28+
private static final Logger log = LoggerFactory.getLogger(AbstractEventProvider.class);
29+
private EventEmitter eventEmitter;
30+
private final ExecutorService emitterExecutor = Executors.newCachedThreadPool();
31+
private List<Hook<?>> hooks;
32+
33+
public void setEventEmitter(EventEmitter eventEmitter) {
34+
this.eventEmitter = eventEmitter;
35+
}
36+
37+
/**
38+
* "Attach" this EventProvider to an SDK, which allows events to propagate from this provider.
39+
* No-op if the same onEmit is already attached.
40+
*
41+
* @param onEmit the function to run when a provider emits events.
42+
* @throws IllegalStateException if attempted to bind a new emitter for already bound provider
43+
*/
44+
public void attach(TriConsumer<EventProvider, ProviderEvent, ProviderEventDetails> onEmit) {
45+
if (eventEmitter == null) {
46+
return;
47+
}
48+
eventEmitter.attach(onEmit);
49+
}
50+
51+
/**
52+
* "Detach" this EventProvider from an SDK, stopping propagation of all events.
53+
*/
54+
public void detach() {
55+
if (eventEmitter == null) {
56+
return;
57+
}
58+
eventEmitter.detach();
59+
}
60+
61+
/**
62+
* Stop the event emitter executor and block until either termination has completed
63+
* or timeout period has elapsed.
64+
*/
65+
@Override
66+
public void shutdown() {
67+
if (eventEmitter == null) {
68+
return;
69+
}
70+
eventEmitter.shutdown();
71+
}
72+
73+
/**
74+
* Emit the specified {@link ProviderEvent}.
75+
*
76+
* @param event The event type
77+
* @param details The details of the event
78+
*/
79+
public Awaitable emit(final ProviderEvent event, final ProviderEventDetails details) {
80+
return eventEmitter.emit(event, details);
81+
}
82+
83+
@Override
84+
public Provider addHooks(Hook<?>... hooks) {
85+
if (this.hooks == null) {
86+
this.hooks = new ArrayList<>();
87+
}
88+
this.hooks.addAll(List.of(hooks));
89+
return this;
90+
}
91+
92+
@Override
93+
public List<Hook<?>> getHooks() {
94+
if (hooks == null) {
95+
return List.of();
96+
}
97+
return List.copyOf(hooks);
98+
}
99+
100+
@Override
101+
public void clearHooks() {
102+
if (hooks == null) {
103+
return;
104+
}
105+
hooks.clear();
106+
}
107+
}

openfeature-api/src/main/java/dev/openfeature/api/Client.java

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,19 @@
11
package dev.openfeature.api;
22

3-
import java.util.List;
3+
import dev.openfeature.api.evaluation.EvaluationClient;
4+
import dev.openfeature.api.evaluation.EvaluationContextHolder;
5+
import dev.openfeature.api.events.EventBus;
6+
import dev.openfeature.api.lifecycle.Hookable;
7+
import dev.openfeature.api.tracking.Tracking;
8+
import dev.openfeature.api.types.ClientMetadata;
49

510
/**
611
* Interface used to resolve flags of varying types.
712
*/
8-
public interface Client extends Features, Tracking, EventBus<Client> {
13+
public interface Client
14+
extends EvaluationClient, Tracking, EventBus<Client>, Hookable<Client>, EvaluationContextHolder<Client> {
915
ClientMetadata getMetadata();
1016

11-
/**
12-
* Return an optional client-level evaluation context.
13-
*
14-
* @return {@link EvaluationContext}
15-
*/
16-
EvaluationContext getEvaluationContext();
17-
18-
/**
19-
* Set the client-level evaluation context.
20-
*
21-
* @param ctx Client level context.
22-
*/
23-
Client setEvaluationContext(EvaluationContext ctx);
24-
25-
/**
26-
* Adds hooks for evaluation.
27-
* Hooks are run in the order they're added in the before stage. They are run in reverse order for all other stages.
28-
*
29-
* @param hooks The hook to add.
30-
*/
31-
Client addHooks(Hook... hooks);
32-
33-
/**
34-
* Fetch the hooks associated to this client.
35-
*
36-
* @return A list of {@link Hook}s.
37-
*/
38-
List<Hook> getHooks();
39-
4017
/**
4118
* Returns the current state of the associated provider.
4219
*

openfeature-api/src/main/java/dev/openfeature/api/Hook.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package dev.openfeature.api;
22

3+
import dev.openfeature.api.evaluation.EvaluationContext;
4+
import dev.openfeature.api.evaluation.FlagEvaluationDetails;
5+
import dev.openfeature.api.lifecycle.HookContext;
36
import java.util.Map;
47
import java.util.Optional;
58

openfeature-api/src/main/java/dev/openfeature/api/OpenFeatureAPI.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package dev.openfeature.api;
22

3+
import dev.openfeature.api.evaluation.EvaluationContextHolder;
4+
import dev.openfeature.api.events.EventBus;
35
import dev.openfeature.api.internal.noop.NoOpOpenFeatureAPI;
6+
import dev.openfeature.api.lifecycle.Hookable;
7+
import dev.openfeature.api.lifecycle.Lifecycle;
48
import java.util.ServiceLoader;
59

610
/**
@@ -18,14 +22,14 @@
1822
*/
1923
public abstract class OpenFeatureAPI
2024
implements OpenFeatureCore,
21-
OpenFeatureHooks,
22-
OpenFeatureContext,
23-
OpenFeatureEventHandling,
24-
OpenFeatureTransactionContext,
25-
OpenFeatureLifecycle {
26-
25+
Hookable<OpenFeatureAPI>,
26+
EvaluationContextHolder<OpenFeatureAPI>,
27+
EventBus<OpenFeatureAPI>,
28+
Transactional,
29+
Lifecycle {
30+
// package-private multi-read/single-write lock
2731
private static volatile OpenFeatureAPI instance;
28-
private static final Object lock = new Object();
32+
private static final Object instanceLock = new Object();
2933

3034
/**
3135
* Gets the singleton OpenFeature API instance.
@@ -35,7 +39,7 @@ public abstract class OpenFeatureAPI
3539
*/
3640
public static OpenFeatureAPI getInstance() {
3741
if (instance == null) {
38-
synchronized (lock) {
42+
synchronized (instanceLock) {
3943
if (instance == null) {
4044
instance = loadImplementation();
4145
}
@@ -89,7 +93,7 @@ private static OpenFeatureAPI loadImplementation() {
8993
* and should be used with caution in production environments.
9094
*/
9195
protected static void resetInstance() {
92-
synchronized (lock) {
96+
synchronized (instanceLock) {
9397
instance = null;
9498
}
9599
}

openfeature-api/src/main/java/dev/openfeature/api/OpenFeatureContext.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

openfeature-api/src/main/java/dev/openfeature/api/OpenFeatureCore.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.openfeature.api;
22

33
import dev.openfeature.api.exceptions.OpenFeatureError;
4+
import dev.openfeature.api.types.ProviderMetadata;
45

56
/**
67
* Core interface for basic OpenFeature operations.
@@ -47,26 +48,26 @@ public interface OpenFeatureCore {
4748
*
4849
* @param provider the provider to set as default
4950
*/
50-
void setProvider(FeatureProvider provider);
51+
void setProvider(Provider provider);
5152

5253
/**
5354
* Add a provider for a domain.
5455
*
5556
* @param domain The domain to bind the provider to.
5657
* @param provider The provider to set.
5758
*/
58-
void setProvider(String domain, FeatureProvider provider);
59+
void setProvider(String domain, Provider provider);
5960

6061
/**
6162
* Sets the default provider and waits for its initialization to complete.
6263
*
6364
* <p>Note: If the provider fails during initialization, an {@link OpenFeatureError} will be thrown.
6465
* It is recommended to wrap this call in a try-catch block to handle potential initialization failures gracefully.
6566
*
66-
* @param provider the {@link FeatureProvider} to set as the default.
67+
* @param provider the {@link Provider} to set as the default.
6768
* @throws OpenFeatureError if the provider fails during initialization.
6869
*/
69-
void setProviderAndWait(FeatureProvider provider) throws OpenFeatureError;
70+
void setProviderAndWait(Provider provider) throws OpenFeatureError;
7071

7172
/**
7273
* Add a provider for a domain and wait for initialization to finish.
@@ -78,20 +79,20 @@ public interface OpenFeatureCore {
7879
* @param provider The provider to set.
7980
* @throws OpenFeatureError if the provider fails during initialization.
8081
*/
81-
void setProviderAndWait(String domain, FeatureProvider provider) throws OpenFeatureError;
82+
void setProviderAndWait(String domain, Provider provider) throws OpenFeatureError;
8283

8384
/**
8485
* Return the default provider.
8586
*/
86-
FeatureProvider getProvider();
87+
Provider getProvider();
8788

8889
/**
8990
* Fetch a provider for a domain. If not found, return the default.
9091
*
9192
* @param domain The domain to look for.
92-
* @return A named {@link FeatureProvider}
93+
* @return A named {@link Provider}
9394
*/
94-
FeatureProvider getProvider(String domain);
95+
Provider getProvider(String domain);
9596

9697
/**
9798
* Get metadata about the default provider.

openfeature-api/src/main/java/dev/openfeature/api/OpenFeatureEventHandling.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

openfeature-api/src/main/java/dev/openfeature/api/OpenFeatureHooks.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)