Pra*_*tik 9 java filtering jexl
我知道,我可以在JEXL中做几件事,但无法在其中找到过滤器功能,这确实非常有用.
我该怎么办呢
var x=[{a:11,b=5},{a:1,b=15},{a:12,b=25},{a:4,b=35},{a:7,b=45}];
return x[.a>10].b; // Which filters to {a:11,b=5} & {a:12,b=25}
// & hence returns [5,25]
Run Code Online (Sandbox Code Playgroud)
首先,您的语法不是有效的 JEXL。我想你的意思是这样的:
var x = [{'a':11,'b':5}, {'a':1,'b':15}, {'a':12,'b':25}, {'a':4,'b':35}, {'a':7,'b':45}];
Run Code Online (Sandbox Code Playgroud)
由于您可以在 JEXL 脚本中的任何对象上调用任何 Java 方法,因此您(至少理论上)拥有对 Java Stream API 的完全访问权限。
然而,Stream API 不能直接从原始数组中获得,我们不能Arrays.stream(x);不费吹灰之力就调用。解决这个问题最简单的方法是创建一个集合:
var x = {{'a':11,'b':5}, {'a':1,'b':15}, {'a':12,'b':25}, {'a':4,'b':35}, {'a':7,'b':45}};
Run Code Online (Sandbox Code Playgroud)
现在我们可以简单地stream()从那里调用并工作:
x.stream();
Run Code Online (Sandbox Code Playgroud)
我们现在想要的是这样的:
x.stream().filter(function(m){m['a']>10});
Run Code Online (Sandbox Code Playgroud)
不幸的是,JEXL 中的方法解析器将无法正确解析Stream.filter(Predicate)JEXL 函数,因为它不知道如何将 JEXL 函数转换为Predicate. JEXL 函数的类型为org.apache.commons.jexl3.internal.Closure。
因此,您至少需要做的是Predicate在 Java 中提供您自己的实现,然后在脚本中创建一个新实例:
public class MyCustomFilterPredicate implements Predicate<HashMap<String, Integer>> {
@Override
public boolean test(final HashMap<String, Integer> m)
{
return m.get("a") > 10;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在 JEXL 脚本中创建一个新实例:
var filterPredicate = new('my.custom.filter.predicate.MyCustomFilterPredicate');
Run Code Online (Sandbox Code Playgroud)
这同样适用于Stream.map(Function):
public class MyCustomMapFunction implements Function<HashMap<String, Integer>, Integer> {
@Override
public Integer apply(final HashMap<String, Integer> m)
{
return m.get("b");
}
}
Run Code Online (Sandbox Code Playgroud)
并再次在脚本中创建一个新实例:
var mapFunction = new('my.custom.map.function.MyCustomMapFunction');
Run Code Online (Sandbox Code Playgroud)
你的整个脚本将如下所示:
var x = {{'a':11,'b':5}, {'a':1,'b':15}, {'a':12,'b':25}, {'a':4,'b':35}, {'a':7,'b':45}};
var filterPredicate = new('my.custom.filter.predicate.MyCustomFilterPredicate');
var mapFunction = new('my.custom.map.function.MyCustomMapFunction');
return x.stream().filter(filterPredicate).map(mapFunction).toArray();
Run Code Online (Sandbox Code Playgroud)
当然,您可能已经注意到谓词和函数实现的可重用性相当有限。这就是为什么我建议创建包装 JEXL 闭包的实现:
public class MyCustomFilterPredicate implements Predicate<Object> {
private final Closure closure;
public MyCustomFilterPredicate(final Closure closure) {
this.closure = closure;
}
@Override
public boolean test(final Object o)
{
return (boolean) closure.execute(JexlEngine.getThreadContext(), o);
}
}
public class MyCustomMapFunction implements Function<Object, Object> {
private final Closure closure;
public MyCustomMapFunction(final Closure closure) {
this.closure = closure;
}
@Override
public Object apply(final Object o)
{
return closure.execute(JexlEngine.getThreadContext(), o);
}
}
Run Code Online (Sandbox Code Playgroud)
现在您可以按如下方式更改脚本并以各种方式重用这些 Java 类:
var x = {{'a':11,'b':5}, {'a':1,'b':15}, {'a':12,'b':25}, {'a':4,'b':35}, {'a':7,'b':45}};
var filterPredicate = new('my.custom.filter.predicate.MyCustomFilterPredicate', function(m){m['a']>10});
var mapFunction = new('my.custom.map.function.MyCustomMapFunction', function(m){m['b']});
return x.stream().filter(filterPredicate).map(mapFunction).toArray();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
381 次 |
| 最近记录: |