I was bored during the holiday season this year and accidentally decided to write a simple library for understanding / filtering libraries for Java (I know that there are excellent ones, I just wanted to write this myself, damn it).
For this list:
LinkedList<Person> list = new LinkedList<Person>(); list.add(new Person("Jack", 20)); list.add(new Person("Liz", 58)); list.add(new Person("Bob", 33));
Syntax:
Iterable<Person> filtered = Query.from(list).where( Condition.ensure("Age", Op.GreaterEqual, 21) .and(Condition.ensure("Age", Op.LessEqual, 50));
I know its ugly, but if I use static imports and use shorter method names, it gets pretty short.
The following syntax is the final goal:
Iterable<Person> list2 = Query.from(list).where("x=> x.Age >= 21 & x.Age <= 50");
Obviously, parsing expressions is not my strongest area, I am having problems parsing nested / multiple conventions. Does anyone know of some resources / literature that might prove useful?
I have only individual conditional expressions that are successfully processed from the syntax of the lambda string at the moment: "x=> x.Name == Jack" . My basic Expression structure is pretty solid and can easily handle any amount of nesting, the problem is just parsing the expression from the string.
thanks
Just for beats, here is a little insight into how the structure of an expression behind the scenes can work (obviously, I could specify "op.GreaterEqual", etc ... in the following example, but I wanted to demonstrate how flexible it is for any amount of nesting):
Condition minAge1 = Condition.ensure("Age", Op.Equal, 20); Condition minAge2 = Condition.ensure("Age", Op.Greater, 20); Expression minAge = new Expression(minAge1, Express.Or, minAge2); Expression maxAge = Condition.ensure("Age", Op.Equal, 50).or(Condition.ensure("Age", Op.Less, 50)); Expression ageExpression = new Expression(minAge, Express.And, maxAge); Condition randomException = Condition.ensure("Name", Op.Equal, "Liz"); Expression expressionFinal = new Expression(ageExpression, Express.Or, randomException);