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     */
017    package org.apache.camel.impl;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Expression;
021    import org.apache.camel.Predicate;
022    import static org.apache.camel.util.ObjectHelper.notNull;
023    
024    /**
025     * A useful base class for {@link Predicate} implementations
026     * 
027     * @version $Revision: 35332 $
028     */
029    public abstract class BinaryPredicateSupport<E extends Exchange> implements Predicate<E> {
030    
031        private final Expression<E> left;
032        private final Expression<E> right;
033    
034        protected BinaryPredicateSupport(Expression<E> left, Expression<E> right) {
035            notNull(left, "left");
036            notNull(right, "right");
037    
038            this.left = left;
039            this.right = right;
040        }
041    
042        @Override
043        public String toString() {
044            return left + " " + getOperationText() + " " + right;
045        }
046    
047        public boolean matches(E exchange) {
048            Object leftValue = left.evaluate(exchange);
049            Object rightValue = right.evaluate(exchange);
050            return matches(exchange, leftValue, rightValue);
051        }
052    
053        public void assertMatches(String text, E exchange) {
054            Object leftValue = left.evaluate(exchange);
055            Object rightValue = right.evaluate(exchange);
056            if (!matches(exchange, leftValue, rightValue)) {
057                throw new AssertionError(text + assertionFailureMessage(exchange, leftValue, rightValue));
058            }
059        }
060    
061        protected abstract boolean matches(E exchange, Object leftValue, Object rightValue);
062    
063        protected abstract String getOperationText();
064    
065        protected String assertionFailureMessage(E exchange, Object leftValue, Object rightValue) {
066            return this + " failed on " + exchange + " with left value <" + leftValue + "> right value <"
067                   + rightValue + ">";
068        }
069    }