I have a spring boot application and we have an application.yml with a set of feature flags on it
featureFlag:
featureOne:true
featureTwo:true
featureThree:true
featureFour:false
Then this file is read by a this class
@Configuration
@ConfigurationProperties(prefix="featureFlag")
public class FeatureFlag{
private Boolean featureOne;
private Boolean featureTwo;
private Boolean featureThree;
private Boolean featureFour;
/*The predicates based on the feature flags*/
private Predicate<FeatureFlag> isFeatureFlagOneEnabled = featureFlag.isFeatureOne();
private Predicate<FeatureFlag> isFeatureFlagTwoEnabled = featureFlag.isFeatureTwo();
private Predicate<FeatureFlag> isFeatureFlagThreeEnabled = featureFlag.isFeatureThree();
private Predicate<FeatureFlag> isFeatureFlagFourEnabled = featureFlag.isFeatureFour();
}
I want to pass the actual predicate and iterate each one of them but I want to know if I can do a generic function that I pass the list of Predicates with its value to be tested and if all of them are true the function return me a true otherwise false
Then in this class add some code like this because I want to generate this list on demand, for example I have a client x that purchase featureOne and featureTwo, in this example I create a list like this
Set<Predicate<FeatureFlag>> rulesForClientX = new HashSet<>();
rulesForClientX.add(isFeatureFlagOneEnabled);
rulesForClientX.add(isFeatureFlagTwoEnabled);
Then I want to create a specific logic for that client and pass it the list of predicates previously created, but I think I would need something like this
Function<List<Predicate<FeatureFlag>>, Boolean> iteratePredicates = (predicates) -> {
//test each predicate and return true if all of them are true otherwise return false
}
Generally, you do not iterate in Prolog. Instead, you write a rule with a pair of recursive clauses, like this: dosomething([]). dosomething([H|T]) :- process(H), dosomething(T).
The best way to iterate the list in terms of performance would be to use iterators ( your second approach using foreach ).
The predicate is a predefined functional interface in Java defined in the java. util. Function package. It helps with manageability of code, aids in unit-testing, and provides various handy functions.
You can create a method that accepts Set<Predicate<FeatureFlag>>
and value, then you can stream set of predicates and use allMatch
public boolean testPredicates(Set<Predicate<FeatureFlag>> predicates, Integer value) {
return predicates.stream().allMatch(pre->pre.test(value));
}
Predicate
sWhat you should look forward to is chaining the predicates that you have.
I pass the list of Predicates with its value to be tested and if all of them are true the function return me a true otherwise false
Based on your requirements this should look like;
public Predicate<FeatureFlag> chainPredicates(Set<Predicate<FeatureFlag>> predicates) {
return predicates.stream()
.reduce(Predicate::and) // all true
.orElse(p -> false); // or false
}
Predicate
Further now, you can consume this single Predicate
easily as
boolean testFeatureFlag(Set<Predicate<FeatureFlag>> predicates, FeatureFlag value) {
return chainPredicates(predicates).test(value);
}
or on a collection say a List<FeatureFlag>
to filter out specific FeatureFlag
s as:
List<FeatureFlag> selectiveFeatures(Set<Predicate<FeatureFlag>> predicates, List<FeatureFlag> featureFlags) {
Predicate<FeatureFlag> flagPredicate = chainPredicates(predicates);
return featureFlags.stream()
.filter(flagPredicate)
.collect(Collectors.toList());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With