com.nike.moirai.ConfigFeatureFlagChecker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moirai-core Show documentation
Show all versions of moirai-core Show documentation
A feature-flag and resource-reloading library for the JVM
package com.nike.moirai;
import com.nike.moirai.config.ConfigDecisionInput;
import com.nike.moirai.resource.reload.ResourceReloader;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* Checks if a feature is enabled using a given supplier of configuration and a predicate for config decisions for that configuration type.
*
* @param the type of config
*/
public class ConfigFeatureFlagChecker implements FeatureFlagChecker {
private final Supplier configSupplier;
private final Predicate> configDecider;
/**
* Given a resource reloader, gets the current config value and calls the configDecider using that config value.
*
* @param resourceReloader a reloader for the config
* @param configDecider a predicate for input using the config
* @param the type of config
* @return a checker for the config from the resource reloader and the provided predicate
*/
public static ConfigFeatureFlagChecker forReloadableResource(ResourceReloader resourceReloader, Predicate> configDecider) {
return new ConfigFeatureFlagChecker<>(resourceReloader::getValue, configDecider);
}
/**
* Given a supplier of a config, calls the supplier for each decision and then calls the configDecider using that config value.
*
* @param configSupplier a supplier of config
* @param configDecider a predicate for input using the config
* @param the type of config
* @return a checker for the config from the supplier and the provided predicate
*/
public static ConfigFeatureFlagChecker forConfigSupplier(Supplier configSupplier, Predicate> configDecider) {
return new ConfigFeatureFlagChecker<>(configSupplier, configDecider);
}
private ConfigFeatureFlagChecker(
Supplier configSupplier,
Predicate> configDecider) {
this.configSupplier = configSupplier;
this.configDecider = configDecider;
}
@Override
public boolean isFeatureEnabled(String featureIdentifier, FeatureCheckInput featureCheckInput) {
return this.configDecider.test(new ConfigDecisionInput<>(this.configSupplier.get(), featureIdentifier, featureCheckInput));
}
}