001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.builder;
018
019import java.util.Map;
020import java.util.function.BiPredicate;
021import java.util.function.Predicate;
022
023import org.apache.camel.Exchange;
024import org.apache.camel.Message;
025
026public class PredicateClause<T> implements org.apache.camel.Predicate {
027    private final T parent;
028    private Predicate<Exchange> predicate;
029
030    public PredicateClause(T parent) {
031        this.parent = parent;
032        this.predicate = null;
033    }
034
035    @Override
036    public boolean matches(Exchange exchange) {
037        return (predicate != null) ?  predicate.test(exchange) : false;
038    }
039
040    // *******************************
041    // Exchange
042    // *******************************
043
044    /**
045     * Define a {@link org.apache.camel.Predicate} which targets the Exchange.
046     */
047    public T exchange(final Predicate<Exchange> predicate) {
048        this.predicate = predicate::test;
049        return parent;
050    }
051
052
053    // *******************************
054    // Message
055    // *******************************
056
057    /**
058     * Define a {@link org.apache.camel.Predicate} which targets the Exchange In Message.
059     *     
060     * <blockquote><pre>{@code
061     * from("direct:aggregate")
062     *     .choice()
063     *         .when()
064     *            .message(m -> m.getBody() != null)
065     *            .log("Received ${body}")
066     *     .endChoice()
067     * }</pre></blockquote>
068     */
069    public T message(final Predicate<Message> predicate) {
070        return exchange(e -> predicate.test(e.getIn()));
071    }
072
073    // *******************************
074    // Body
075    // *******************************
076
077
078    /**
079     * Define a {@link org.apache.camel.Predicate} which targets the Exchange In Body.
080     *     
081     * <blockquote><pre>{@code
082     * from("direct:aggregate")
083     *     .choice()
084     *         .when()
085     *            .body(b -> b != null)
086     *            .log("Received ${body}")
087     *     .endChoice()
088     * }</pre></blockquote>
089     */
090    public T body(final Predicate<Object> predicate) {
091        return exchange(e -> predicate.test(e.getIn().getBody()));
092    }
093
094    /**
095     * Define a {@link org.apache.camel.Predicate} which targets the typed Exchange In Body.
096     *     
097     * <blockquote><pre>{@code
098     * from("direct:aggregate")
099     *     .choice()
100     *         .when()
101     *            .body(Long.class, b -> (b & 1) == 0)
102     *            .log("Received even number ${body}")
103     *     .endChoice()
104     * }</pre></blockquote>
105     */
106    public <B> T body(final Class<B> type, final Predicate<B> predicate) {
107        return exchange(e -> predicate.test(e.getIn().getBody(type)));
108    }
109
110    /**
111     * Define a {@link org.apache.camel.Predicate} which targets the Exchange In Body and its Headers.
112     *     
113     * <blockquote><pre>{@code
114     * from("direct:aggregate")
115     *     .choice()
116     *         .when()
117     *            .body((b, h) -> b != null || h.containsKy("ToProcess"))
118     *            .log("Received ${body}")
119     *     .endChoice()
120     * }</pre></blockquote>
121     */
122    public T body(final BiPredicate<Object, Map<String, Object>> predicate) {
123        return exchange(e -> predicate.test(
124            e.getIn().getBody(),
125            e.getIn().getHeaders())
126        );
127    }
128
129    /**
130     * Define a {@link org.apache.camel.Predicate} which targets the typed Exchange In Body and its Headers.
131     *     
132     * <blockquote><pre>{@code
133     * from("direct:aggregate")
134     *     .choice()
135     *         .when()
136     *            .body(String.class, (b, h) -> b != null && !b.isEmpty() || h.containsKy("ToProcess"))
137     *            .log("Received ${body}")
138     *     .endChoice()
139     * }</pre></blockquote>
140     */
141    public <B> T body(final Class<B> type, final BiPredicate<B, Map<String, Object>> predicate) {
142        return exchange(e -> predicate.test(
143            e.getIn().getBody(type),
144            e.getIn().getHeaders())
145        );
146    }
147}