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;
018    
019    import org.apache.camel.util.ObjectHelper;
020    import static org.apache.camel.util.ObjectHelper.notNull;
021    
022    /**
023     * A helper class for working with predicates
024     *
025     * @version $Revision: 520261 $
026     */
027    public class Predicates {
028        /**
029         * A helper method to combine multiple predicates by a logical AND
030         */
031        public static <E extends Exchange> Predicate<E> and(final Predicate<E> left, final Predicate<E> right) {
032            notNull(left, "left");
033            notNull(right, "right");
034            return new Predicate<E>() {
035                public boolean evaluate(E exchange) {
036                    return left.evaluate(exchange) && right.evaluate(exchange);
037                }
038            };
039        }
040    
041        /**
042         * A helper method to combine multiple predicates by a logical OR
043         */
044        public static <E extends Exchange> Predicate<E> or(final Predicate<E> left, final Predicate<E> right) {
045            notNull(left, "left");
046            notNull(right, "right");
047            return new Predicate<E>() {
048                public boolean evaluate(E exchange) {
049                    return left.evaluate(exchange) || right.evaluate(exchange);
050                }
051            };
052        }
053    
054    
055        public static <E extends Exchange> Predicate<E> isEqualTo(final Expression<E> left, final Expression<E> right) {
056            notNull(left, "left");
057            notNull(right, "right");
058    
059            return new Predicate<E>() {
060                public boolean evaluate(E exchange) {
061                    Object value1 = left.evaluate(exchange);
062                    Object value2 = right.evaluate(exchange);
063                    return ObjectHelper.equals(value1, value2);
064                }
065            };
066        }
067    
068        public static <E extends Exchange> Predicate<E> isNotEqualTo(final Expression<E> left, final Expression<E> right) {
069            notNull(left, "left");
070            notNull(right, "right");
071    
072            return new Predicate<E>() {
073                public boolean evaluate(E exchange) {
074                    Object value1 = left.evaluate(exchange);
075                    Object value2 = right.evaluate(exchange);
076                    return !ObjectHelper.equals(value1, value2);
077                }
078            };
079        }
080    
081        public static <E extends Exchange> Predicate<E> isLessThan(final Expression<E> left, final Expression<E> right) {
082            notNull(left, "left");
083            notNull(right, "right");
084    
085            return new Predicate<E>() {
086                public boolean evaluate(E exchange) {
087                    Object value1 = left.evaluate(exchange);
088                    Object value2 = right.evaluate(exchange);
089                    return ObjectHelper.compare(value1, value2) < 0;
090                }
091            };
092        }
093    
094        public static <E extends Exchange> Predicate<E> isLessThanOrEqualTo(final Expression<E> left, final Expression<E> right) {
095            notNull(left, "left");
096            notNull(right, "right");
097    
098            return new Predicate<E>() {
099                public boolean evaluate(E exchange) {
100                    Object value1 = left.evaluate(exchange);
101                    Object value2 = right.evaluate(exchange);
102                    return ObjectHelper.compare(value1, value2) <= 0;
103                }
104            };
105        }
106    
107        public static <E extends Exchange> Predicate<E> isGreaterThan(final Expression<E> left, final Expression<E> right) {
108            notNull(left, "left");
109            notNull(right, "right");
110    
111            return new Predicate<E>() {
112                public boolean evaluate(E exchange) {
113                    Object value1 = left.evaluate(exchange);
114                    Object value2 = right.evaluate(exchange);
115                    return ObjectHelper.compare(value1, value2) > 0;
116                }
117            };
118        }
119    
120        public static <E extends Exchange> Predicate<E> isGreaterThanOrEqualTo(final Expression<E> left, final Expression<E> right) {
121            notNull(left, "left");
122            notNull(right, "right");
123    
124            return new Predicate<E>() {
125                public boolean evaluate(E exchange) {
126                    Object value1 = left.evaluate(exchange);
127                    Object value2 = right.evaluate(exchange);
128                    return ObjectHelper.compare(value1, value2) >= 0;
129                }
130            };
131        }
132    }