javaexpressionevaluate

Library for evaluating logical expressions on Java objects


Let's say I have the following class:

class Person {
    int age;
    String city;
    Collection<Person> friends;
    Person spouse;
}

I need a library which would allow me to evaluate whether a logical expression is true on a given Person object. The expression would look something like this:

((age>25 OR spouse.age>27) AND city=="New-York" AND size(friends)>100)

So, the requirements are:

  1. Ability to use basic logical operators
  2. Access properties of the given object
  3. Access properties of an internal object
  4. Use simple mathematical\sql-like functions such as size,max,sum

Suggestions?


Solution

  • OK, think I found what I wanted, it's a variation on assylias' answer but I prefer it since it's more standardized (woudn't want to depend specifically on Javascript expressions, or run Javascript at all for that matter).

    Apparently there is a Unified Expression Language for evaluating expressions, it was originally designed for JSP but can be used for other stuff as well. There are several parser implementations, I'll probably go either with Spring EL or JUEL