Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java library for defining predicates as SQL-like strings?

Lambdaj (and some other similar libraries) provides a having function which allows me to define predicates like so (example directly from the lambdaj Features page):

List<Person> oldFriends = 
    filter(having(on(Person.class).getAge(), greaterThan(30)), meAndMyFriends);

I'd quite like to be able to define my Java predicate object using a straightforward string syntax, "age > 30" - similar to a SQL where clause - so the filter above becomes something like:

List<Person> oldFriends = 
    filter(having(Person.class, "age > 30"), meAndMyFriends);

Does such a library exist, or can anyone recommend some building blocks for the query parsing part I could use to build one myself? I don't actually mind what kind of predicate (hamcrest, guava etc) it creates.

Off the top of my head I can think of a number of things it would be awesome for it to support: equalities and inequalities, on custom and primitive types, and/or/not, parentheses, LIKE (for Strings), in(...), interpretiation of enum names, properties of properties.

Here's an example of a more complex predicate:

"salesCount > 10 and (country='UK' or city='New York')
and attitude not in (MENACING, RUDE)
and product.name <> 'Widget' "

(This example assumes that the class the predicate is applied to (say a SalesPerson class) has methods getSalesCount(), getCountry(), and getCity(), as well as getAttitude() (which returns an enum). It also has a property getProduct which returns a type with method getName).

Motivation: we have a client-server system that has multiple language APIs (currently Java & C#); I'm looking for a language-agnostic way for a user to specify a predicate that filters on a set of objects the exact contents of which are known only to the server process (which is written in Java).

like image 436
bacar Avatar asked Nov 05 '12 18:11

bacar


People also ask

What is predicate interface in Java?

This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. @FunctionalInterface public interface Predicate<T> Represents a predicate (boolean-valued function) of one argument. This is a functional interface whose functional method is test(Object) .

Can we use like operator in Java?

A LIKE expression determines whether a wildcard pattern matches a string. The path expression must have a string or numeric value. If this value is NULL, the value of the LIKE expression is unknown. The pattern value is a string literal that can contain wildcard characters.

What is Hazelcast predicate?

The Predicates API is a programming interface for querying data in Hazelcast, which is similar to the Java Persistence Query Language (JPQL). Note. The Predicates API will be deprecated in an upcoming release. The recommended way to query data in Hazelcast is SQL.


1 Answers

I would give a look at MVEL. It supports expressions of the like :

(user.name == 'John Doe') && ((x * 2) - 1) > 20

Used it once on a project to express some basic business rules from a client.

like image 90
Pierre-Henri Avatar answered Oct 01 '22 02:10

Pierre-Henri