diff --git a/.gitignore b/.gitignore
index 79c51f6..9d6e329 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,7 @@
# Gradle
.gradle/
/build/
+buildSrc/build/
# IDE
.idea/
diff --git a/build.gradle.kts b/build.gradle.kts
index 40ab170..22f5440 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -10,7 +10,7 @@ plugins {
group = "io.flamingock"
-val declaredVersion = "1.5.3-SNAPSHOT"
+val declaredVersion = "1.6.0-SNAPSHOT"
version = VersionManager.resolveVersion(declaredVersion, project.hasProperty("release"))
repositories {
diff --git a/src/main/java/io/flamingock/internal/util/FeatureFlag.java b/src/main/java/io/flamingock/internal/util/FeatureFlag.java
new file mode 100644
index 0000000..63d6c68
--- /dev/null
+++ b/src/main/java/io/flamingock/internal/util/FeatureFlag.java
@@ -0,0 +1,219 @@
+/*
+ * Copyright 2025 Flamingock (https://www.flamingock.io)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.flamingock.internal.util;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Supplier;
+
+/**
+ * Simple, internal, in-memory feature flag registry.
+ *
+ *
Flags are identified by name and are disabled unless explicitly enabled. There is no external
+ * configuration source: flags are only turned on and off from code, so they cannot be overridden by
+ * the user of the library.
+ *
+ *
Flags can be queried, to be used in a regular condition:
+ *
+ *
{@code
+ * FeatureFlag.enable("my-feature");
+ *
+ * if (FeatureFlag.isEnabled("my-feature")) {
+ * ...
+ * }
+ * }
+ *
+ * or used to guard an action directly:
+ *
+ *
{@code
+ * FeatureFlag.ifEnabled("my-feature", () -> service.doNewThing());
+ * FeatureFlag.ifEnabledOrElse("my-feature", () -> newWay(), () -> oldWay());
+ *
+ * String value = FeatureFlag.getIfEnabled("my-feature", () -> newValue(), "fallback");
+ * }
+ *
+ * The state is global and shared by the whole JVM. This class is thread-safe.
+ */
+public final class FeatureFlag {
+
+ private static final Map FLAGS = new ConcurrentHashMap<>();
+
+ private FeatureFlag() {
+ }
+
+ /**
+ * Enables the given feature.
+ *
+ * @param feature feature name
+ * @return whether the feature was enabled before this call
+ */
+ public static boolean enable(String feature) {
+ return set(feature, true);
+ }
+
+ /**
+ * Disables the given feature.
+ *
+ * @param feature feature name
+ * @return whether the feature was enabled before this call
+ */
+ public static boolean disable(String feature) {
+ return set(feature, false);
+ }
+
+ /**
+ * Sets the state of the given feature.
+ *
+ * @param feature feature name
+ * @param enabled new state
+ * @return whether the feature was enabled before this call
+ */
+ public static boolean set(String feature, boolean enabled) {
+ return isEnabled(FLAGS.put(validateFeature(feature), enabled));
+ }
+
+ /**
+ * Removes the given feature from the registry, so it goes back to its default state(disabled).
+ *
+ * @param feature feature name
+ * @return whether the feature was enabled before this call
+ */
+ public static boolean remove(String feature) {
+ return isEnabled(FLAGS.remove(validateFeature(feature)));
+ }
+
+ /**
+ * Removes every feature from the registry. Mainly intended for tests.
+ */
+ public static void clear() {
+ FLAGS.clear();
+ }
+
+ /**
+ * Returns whether the given feature is enabled. Unknown features are considered disabled.
+ *
+ * @param feature feature name
+ * @return true if the feature is enabled
+ */
+ public static boolean isEnabled(String feature) {
+ return isEnabled(feature, false);
+ }
+
+ /**
+ * Returns whether the given feature is enabled, providing the value to be used when the feature
+ * hasn't been explicitly set.
+ *
+ * @param feature feature name
+ * @param defaultValue value returned when the feature is not registered
+ * @return true if the feature is enabled
+ */
+ public static boolean isEnabled(String feature, boolean defaultValue) {
+ Boolean state = FLAGS.get(validateFeature(feature));
+ return state != null ? state : defaultValue;
+ }
+
+ /**
+ * Returns whether the given feature is disabled. Unknown features are considered disabled.
+ *
+ * @param feature feature name
+ * @return true if the feature is disabled
+ */
+ public static boolean isDisabled(String feature) {
+ return !isEnabled(feature);
+ }
+
+ /**
+ * Returns whether the given feature is disabled, providing the value to be used when the feature
+ * hasn't been explicitly set.
+ *
+ * @param feature feature name
+ * @param defaultValue state assumed when the feature is not registered
+ * @return true if the feature is disabled
+ */
+ public static boolean isDisabled(String feature, boolean defaultValue) {
+ return !isEnabled(feature, defaultValue);
+ }
+
+ /**
+ * Runs the given action only if the feature is enabled.
+ *
+ * @param feature feature name
+ * @param action action to run when the feature is enabled
+ */
+ public static void ifEnabled(String feature, Runnable action) {
+ Objects.requireNonNull(action, "action must not be null");
+ if (isEnabled(feature)) {
+ action.run();
+ }
+ }
+
+ /**
+ * Runs the given action only if the feature is disabled.
+ *
+ * @param feature feature name
+ * @param action action to run when the feature is disabled
+ */
+ public static void ifDisabled(String feature, Runnable action) {
+ Objects.requireNonNull(action, "action must not be null");
+ if (isDisabled(feature)) {
+ action.run();
+ }
+ }
+
+ /**
+ * Runs one action or the other, depending on the state of the feature.
+ *
+ * @param feature feature name
+ * @param action action to run when the feature is enabled
+ * @param fallbackAction action to run when the feature is disabled
+ */
+ public static void ifEnabledOrElse(String feature, Runnable action, Runnable fallbackAction) {
+ Objects.requireNonNull(action, "action must not be null");
+ Objects.requireNonNull(fallbackAction, "fallbackAction must not be null");
+ if (isEnabled(feature)) {
+ action.run();
+ } else {
+ fallbackAction.run();
+ }
+ }
+
+ /**
+ * Returns the value provided by the supplier if the feature is enabled, or the fallback value
+ * otherwise. The supplier is only invoked when the feature is enabled.
+ *
+ * @param feature feature name
+ * @param supplier supplier invoked when the feature is enabled
+ * @param fallback value returned when the feature is disabled
+ * @param returned type
+ * @return the supplied value or the fallback
+ */
+ public static T getIfEnabled(String feature, Supplier supplier, T fallback) {
+ Objects.requireNonNull(supplier, "supplier must not be null");
+ return isEnabled(feature) ? supplier.get() : fallback;
+ }
+
+ private static boolean isEnabled(Boolean state) {
+ return state != null && state;
+ }
+
+ private static String validateFeature(String feature) {
+ if (StringUtil.isEmpty(feature)) {
+ throw new IllegalArgumentException("feature must not be null or empty");
+ }
+ return feature;
+ }
+}
diff --git a/src/test/java/io/flamingock/internal/util/FeatureFlagTest.java b/src/test/java/io/flamingock/internal/util/FeatureFlagTest.java
new file mode 100644
index 0000000..24be6dd
--- /dev/null
+++ b/src/test/java/io/flamingock/internal/util/FeatureFlagTest.java
@@ -0,0 +1,188 @@
+/*
+ * Copyright 2025 Flamingock (https://www.flamingock.io)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.flamingock.internal.util;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class FeatureFlagTest {
+
+ private static final String FEATURE = "my-feature";
+
+ @AfterEach
+ void tearDown() {
+ FeatureFlag.clear();
+ }
+
+ @Test
+ void shouldBeDisabledWhenNotRegistered() {
+ assertFalse(FeatureFlag.isEnabled(FEATURE));
+ assertTrue(FeatureFlag.isDisabled(FEATURE));
+ }
+
+ @Test
+ void shouldUseProvidedDefaultWhenNotRegistered() {
+ assertTrue(FeatureFlag.isEnabled(FEATURE, true));
+ assertFalse(FeatureFlag.isDisabled(FEATURE, true));
+ }
+
+ @Test
+ void shouldIgnoreProvidedDefaultWhenRegistered() {
+ FeatureFlag.disable(FEATURE);
+
+ assertFalse(FeatureFlag.isEnabled(FEATURE, true));
+ assertTrue(FeatureFlag.isDisabled(FEATURE, true));
+ }
+
+ @Test
+ void shouldEnableAndDisableFeature() {
+ FeatureFlag.enable(FEATURE);
+ assertTrue(FeatureFlag.isEnabled(FEATURE));
+ assertFalse(FeatureFlag.isDisabled(FEATURE));
+
+ FeatureFlag.disable(FEATURE);
+ assertFalse(FeatureFlag.isEnabled(FEATURE));
+ assertTrue(FeatureFlag.isDisabled(FEATURE));
+ }
+
+ @Test
+ void shouldSetFeatureFromBooleanValue() {
+ FeatureFlag.set(FEATURE, true);
+ assertTrue(FeatureFlag.isEnabled(FEATURE));
+
+ FeatureFlag.set(FEATURE, false);
+ assertFalse(FeatureFlag.isEnabled(FEATURE));
+ }
+
+ @Test
+ void shouldReturnPreviousStateOnMutation() {
+ assertFalse(FeatureFlag.enable(FEATURE));
+ assertTrue(FeatureFlag.enable(FEATURE));
+ assertTrue(FeatureFlag.disable(FEATURE));
+ assertFalse(FeatureFlag.set(FEATURE, true));
+ assertTrue(FeatureFlag.remove(FEATURE));
+ assertFalse(FeatureFlag.remove(FEATURE));
+ }
+
+ @Test
+ void shouldGoBackToDefaultWhenRemoved() {
+ FeatureFlag.enable(FEATURE);
+ FeatureFlag.remove(FEATURE);
+
+ assertFalse(FeatureFlag.isEnabled(FEATURE));
+ assertTrue(FeatureFlag.isEnabled(FEATURE, true));
+ }
+
+ @Test
+ void shouldClearEveryFeature() {
+ FeatureFlag.enable(FEATURE);
+ FeatureFlag.enable("another-feature");
+
+ FeatureFlag.clear();
+
+ assertFalse(FeatureFlag.isEnabled(FEATURE));
+ assertFalse(FeatureFlag.isEnabled("another-feature"));
+ }
+
+ @Test
+ void shouldNotMixFeatures() {
+ FeatureFlag.enable(FEATURE);
+
+ assertTrue(FeatureFlag.isEnabled(FEATURE));
+ assertFalse(FeatureFlag.isEnabled("another-feature"));
+ }
+
+ @Test
+ void shouldRunActionOnlyWhenEnabled() {
+ AtomicInteger executions = new AtomicInteger(0);
+
+ FeatureFlag.ifEnabled(FEATURE, executions::incrementAndGet);
+ assertEquals(0, executions.get());
+
+ FeatureFlag.enable(FEATURE);
+ FeatureFlag.ifEnabled(FEATURE, executions::incrementAndGet);
+ assertEquals(1, executions.get());
+ }
+
+ @Test
+ void shouldRunActionOnlyWhenDisabled() {
+ AtomicInteger executions = new AtomicInteger(0);
+
+ FeatureFlag.ifDisabled(FEATURE, executions::incrementAndGet);
+ assertEquals(1, executions.get());
+
+ FeatureFlag.enable(FEATURE);
+ FeatureFlag.ifDisabled(FEATURE, executions::incrementAndGet);
+ assertEquals(1, executions.get());
+ }
+
+ @Test
+ void shouldRunFallbackActionWhenDisabled() {
+ AtomicInteger actionExecutions = new AtomicInteger(0);
+ AtomicInteger fallbackExecutions = new AtomicInteger(0);
+
+ FeatureFlag.ifEnabledOrElse(FEATURE, actionExecutions::incrementAndGet, fallbackExecutions::incrementAndGet);
+ assertEquals(0, actionExecutions.get());
+ assertEquals(1, fallbackExecutions.get());
+
+ FeatureFlag.enable(FEATURE);
+ FeatureFlag.ifEnabledOrElse(FEATURE, actionExecutions::incrementAndGet, fallbackExecutions::incrementAndGet);
+ assertEquals(1, actionExecutions.get());
+ assertEquals(1, fallbackExecutions.get());
+ }
+
+ @Test
+ void shouldReturnFallbackValueWhenDisabled() {
+ assertEquals("fallback", FeatureFlag.getIfEnabled(FEATURE, () -> "supplied", "fallback"));
+
+ FeatureFlag.enable(FEATURE);
+ assertEquals("supplied", FeatureFlag.getIfEnabled(FEATURE, () -> "supplied", "fallback"));
+ }
+
+ @Test
+ void shouldNotInvokeSupplierWhenDisabled() {
+ AtomicInteger invocations = new AtomicInteger(0);
+
+ FeatureFlag.getIfEnabled(FEATURE, invocations::incrementAndGet, -1);
+
+ assertEquals(0, invocations.get());
+ }
+
+ @Test
+ void shouldRejectInvalidFeatureName() {
+ assertThrows(IllegalArgumentException.class, () -> FeatureFlag.isEnabled(null));
+ assertThrows(IllegalArgumentException.class, () -> FeatureFlag.isEnabled(""));
+ assertThrows(IllegalArgumentException.class, () -> FeatureFlag.enable(null));
+ assertThrows(IllegalArgumentException.class, () -> FeatureFlag.enable(""));
+ }
+
+ @Test
+ void shouldRejectNullAction() {
+ FeatureFlag.enable(FEATURE);
+
+ assertThrows(NullPointerException.class, () -> FeatureFlag.ifEnabled(FEATURE, null));
+ assertThrows(NullPointerException.class, () -> FeatureFlag.ifDisabled(FEATURE, null));
+ assertThrows(NullPointerException.class, () -> FeatureFlag.ifEnabledOrElse(FEATURE, null, () -> {}));
+ assertThrows(NullPointerException.class, () -> FeatureFlag.getIfEnabled(FEATURE, null, "fallback"));
+ }
+}