Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 570785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:26:19+00:00 2026-05-13T13:26:19+00:00

I got bored during the holiday season this year and randomly decided to write

  • 0

I got bored during the holiday season this year and randomly decided to write a simple list comprehension/filtering library for Java (I know there are some great ones out there, I just wanted to write it my self for the hell of 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 is:

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 becomes pretty concise.

The following syntax is the ultimate goal:

Iterable<Person> list2 = Query.from(list).where("x=> x.Age >= 21 & x.Age <= 50");

Apparently expression parsing is not my strongest area, im having trouble with parsing nested/multiple conditionals. Anyone know of some resources/literature I might find helpful?

I’ve only got single conditional expressions being sucessfully parsed from String lambda syntax at the moment: "x=> x.Name == Jack". My underlying Expression structure is fairly solid and can easily handle any amount of nesting, the issue is just the expression parsing from a string.

Thanks

Just for kicks, here is a little insight as to how the expression structure behind the scenes can work (obviously I could have specified ‘op.GreaterEqual’, etc… in the following example, but I wanted to demonstrate how it is flexible to 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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:26:20+00:00Added an answer on May 13, 2026 at 1:26 pm

    Basically what you’ll want is a recursive descent parser for expressions. It’s a topic featured heavily in compiler theory so any book on compilers will cover the topic. In formal grammar terms it will look something like this:

    condition  : orAtom ('||' orAtom)+ ;
    orAtom     : atom ('&&' atom)+ ;
    atom       : '(' condition ')'
               | expression ;
    expression : value OPER value ;
    value      : VARIABLE | LITERAL '
    VARIABLE   : (LETTER | '_') (LETTER | DIGIT | '_')* ;
    LITERAL    : NUMBER
               | STRING ;
    NUMBER     : '-'? DIGIT+ ('.' DIGIT+)? ;
    STRING     : '"' . CHAR* . '"' '
    CHAR       : ('\\' | '\"' | .) + ;
    LETTER     : 'a'..'z' | 'A'..'Z' ;
    DIGIT      : '0'..'9' ;
    OPER       : '>' | '>=' | '<' | '<=' | '=' | '!=' ;
    

    The grammar above is (mostly) in ANTLR form as that what I’m most familiar with.

    Parsing boolean or arithmetic expressions is a classic introductory topic when dealing with parsing so you should be able to find plenty of literature on it. If you want to pursue ANTLR (since you’re using Java) I’d highly suggest reading The Definitive ANTLR Reference: Building Domain-Specific Languages.

    If all this looks like overkill and all a bit much to take in, you might be right. It’s a tough topic to get started in.

    One alternative you have is not to create an arbitrary string expression but instead use a fluent interface (like you’re doing):

    List results = from(source)
      .where(var("x").greaterThan(25), var("x").lessThan(50))
      .select("field1", "field2");
    

    as that is stating the expression tree in code and should be easier to implement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got bored earlier and wondered if you could execute terminal commands on the
Got this error message while trying to load view: The model item passed into
got a simple web site, in html. I do a simple function to move
Got a question for some jQuery wizard who might stumble upon this. When I
got a problem, can't get my head around this jquery and would appreciate your
I recently got bored and fired up my old Mac OS Classic emulator, and
I've got some data I'd like to pull off our SQL server. This old
This has got me really flumoxed! In the datalayer ADO.NET connects to SQL Server
Got the following code: $(document).ready(function() { $('a.add-item').click(function() { if ($(this).parent().find('input').attr('value', '0')) { $(this).parent().find('input').attr('value', '1')
Got it from php.net, but I am not sure is this how everybody destroy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.