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     * @deprecated is a duplicate of the same class in the builder package. Will be removed in Camel 2.0
028     * @version $Revision: 65236 $
029     */
030    public abstract class BinaryPredicateSupport<E extends Exchange> implements Predicate<E> {
031    
032        private final Expression<E> left;
033        private final Expression<E> right;
034    
035        protected BinaryPredicateSupport(Expression<E> left, Expression<E> right) {
036            notNull(left, "left");
037            notNull(right, "right");
038    
039            this.left = left;
040            this.right = right;
041        }
042    
043        @Override
044        public String toString() {
045            return left + " " + getOperationText() + " " + right;
046        }
047    
048        public boolean matches(E exchange) {
049            Object leftValue = left.evaluate(exchange);
050            Object rightValue = right.evaluate(exchange);
051            return matches(exchange, leftValue, rightValue);
052        }
053    
054        public void assertMatches(String text, E exchange) {
055            Object leftValue = left.evaluate(exchange);
056            Object rightValue = right.evaluate(exchange);
057            if (!matches(exchange, leftValue, rightValue)) {
058                throw new AssertionError(text + assertionFailureMessage(exchange, leftValue, rightValue));
059            }
060        }
061    
062        protected abstract boolean matches(E exchange, Object leftValue, Object rightValue);
063    
064        protected abstract String getOperationText();
065    
066        protected String assertionFailureMessage(E exchange, Object leftValue, Object rightValue) {
067            return this + " failed on " + exchange + " with left value <" + leftValue + "> right value <"
068                   + rightValue + ">";
069        }
070    }