camel-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "christian ohr (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (CAMEL-7831) create a java8 only demo showing how to use lambda expressions for Predicate / Expression inside the Java DSL (using Message as a typesafe parameter)
Date Tue, 21 Jul 2015 14:49:05 GMT

    [ https://issues.apache.org/jira/browse/CAMEL-7831?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14635183#comment-14635183
] 

christian ohr edited comment on CAMEL-7831 at 7/21/15 2:48 PM:
---------------------------------------------------------------

Predicate and Processor can be implemented with lambdas or function references, but Expressions
cannot. 
Lambdas can only implement an interface fully known at compile-time, but Expression#evaluate
is generic.

As of now, one requires a bridge, like this:

{code}
    private <V> Expression function(Function<Exchange, V> function) {
        return new ExpressionBridge<>(function);
    }

    private class ExpressionBridge<V> implements Expression {

        private final Function<Exchange, V> function;

        public ExpressionBridge(Function<Exchange, V> function) {
            this.function = function;
        }

        @Override
        public <T> T evaluate(Exchange exchange, Class<T> type) {
            return exchange.getContext().getTypeConverter().convertTo(type, function.apply(exchange));
        }
    }
{code}




was (Author: christian.ohr):
Predicate and Processor can be implemented with lambdas or function references, but Expressions
cannot. 
Lambdas can only implement an interface fully known at compile-time, but Expression#evaluate
is generic.



> create a java8 only demo showing how to use lambda expressions for Predicate / Expression
inside the Java DSL (using Message as a typesafe parameter)
> -----------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: CAMEL-7831
>                 URL: https://issues.apache.org/jira/browse/CAMEL-7831
>             Project: Camel
>          Issue Type: New Feature
>          Components: examples
>            Reporter: james strachan
>             Fix For: 2.16.0
>
>
> e.g. so folks can type things like
> {code}
> from("seda:cheese").
>   filter(m -> m.getHeader("foo", int.class) > 5).
>   map(m -> createPurchaseOrder(m.getBody(Invoice.class)).
>   to("seda:bar");
> {code}
> Note if the current Predicate/Expression classes don't work as lambdas we could add a
new interface with a single method to the DSL to ensure Java 8 does the right thing with lambdas



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message