|
| 1 | +package dev.openfeature.sdk; |
| 2 | + |
| 3 | +import dev.openfeature.sdk.internal.TriConsumer; |
| 4 | + |
| 5 | +/** |
| 6 | + * Abstract EventProvider. Providers must extend this class to support events. |
| 7 | + * Emit events with {@link #emit(ProviderEvent, ProviderEventDetails)}. Please |
| 8 | + * note that the SDK will automatically emit |
| 9 | + * {@link ProviderEvent#PROVIDER_READY } or |
| 10 | + * {@link ProviderEvent#PROVIDER_ERROR } accordingly when |
| 11 | + * {@link FeatureProvider#initialize(EvaluationContext)} completes successfully |
| 12 | + * or with error, so these events need not be emitted manually during |
| 13 | + * initialization. |
| 14 | + * |
| 15 | + * @see FeatureProvider |
| 16 | + */ |
| 17 | +public abstract class EventProvider implements FeatureProvider { |
| 18 | + |
| 19 | + private TriConsumer<EventProvider, ProviderEvent, ProviderEventDetails> onEmit = null; |
| 20 | + |
| 21 | + /** |
| 22 | + * "Attach" this EventProvider to an SDK, which allows events to propagate from this provider. |
| 23 | + * No-op if the same onEmit is already attached. |
| 24 | + * |
| 25 | + * @param onEmit the function to run when a provider emits events. |
| 26 | + */ |
| 27 | + void attach(TriConsumer<EventProvider, ProviderEvent, ProviderEventDetails> onEmit) { |
| 28 | + if (this.onEmit != null && this.onEmit != onEmit) { |
| 29 | + // if we are trying to attach this provider to a different onEmit, something has gone wrong |
| 30 | + throw new IllegalStateException("Provider " + this.getMetadata().getName() + " is already attached."); |
| 31 | + } else { |
| 32 | + this.onEmit = onEmit; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * "Detach" this EventProvider from an SDK, stopping propagation of all events. |
| 38 | + */ |
| 39 | + void detach() { |
| 40 | + this.onEmit = null; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Emit the specified {@link ProviderEvent}. |
| 45 | + * |
| 46 | + * @param event The event type |
| 47 | + * @param details The details of the event |
| 48 | + */ |
| 49 | + public void emit(ProviderEvent event, ProviderEventDetails details) { |
| 50 | + if (this.onEmit != null) { |
| 51 | + this.onEmit.accept(this, event, details); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Emit a {@link ProviderEvent#PROVIDER_READY} event. |
| 57 | + * Shorthand for {@link #emit(ProviderEvent, ProviderEventDetails)} |
| 58 | + * |
| 59 | + * @param details The details of the event |
| 60 | + */ |
| 61 | + public void emitProviderReady(ProviderEventDetails details) { |
| 62 | + emit(ProviderEvent.PROVIDER_READY, details); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Emit a |
| 67 | + * {@link ProviderEvent#PROVIDER_CONFIGURATION_CHANGED} |
| 68 | + * event. Shorthand for {@link #emit(ProviderEvent, ProviderEventDetails)} |
| 69 | + * |
| 70 | + * @param details The details of the event |
| 71 | + */ |
| 72 | + public void emitProviderConfigurationChanged(ProviderEventDetails details) { |
| 73 | + emit(ProviderEvent.PROVIDER_CONFIGURATION_CHANGED, details); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Emit a {@link ProviderEvent#PROVIDER_STALE} event. |
| 78 | + * Shorthand for {@link #emit(ProviderEvent, ProviderEventDetails)} |
| 79 | + * |
| 80 | + * @param details The details of the event |
| 81 | + */ |
| 82 | + public void emitProviderStale(ProviderEventDetails details) { |
| 83 | + emit(ProviderEvent.PROVIDER_STALE, details); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Emit a {@link ProviderEvent#PROVIDER_ERROR} event. |
| 88 | + * Shorthand for {@link #emit(ProviderEvent, ProviderEventDetails)} |
| 89 | + * |
| 90 | + * @param details The details of the event |
| 91 | + */ |
| 92 | + public void emitProviderError(ProviderEventDetails details) { |
| 93 | + emit(ProviderEvent.PROVIDER_ERROR, details); |
| 94 | + } |
| 95 | +} |
0 commit comments