目的是创建一个可在流过滤器中使用的新谓词:
myCollectionOfElement
.stream()
.filter(
MyStaticHelperClass.compose(MyStaticHelperClass.getSubElement1OfTheElement(),MyStaticHelperClass.getPredicate1OnSubElement1()))
.sorted(MyStaticHelperClass.getOtherSubElement().reversed())
.limit(10)
.collect(Collectors.toList())
Run Code Online (Sandbox Code Playgroud)
getSubElement1OfTheElement()返回Function<E,S>(E包含S属性)
getPredicate1OnSubElement1()返回Predicate<S>
我使用静态函数来公开方法引用和函数.我这样做是因为在Velocity模板中调用了流,并且此上下文不支持lambda语法和方法引用. 我不想为所有可能的组合创建静态函数,所以我真的希望它们是可组合的.
例如,在这里,我不想有静态,getPredicate1OnElementThatCheckProperty1OnTheSubElement1()因为我可以撰写getSubElement1OfTheElement()和getPredicate1OnSubElement1().
所以我需要一个撰写函数:
// returns a new Predicate constructed by applying Predicate predicate on the result of Function function
public static <E,S> Predicate<E> compose(Function<E,S> function, Predicate<S> predicate)
// most intuitive : lambda
return value -> predicate.test(function.apply(value));
// with method references
return function.andThen(predicate::test)::apply;
// predicate.compose is not available because Predicate interface doesn't extends Function …Run Code Online (Sandbox Code Playgroud) 我在内存数据库中使用Play Framework 1.2.4和H2.
public void aBigDecimalSavingTest() {
BigDecimalEntity bde = new BigDecimalEntity();
bde.bd= new BigDecimal("0.225");
System.out.println(bde.bd); // print 0.225
bde.save();
bde = BigDecimalEntity.findById(Long.valueOf("1"));
System.out.println(bde.bd); // print 0.23
}
Run Code Online (Sandbox Code Playgroud)
这个问题来自哪里?玩框架?数据库支持?JPA缺少注释?...
谢谢!