Coverage Report - org.apache.camel.builder.PredicateBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
PredicateBuilder
50% 
45% 
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 java.util.regex.Matcher;
 20  
 import java.util.regex.Pattern;
 21  
 
 22  
 import org.apache.camel.Exchange;
 23  
 import org.apache.camel.Expression;
 24  
 import org.apache.camel.Predicate;
 25  
 import org.apache.camel.impl.BinaryPredicateSupport;
 26  
 import org.apache.camel.impl.PredicateSupport;
 27  
 import org.apache.camel.util.ObjectHelper;
 28  
 
 29  
 import static org.apache.camel.util.ObjectHelper.compare;
 30  
 import static org.apache.camel.util.ObjectHelper.notNull;
 31  
 
 32  
 /**
 33  
  * A helper class for working with predicates
 34  
  * 
 35  
  * @version $Revision: 520261 $
 36  
  */
 37  
 public class PredicateBuilder {
 38  
 
 39  
     /**
 40  
      * Utility classes should not have a public constructor.
 41  
      */
 42  0
     private PredicateBuilder() {        
 43  0
     }
 44  
 
 45  
     /**
 46  
      * Converts the given expression into an {@link Predicate}
 47  
      */
 48  
     public static <E extends Exchange> Predicate<E> toPredicate(final Expression<E> expression) {
 49  9
         return new PredicateSupport<E>() {
 50  
             public boolean matches(E exchange) {
 51  15
                 Object value = expression.evaluate(exchange);
 52  15
                 return evaluateValuePredicate(value);
 53  
             }
 54  
 
 55  
             @Override
 56  24
             public String toString() {
 57  18
                 return expression.toString();
 58  
             }
 59  
         };
 60  
     }
 61  
 
 62  
     /**
 63  
      * Evaluate the value as a predicate which attempts to convert the value to
 64  
      * a boolean otherwise true is returned if the value is not null
 65  
      */
 66  
     public static boolean evaluateValuePredicate(Object value) {
 67  15
         if (value instanceof Boolean) {
 68  0
             Boolean aBoolean = (Boolean)value;
 69  0
             return aBoolean.booleanValue();
 70  
         }
 71  15
         return value != null;
 72  
     }
 73  
 
 74  
     /**
 75  
      * A helper method to return the logical not of the given predicate
 76  
      */
 77  
     public static <E extends Exchange> Predicate<E> not(final Predicate<E> predicate) {
 78  12
         notNull(predicate, "predicate");
 79  12
         return new PredicateSupport<E>() {
 80  
             public boolean matches(E exchange) {
 81  12
                 return !predicate.matches(exchange);
 82  
             }
 83  
 
 84  
             @Override
 85  24
             public String toString() {
 86  0
                 return "not " + predicate;
 87  
             }
 88  
         };
 89  
     }
 90  
 
 91  
     /**
 92  
      * A helper method to combine multiple predicates by a logical AND
 93  
      */
 94  
     public static <E extends Exchange> Predicate<E> and(final Predicate<E> left, final Predicate<E> right) {
 95  0
         notNull(left, "left");
 96  0
         notNull(right, "right");
 97  0
         return new PredicateSupport<E>() {
 98  
             public boolean matches(E exchange) {
 99  0
                 return left.matches(exchange) && right.matches(exchange);
 100  
             }
 101  
 
 102  
             @Override
 103  0
             public String toString() {
 104  0
                 return "(" + left + ") and (" + right + ")";
 105  
             }
 106  
         };
 107  
     }
 108  
 
 109  
     /**
 110  
      * A helper method to combine multiple predicates by a logical OR
 111  
      */
 112  
     public static <E extends Exchange> Predicate<E> or(final Predicate<E> left, final Predicate<E> right) {
 113  0
         notNull(left, "left");
 114  0
         notNull(right, "right");
 115  0
         return new PredicateSupport<E>() {
 116  
             public boolean matches(E exchange) {
 117  0
                 return left.matches(exchange) || right.matches(exchange);
 118  
             }
 119  
 
 120  
             @Override
 121  0
             public String toString() {
 122  0
                 return "(" + left + ") or (" + right + ")";
 123  
             }
 124  
         };
 125  
     }
 126  
 
 127  
     public static <E extends Exchange> Predicate<E> isEqualTo(final Expression<E> left,
 128  
                                                               final Expression<E> right) {
 129  87
         return new BinaryPredicateSupport<E>(left, right) {
 130  
 
 131  
             protected boolean matches(E exchange, Object leftValue, Object rightValue) {
 132  84
                 return ObjectHelper.equals(leftValue, rightValue);
 133  
             }
 134  
 
 135  87
             protected String getOperationText() {
 136  90
                 return "==";
 137  
             }
 138  
         };
 139  
     }
 140  
 
 141  
     public static <E extends Exchange> Predicate<E> isNotEqualTo(final Expression<E> left,
 142  
                                                                  final Expression<E> right) {
 143  0
         return new BinaryPredicateSupport<E>(left, right) {
 144  
 
 145  
             protected boolean matches(E exchange, Object leftValue, Object rightValue) {
 146  0
                 return !ObjectHelper.equals(leftValue, rightValue);
 147  
             }
 148  
 
 149  0
             protected String getOperationText() {
 150  0
                 return "==";
 151  
             }
 152  
         };
 153  
     }
 154  
 
 155  
     public static <E extends Exchange> Predicate<E> isLessThan(final Expression<E> left,
 156  
                                                                final Expression<E> right) {
 157  0
         return new BinaryPredicateSupport<E>(left, right) {
 158  
 
 159  
             protected boolean matches(E exchange, Object leftValue, Object rightValue) {
 160  0
                 return compare(leftValue, rightValue) < 0;
 161  
             }
 162  
 
 163  0
             protected String getOperationText() {
 164  0
                 return "<";
 165  
             }
 166  
         };
 167  
     }
 168  
 
 169  
     public static <E extends Exchange> Predicate<E> isLessThanOrEqualTo(final Expression<E> left,
 170  
                                                                         final Expression<E> right) {
 171  0
         return new BinaryPredicateSupport<E>(left, right) {
 172  
 
 173  
             protected boolean matches(E exchange, Object leftValue, Object rightValue) {
 174  0
                 return compare(leftValue, rightValue) <= 0;
 175  
             }
 176  
 
 177  0
             protected String getOperationText() {
 178  0
                 return "<=";
 179  
             }
 180  
         };
 181  
     }
 182  
 
 183  
     public static <E extends Exchange> Predicate<E> isGreaterThan(final Expression<E> left,
 184  
                                                                   final Expression<E> right) {
 185  6
         return new BinaryPredicateSupport<E>(left, right) {
 186  
 
 187  
             protected boolean matches(E exchange, Object leftValue, Object rightValue) {
 188  6
                 return compare(leftValue, rightValue) > 0;
 189  
             }
 190  
 
 191  6
             protected String getOperationText() {
 192  18
                 return ">";
 193  
             }
 194  
         };
 195  
     }
 196  
 
 197  
     public static <E extends Exchange> Predicate<E> isGreaterThanOrEqualTo(final Expression<E> left,
 198  
                                                                            final Expression<E> right) {
 199  0
         return new BinaryPredicateSupport<E>(left, right) {
 200  
 
 201  
             protected boolean matches(E exchange, Object leftValue, Object rightValue) {
 202  0
                 return compare(leftValue, rightValue) < 0;
 203  
             }
 204  
 
 205  0
             protected String getOperationText() {
 206  0
                 return ">=";
 207  
             }
 208  
         };
 209  
     }
 210  
 
 211  
     public static <E extends Exchange> Predicate<E> contains(final Expression<E> left,
 212  
                                                              final Expression<E> right) {
 213  6
         return new BinaryPredicateSupport<E>(left, right) {
 214  
 
 215  
             protected boolean matches(E exchange, Object leftValue, Object rightValue) {
 216  9
                 return ObjectHelper.contains(leftValue, rightValue);
 217  
             }
 218  
 
 219  6
             protected String getOperationText() {
 220  12
                 return "contains";
 221  
             }
 222  
         };
 223  
     }
 224  
 
 225  
     public static <E extends Exchange> Predicate<E> isNull(final Expression<E> expression) {
 226  0
         return isEqualTo(expression, ExpressionBuilder.<E> constantExpression(null));
 227  
     }
 228  
 
 229  
     public static <E extends Exchange> Predicate<E> isNotNull(final Expression<E> expression) {
 230  0
         return isNotEqualTo(expression, ExpressionBuilder.<E> constantExpression(null));
 231  
     }
 232  
 
 233  
     public static <E extends Exchange> Predicate<E> isInstanceOf(final Expression<E> expression,
 234  
                                                                  final Class type) {
 235  3
         notNull(expression, "expression");
 236  3
         notNull(type, "type");
 237  
 
 238  3
         return new PredicateSupport<E>() {
 239  
             public boolean matches(E exchange) {
 240  0
                 Object value = expression.evaluate(exchange);
 241  0
                 return type.isInstance(value);
 242  
             }
 243  
 
 244  
             @Override
 245  
             public String toString() {
 246  9
                 return expression + " instanceof " + type.getName();
 247  
             }
 248  
 
 249  
             @Override
 250  3
             protected String assertionFailureMessage(E exchange) {
 251  0
                 return super.assertionFailureMessage(exchange) + " for <" + expression.evaluate(exchange)
 252  
                        + ">";
 253  
             }
 254  
         };
 255  
     }
 256  
 
 257  
     /**
 258  
      * Returns a predicate which is true if the expression matches the given
 259  
      * regular expression
 260  
      * 
 261  
      * @param expression the expression to evaluate
 262  
      * @param regex the regular expression to match against
 263  
      * @return a new predicate
 264  
      */
 265  
     public static <E extends Exchange> Predicate<E> regex(final Expression<E> expression, final String regex) {
 266  6
         return regex(expression, Pattern.compile(regex));
 267  
     }
 268  
 
 269  
     /**
 270  
      * Returns a predicate which is true if the expression matches the given
 271  
      * regular expression
 272  
      * 
 273  
      * @param expression the expression to evaluate
 274  
      * @param pattern the regular expression to match against
 275  
      * @return a new predicate
 276  
      */
 277  
     public static <E extends Exchange> Predicate<E> regex(final Expression<E> expression,
 278  
                                                           final Pattern pattern) {
 279  6
         notNull(expression, "expression");
 280  6
         notNull(pattern, "pattern");
 281  
 
 282  6
         return new PredicateSupport<E>() {
 283  
             public boolean matches(E exchange) {
 284  12
                 Object value = expression.evaluate(exchange);
 285  12
                 if (value != null) {
 286  12
                     Matcher matcher = pattern.matcher(value.toString());
 287  12
                     return matcher.matches();
 288  
                 }
 289  0
                 return false;
 290  
             }
 291  
 
 292  
             @Override
 293  
             public String toString() {
 294  15
                 return expression + ".matches(" + pattern + ")";
 295  
             }
 296  
 
 297  
             @Override
 298  18
             protected String assertionFailureMessage(E exchange) {
 299  3
                 return super.assertionFailureMessage(exchange) + " for <" + expression.evaluate(exchange)
 300  
                        + ">";
 301  
             }
 302  
 
 303  
         };
 304  
     }
 305  
 }