Coverage Report - org.apache.camel.builder.PredicateBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
PredicateBuilder
44% 
25% 
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.builder;
 18  
 
 19  
 import static org.apache.camel.util.ObjectHelper.compare;
 20  
 import org.apache.camel.Exchange;
 21  
 import org.apache.camel.Expression;
 22  
 import org.apache.camel.Predicate;
 23  
 import org.apache.camel.impl.PredicateSupport;
 24  
 import org.apache.camel.impl.BinaryPredicateSupport;
 25  
 import org.apache.camel.util.ObjectHelper;
 26  
 import static org.apache.camel.util.ObjectHelper.notNull;
 27  
 
 28  
 import java.util.regex.Matcher;
 29  
 import java.util.regex.Pattern;
 30  
 
 31  
 /**
 32  
  * A helper class for working with predicates
 33  
  *
 34  
  * @version $Revision: 520261 $
 35  
  */
 36  0
 public class PredicateBuilder {
 37  
     /**
 38  
      * A helper method to combine multiple predicates by a logical AND
 39  
      */
 40  
     public static <E extends Exchange> Predicate<E> and(final Predicate<E> left, final Predicate<E> right) {
 41  0
         notNull(left, "left");
 42  0
         notNull(right, "right");
 43  0
         return new PredicateSupport<E>() {
 44  
             public boolean matches(E exchange) {
 45  0
                 return left.matches(exchange) && right.matches(exchange);
 46  
             }
 47  
 
 48  
             @Override
 49  0
             public String toString() {
 50  0
                 return "(" + left + ") and (" + right + ")";
 51  
             }
 52  
         };
 53  
     }
 54  
 
 55  
     /**
 56  
      * A helper method to combine multiple predicates by a logical OR
 57  
      */
 58  
     public static <E extends Exchange> Predicate<E> or(final Predicate<E> left, final Predicate<E> right) {
 59  0
         notNull(left, "left");
 60  0
         notNull(right, "right");
 61  0
         return new PredicateSupport<E>() {
 62  
             public boolean matches(E exchange) {
 63  0
                 return left.matches(exchange) || right.matches(exchange);
 64  
             }
 65  
 
 66  
             @Override
 67  0
             public String toString() {
 68  0
                 return "(" + left + ") or (" + right + ")";
 69  
             }
 70  
         };
 71  
     }
 72  
 
 73  
     public static <E extends Exchange> Predicate<E> isEqualTo(final Expression<E> left, final Expression<E> right) {
 74  21
         return new BinaryPredicateSupport<E>(left, right) {
 75  
 
 76  
             protected boolean matches(E exchange, Object leftValue, Object rightValue) {
 77  20
                 return ObjectHelper.equals(leftValue, rightValue);
 78  
             }
 79  
 
 80  21
             protected String getOperationText() {
 81  35
                 return "==";
 82  
             }
 83  
         };
 84  
     }
 85  
 
 86  
     public static <E extends Exchange> Predicate<E> isNotEqualTo(final Expression<E> left, final Expression<E> right) {
 87  0
         return new BinaryPredicateSupport<E>(left, right) {
 88  
 
 89  
             protected boolean matches(E exchange, Object leftValue, Object rightValue) {
 90  0
                 return !ObjectHelper.equals(leftValue, rightValue);
 91  
             }
 92  
 
 93  0
             protected String getOperationText() {
 94  0
                 return "==";
 95  
             }
 96  
         };
 97  
     }
 98  
 
 99  
     public static <E extends Exchange> Predicate<E> isLessThan(final Expression<E> left, final Expression<E> right) {
 100  0
         return new BinaryPredicateSupport<E>(left, right) {
 101  
 
 102  
             protected boolean matches(E exchange, Object leftValue, Object rightValue) {
 103  0
                 return compare(leftValue, rightValue) < 0;
 104  
             }
 105  
 
 106  0
             protected String getOperationText() {
 107  0
                 return "<";
 108  
             }
 109  
         };
 110  
     }
 111  
 
 112  
     public static <E extends Exchange> Predicate<E> isLessThanOrEqualTo(final Expression<E> left, final Expression<E> right) {
 113  0
         return new BinaryPredicateSupport<E>(left, right) {
 114  
 
 115  
             protected boolean matches(E exchange, Object leftValue, Object rightValue) {
 116  0
                 return compare(leftValue, rightValue) <= 0;
 117  
             }
 118  
 
 119  0
             protected String getOperationText() {
 120  0
                 return "<=";
 121  
             }
 122  
         };
 123  
     }
 124  
 
 125  
     public static <E extends Exchange> Predicate<E> isGreaterThan(final Expression<E> left, final Expression<E> right) {
 126  2
         return new BinaryPredicateSupport<E>(left, right) {
 127  
 
 128  
             protected boolean matches(E exchange, Object leftValue, Object rightValue) {
 129  2
                 return compare(leftValue, rightValue) > 0;
 130  
             }
 131  
 
 132  2
             protected String getOperationText() {
 133  7
                 return ">";
 134  
             }
 135  
         };
 136  
     }
 137  
 
 138  
     public static <E extends Exchange> Predicate<E> isGreaterThanOrEqualTo(final Expression<E> left, final Expression<E> right) {
 139  0
         return new BinaryPredicateSupport<E>(left, right) {
 140  
 
 141  
             protected boolean matches(E exchange, Object leftValue, Object rightValue) {
 142  0
                 return compare(leftValue, rightValue) < 0;
 143  
             }
 144  
 
 145  0
             protected String getOperationText() {
 146  0
                 return ">=";
 147  
             }
 148  
         };
 149  
     }
 150  
 
 151  
     public static <E extends Exchange> Predicate<E> contains(final Expression<E> left, final Expression<E> right) {
 152  2
         return new BinaryPredicateSupport<E>(left, right) {
 153  
 
 154  
             protected boolean matches(E exchange, Object leftValue, Object rightValue) {
 155  3
                 return ObjectHelper.contains(leftValue, rightValue);
 156  
             }
 157  
 
 158  2
             protected String getOperationText() {
 159  4
                 return "contains";
 160  
             }
 161  
         };
 162  
     }
 163  
 
 164  
     public static <E extends Exchange> Predicate<E> isNull(final Expression<E> expression) {
 165  0
         return isEqualTo(expression, ExpressionBuilder.<E>constantExpression(null));
 166  
     }
 167  
 
 168  
     public static <E extends Exchange> Predicate<E> isNotNull(final Expression<E> expression) {
 169  0
         return isNotEqualTo(expression, ExpressionBuilder.<E>constantExpression(null));
 170  
     }
 171  
 
 172  
     public static <E extends Exchange> Predicate<E> isInstanceOf(final Expression<E> expression, final Class type) {
 173  1
         notNull(expression, "expression");
 174  1
         notNull(type, "type");
 175  
 
 176  1
         return new PredicateSupport<E>() {
 177  
             public boolean matches(E exchange) {
 178  0
                 Object value = expression.evaluate(exchange);
 179  0
                 return type.isInstance(value);
 180  
             }
 181  
 
 182  
             @Override
 183  
             public String toString() {
 184  3
                 return expression + " instanceof " + type.getName();
 185  
             }
 186  
 
 187  
             @Override
 188  1
             protected String assertionFailureMessage(E exchange) {
 189  0
                 return super.assertionFailureMessage(exchange) + " for <" + expression.evaluate(exchange) + ">";
 190  
             }
 191  
         };
 192  
     }
 193  
 
 194  
 
 195  
     /**
 196  
      * Returns a predicate which is true if the expression matches the given regular expression
 197  
      *
 198  
      * @param expression the expression to evaluate
 199  
      * @param regex the regular expression to match against
 200  
      * @return a new predicate
 201  
      */
 202  
     public static <E extends Exchange> Predicate<E> regex(final Expression<E> expression, final String regex) {
 203  2
         return regex(expression, Pattern.compile(regex));
 204  
     }
 205  
 
 206  
     /**
 207  
      * Returns a predicate which is true if the expression matches the given regular expression
 208  
      *
 209  
      * @param expression the expression to evaluate
 210  
      * @param pattern the regular expression to match against
 211  
      * @return a new predicate
 212  
      */
 213  
     public static <E extends Exchange> Predicate<E> regex(final Expression<E> expression, final Pattern pattern) {
 214  2
         notNull(expression, "expression");
 215  2
         notNull(pattern, "pattern");
 216  
 
 217  2
         return new PredicateSupport<E>() {
 218  
             public boolean matches(E exchange) {
 219  4
                 Object value = expression.evaluate(exchange);
 220  4
                 if (value != null) {
 221  4
                     Matcher matcher = pattern.matcher(value.toString());
 222  4
                     return matcher.matches();
 223  
                 }
 224  0
                 return false;
 225  
             }
 226  
 
 227  
             @Override
 228  
             public String toString() {
 229  5
                 return expression + ".matches(" + pattern + ")";
 230  
             }
 231  
 
 232  
             @Override
 233  6
             protected String assertionFailureMessage(E exchange) {
 234  1
                 return super.assertionFailureMessage(exchange) + " for <" + expression.evaluate(exchange) + ">";
 235  
             }
 236  
 
 237  
         };
 238  
     }
 239  
 
 240  
 }