简体   繁体   English

使用Java lambda的外部可配置过滤

[英]Externally configurable filtering using Java lambdas

Lets say I have an external JSON as: 假设我有一个外部JSON:

[{
    "condition": "equals",
    "field": "name",
    "value": "jack"
  },
  {
    "condition": "greater",
    "field": "age",
    "value": 25
  }
]

What this means is I want to check, in a list of Person objects for people with name as "jack" and age > 25 . 这意味着我想在名为"jack"age > 25人的Person对象列表中进行检查。 It is fairly simple with Java 8 filtering(example shown is filtering just on name). 使用Java 8过滤相当简单(显示的示例仅在名称上过滤)。

However, I want to make this filter configurable and also apply multiple filters. 但是,我想使此过滤器可配置,并且还应用多个过滤器。 Assuming the Person pojo below (which is self-explanatory), takes name and age, how can I make the filters dynamic or configurable? 假设下面的Person pojo(这是不言自明的),取名字和年龄,我怎样才能使过滤器动态或可配置?

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Accessors and Mutators
}
List<Person> persons = Arrays.asList(
    new Person("mkyong", 30),
    new Person("jack", 20),
    new Person("lawrence", 40)
);

Person result1 = persons.stream()            // Convert to steam
    .filter(x -> "jack".equals(x.getName())) // we want "jack" only
    .findAny()                               // If 'findAny' then return found
    .orElse(null);  

I am expecting a list of Person object which satisfy the criterion as a result. 我期待一个满足条件的Person对象列表。

That has little to do with streams per-se as far as I can tell. 据我所知,这与streams本身没什么关系。 That filter method is just a Predicate which can be supplied as an instance, for example from a factory method that you could create. 该过滤方法只是一个Predicate ,可以作为实例提供,例如从您可以创建的工厂方法。

static Predicate<Person> fromName(String value){
    return p -> p.getName().equals(value);
}

Suppose you have another method: 假设你有另一种方法:

static Predicate<Person> fromAge(int value) {
    return p -> p.getAge() == value;
}

Because Predicate.and , Predicate.or exists you could do: 因为Predicate.andPredicate.or存在,你可以这样做:

 fromAge(12).and(fromName("jack"));

thus chaining your predicates 从而链接你的谓词

There are two approaches to this. 这有两种方法。

Either you just chain call the desired filters inline: 您可以直接串联调用所需的过滤器:

Person result1 = persons.stream()
    .filter(x -> "jack".equals(x.getName()))
    .filter(x -> 25 > x.getAge()) // new filter
    .findAny()
    .orElse(null);

Or, you have your own class implementing Predicate<Person> . 或者,您有自己的类实现Predicate<Person>

In which case, you may use a builder pattern to build your predicate step-by-step. 在这种情况下,您可以使用构建器模式逐步构建谓词。

The difficulties you'll have to face are as follows: 您将面临的困难如下:

  • Handling boolean relation between terms ( && vs || ) 处理术语之间的布尔关系( && vs ||
  • Handling nested boolean relations ( (a && b) || c is not a && (b || c) ) 处理嵌套的布尔关系( (a && b) || c不是a && (b || c)

Note that Predicate has handy and and or methods to chain predicates. 请注意, Predicate具有链接谓词的方便and / or方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM