Java8里面Predicate
一般情况下我们用过滤一个条件都很单调,就是什么等于什么...直接.equals就行了,但是实际业务中肯定不都是这么简单的逻辑,如果稍有几个过滤条件就需要写多个filter甚至于多个循环才能达到效果。
java 8中的Predicate 接口给我们提供了方便。
请看如下代码
byPanelFilterLossCode的内部实现
private PredicatebyPanelFilterLossCode(List subRuleList) { return panel -> { String lossCode = panel.getPnlRejType()== null ? "" : panel.getPnlRejType(); //TODO: rule check时不读Mysql,所有数据要么放在redis 要么放在本地内存 return subRuleList.stream().anyMatch(subRule -> StringUtils.equalsIgnoreCase(StringUtils.trim(lossCode), subRule.getValue())); }; }
可以看到filter的其实是一个表达式。如果还有点疑惑,请看下面这段代码
public Predicate buildFilterPredicate(MRtmRuleD rule, FileGlassPojo fileGlassPojo, LocalDateTime processEndDateTime) {
final List localDateTimes = RuleCheckUtil.calcMovingRangeDateime(rule, processEndDateTime);
final long startLongTime = localDateTimes.get(0).atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
final long processEndLongTime = localDateTimes.get(1).atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
Predicate filter = tuple -> tuple.getScore() >= startLongTime && tuple.getScore() <= processEndLongTime;
return filter;
}



