io.lightflame.routerules.RouteStore Maven / Gradle / Ivy
The newest version!
package io.lightflame.routerules;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
/**
* RuleUtil
*/
public class RouteStore {
private List> routeRules = new ArrayList<>();
public UUID addRouteRule(RouteRules routeRule){
if (routeRule.getKey() == null){
routeRule.setKey(UUID.randomUUID());
}
routeRules.add(routeRule);
return routeRule.getKey();
}
public RouteRules getRouteRules(E income){
Optional> optRule = this.routeRules
.stream()
.filter(x -> x.match(income))
.max(Comparator.comparing((RouteRules r) -> r.score()));
if (!optRule.isPresent()){
return null;
}
return optRule.get();
}
public RouteStore addRuleToStore(String key, Rule rule){
Optional> optRule = this.routeRules
.stream()
.filter(x -> x.getKey().equals(key))
.findFirst();
if (optRule.isPresent()){
optRule.get().addRule(rule);
}
return this;
}
}