How to initialize and always-true BooleanExpression?

6,221 views
Skip to first unread message

Marcel Stör

unread,
May 14, 2012, 3:25:25 PM5/14/12
to quer...@googlegroups.com
We dynamically build expressions based on a number of member variables.
This looks kind of like this:

public BooleanExpression getFilterExpression() {
BooleanExpression expression = <QEntity>.isNotNull();

if (A) {
BooleanBuilder builder = new BooleanBuilder();
<some-operation-on-the-builder>
expression = expression.and(builder);
}
if (B) {
BooleanBuilder builder = new BooleanBuilder();
<more-builder-stuff>
expression = expression.and(builder);
}
if (C) {
expression = expression.and(<another-predicate>);
}
if (D) {
expression = expression.and(<final-predicate>);
}
return expression;
}

After the first line we know for sure 'expression' is not null, that
makes the code more legible.
However, we'd much rather drop the superfluous always-true query
condition on the first line. Is there any other way we could initialize
the BooleanExpression?

Cheers,
Marcel

--
Marcel St�r, http://www.frightanic.com
Couchsurfing: http://www.couchsurfing.com/people/marcelstoer
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

Timo Westkämper

unread,
May 14, 2012, 3:41:11 PM5/14/12
to Querydsl on behalf of Marcel Stör
Hi.

BooleanBuilder is mutable builder for BooleanExpression. Your code looks like you expect it to be immutable.

Your code should look like this

public BooleanExpression getFilterExpression() {

    BooleanBuilder builder = new BooleanBuilder();

    if (A) {
        builder.and(<some expression>);
    }
    if (B) {
        builder.and(<some expression>);
    }
    ...
    return builder;
}

Br,
Timo 




On Mon, May 14, 2012 at 10:25 PM, Querydsl on behalf of Marcel Stör <quer...@googlegroups.com> wrote:
We dynamically build expressions based on a number of member variables. This looks kind of like this:

public BooleanExpression getFilterExpression() {
 BooleanExpression expression = <QEntity>.isNotNull();

 if (A) {
   BooleanBuilder builder = new BooleanBuilder();
   <some-operation-on-the-builder>
   expression = expression.and(builder);
 }
 if (B) {
   BooleanBuilder builder = new BooleanBuilder();
   <more-builder-stuff>
   expression = expression.and(builder);
 }
 if (C) {
   expression = expression.and(<another-predicate>);
 }
 if (D) {
   expression = expression.and(<final-predicate>);
 }
 return expression;
}

After the first line we know for sure 'expression' is not null, that makes the code more legible.
However, we'd much rather drop the superfluous always-true query condition on the first line. Is there any other way we could initialize the BooleanExpression?

Cheers,
Marcel


--
Marcel Stör, http://www.frightanic.com

Couchsurfing: http://www.couchsurfing.com/people/marcelstoer
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org



--
Timo Westkämper
Mysema Oy
+358 (0)40 591 2172
www.mysema.com



Matthew Adams

unread,
May 23, 2012, 9:47:43 AM5/23/12
to quer...@googlegroups.com
I'm doing something similar, but this fails because a BooleanBuilder is not type compatible with BooleanExpression.  The method that you advise Marcel to write won't compile, because it returns a BooleanExpression, and a BooleanBuilder is not type compatible with BooleanExpression.

How do you convert a BooleanBuilder to a BooleanExpression?

Timo Westkämper

unread,
May 23, 2012, 9:56:43 AM5/23/12
to Querydsl on behalf of Matthew Adams
Hi.


On Wed, May 23, 2012 at 4:47 PM, Querydsl on behalf of Matthew Adams <quer...@googlegroups.com> wrote:
I'm doing something similar, but this fails because a BooleanBuilder is not type compatible with BooleanExpression.  

Yes, that's true. I overlooked it. Both implement the Predicate interface and that is the type that should be returned. BooleanExpression provides an immutable DSL for predicate construction and BooleanBuilder provides provides a similar, but mutable DSL.

Conversion is not directly possible and shouldn't be necessary.

Br,
Timo
 

Matthew Adams

unread,
May 23, 2012, 10:01:05 AM5/23/12
to quer...@googlegroups.com
Ok.  Would it be bad design for BooleanBuilder to be changed to offer a toBooleanExpression() method?  Predicate doesn't have methods like and, or, etc.

Timo Westkämper

unread,
May 23, 2012, 10:07:11 AM5/23/12
to Querydsl on behalf of Matthew Adams
We tried to keep the core Expression model independent of the DSL types. Predicate is a core DSL type and BooleanBuilder and BooleanExpression are independent DSL types. I believe that providing direct conversion from BooleanBuilder to BooleanExpression will be misleading, since the signature is similar, but the semantics are different.

Also I don't see many case where BooleanBuilder to BooleanExpression would be useful.

Timo

Matthew Adams

unread,
May 23, 2012, 10:11:16 AM5/23/12
to quer...@googlegroups.com
Ok, no problem.  I defer to your expertise.  :)
Reply all
Reply to author
Forward
0 new messages