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.builder;
018    
019    import org.apache.camel.BinaryPredicate;
020    import org.apache.camel.Exchange;
021    import org.apache.camel.Expression;
022    import org.apache.camel.Predicate;
023    
024    import static org.apache.camel.util.ObjectHelper.notNull;
025    
026    /**
027     * A useful base class for {@link Predicate} implementations
028     *
029     * @version $Revision: 1003810 $
030     */
031    public abstract class BinaryPredicateSupport implements BinaryPredicate {
032    
033        private final Expression left;
034        private final Expression right;
035        private Object lastLeftValue;
036        private Object lastRightValue;
037    
038        protected BinaryPredicateSupport(Expression left, Expression right) {
039            notNull(left, "left");
040            notNull(right, "right");
041    
042            this.left = left;
043            this.right = right;
044        }
045    
046        @Override
047        public String toString() {
048            return left + " " + getOperationText() + " " + right;
049        }
050    
051        public boolean matches(Exchange exchange) {
052            // must be thread safe and store result in local objects
053            Object leftValue = left.evaluate(exchange, Object.class);
054            Object rightValue = right.evaluate(exchange, Object.class);
055            // remember last result (may not be thread safe)
056            lastRightValue = rightValue;
057            lastLeftValue = leftValue;
058            return matches(exchange, leftValue, rightValue);
059        }
060    
061        protected abstract boolean matches(Exchange exchange, Object leftValue, Object rightValue);
062    
063        protected abstract String getOperationText();
064    
065        public Expression getRight() {
066            return right;
067        }
068    
069        public Expression getLeft() {
070            return left;
071        }
072    
073        public String getOperator() {
074            return getOperationText();
075        }
076    
077        public Object getRightValue() {
078            return lastRightValue;
079        }
080    
081        public Object getLeftValue() {
082            return lastLeftValue;
083        }
084    }