You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Trevv <45...@safe-mail.net> on 2009/02/17 05:01:20 UTC

How to negate a Predicate?

Most of my uses of .when are like the first snippet below, very easy to read.

But I have a few uses of .when which need to negate the Predicate, and in
those places I'm using code like the second snippet below.  It works well,
and there are no compiler warnings, but it is NOT easy to read.  How should
I make this code easier to read?

    // take the first branch, if the regex DOES match...

    choice().
      when(header("foo").
             regex("cheese|whey"))

    ========

    // take the first branch, if the regex DOES NOT match...

    choice().
      when(PredicateBuilder.
             not(Builder.
                   <MinaExchange>header("foo").
                     regex("cheese|whey")))
-- 
View this message in context: http://www.nabble.com/How-to-negate-a-Predicate--tp22049751s22882p22049751.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: How to negate a Predicate?

Posted by Hadrian Zbarcea <hz...@gmail.com>.
Please note that the generics will stay as is on the camel-1.x branch,  
so upgrading to 1.6.0 will not help with that.

Hadrian

On Feb 17, 2009, at 11:55 PM, Claus Ibsen wrote:

> Hi
>
> All that generics fuss is fixed in Camel 2.0 as part of our API  
> cleanup.
>
> Glad its working for you and the route is easier to read.
>
>
> On Wed, Feb 18, 2009 at 2:54 AM, Trevv <45...@safe-mail.net> wrote:
>>
>> Thanks Claus, that's a good idea.  Using "import static" makes the  
>> usage of
>> PredicateBuilder.not much easier to read.
>>
>> When I use your code as-is, I get these warnings:
>>
>>
>> Type safety: Unchecked invocation not(Predicate) of the generic  
>> method
>> not(Predicate<E>) of type PredicateBuilder
>>>
>>> Type safety: The expression of type Predicate needs unchecked  
>>> conversion
>>> to conform to Predicate<E>
>>
>> in Camel 1.5.0.  It's too late in my release cycle to upgrade to  
>> Camel 1.6.0
>> now.  But that's OK; I can avoid those warnings by using  
>> Builder.header
>> instead of BuilderSupport.header.
>>
>> My code now looks like this:
>>
>>   choice().
>>     when(not(Builder.
>>                header("foo").
>>                  regex("cheese|whey")))
>>
>> which is much easier to read than before.
>> --
>> View this message in context: http://www.nabble.com/How-to-negate-a-Predicate--tp22049751s22882p22070670.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>>
>
>
>
> -- 
> Claus Ibsen
> Apache Camel Committer
>
> Open Source Integration: http://fusesource.com
> Blog: http://davsclaus.blogspot.com/


Re: How to negate a Predicate?

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

All that generics fuss is fixed in Camel 2.0 as part of our API cleanup.

Glad its working for you and the route is easier to read.


On Wed, Feb 18, 2009 at 2:54 AM, Trevv <45...@safe-mail.net> wrote:
>
> Thanks Claus, that's a good idea.  Using "import static" makes the usage of
> PredicateBuilder.not much easier to read.
>
> When I use your code as-is, I get these warnings:
>
>
> Type safety: Unchecked invocation not(Predicate) of the generic method
> not(Predicate<E>) of type PredicateBuilder
>>
>> Type safety: The expression of type Predicate needs unchecked conversion
>> to conform to Predicate<E>
>
> in Camel 1.5.0.  It's too late in my release cycle to upgrade to Camel 1.6.0
> now.  But that's OK; I can avoid those warnings by using Builder.header
> instead of BuilderSupport.header.
>
> My code now looks like this:
>
>    choice().
>      when(not(Builder.
>                 header("foo").
>                   regex("cheese|whey")))
>
> which is much easier to read than before.
> --
> View this message in context: http://www.nabble.com/How-to-negate-a-Predicate--tp22049751s22882p22070670.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>



-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/

Re: How to negate a Predicate?

Posted by Trevv <45...@safe-mail.net>.
Thanks Claus, that's a good idea.  Using "import static" makes the usage of
PredicateBuilder.not much easier to read.

When I use your code as-is, I get these warnings:


Type safety: Unchecked invocation not(Predicate) of the generic method
not(Predicate<E>) of type PredicateBuilder
> 
> Type safety: The expression of type Predicate needs unchecked conversion
> to conform to Predicate<E>

in Camel 1.5.0.  It's too late in my release cycle to upgrade to Camel 1.6.0
now.  But that's OK; I can avoid those warnings by using Builder.header
instead of BuilderSupport.header.

My code now looks like this:

    choice().
      when(not(Builder.
                 header("foo").
                   regex("cheese|whey")))

which is much easier to read than before.
-- 
View this message in context: http://www.nabble.com/How-to-negate-a-Predicate--tp22049751s22882p22070670.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: How to negate a Predicate?

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

I will add a sample to the predicate wiki page
http://camel.apache.org/predicate.html

But there is a hikup with the apache server right now, so here is part
of the source code for an unit test:

import static org.apache.camel.builder.PredicateBuilder.not;

...

    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
                // START SNIPPET: e1
                from("direct:start")
                    .choice()

.when(not(header("username").regex("goofy|pluto"))).to("mock:people")
                        .otherwise().to("mock:animals")
                    .end();
                // END SNIPPET: e1
            }
        };
    }


On Tue, Feb 17, 2009 at 5:01 AM, Trevv <45...@safe-mail.net> wrote:
>
> Most of my uses of .when are like the first snippet below, very easy to read.
>
> But I have a few uses of .when which need to negate the Predicate, and in
> those places I'm using code like the second snippet below.  It works well,
> and there are no compiler warnings, but it is NOT easy to read.  How should
> I make this code easier to read?
>
>    // take the first branch, if the regex DOES match...
>
>    choice().
>      when(header("foo").
>             regex("cheese|whey"))
>
>    ========
>
>    // take the first branch, if the regex DOES NOT match...
>
>    choice().
>      when(PredicateBuilder.
>             not(Builder.
>                   <MinaExchange>header("foo").
>                     regex("cheese|whey")))
> --
> View this message in context: http://www.nabble.com/How-to-negate-a-Predicate--tp22049751s22882p22049751.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>



-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/