Coverage Report - org.apache.camel.builder.ValueBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ValueBuilder
48% 
100% 
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 org.apache.camel.Exchange;
 20  
 import org.apache.camel.Expression;
 21  
 import org.apache.camel.Predicate;
 22  
 
 23  
 /**
 24  
  * A builder of expressions or predicates based on values.
 25  
  * 
 26  
  * @version $Revision: $
 27  
  */
 28  
 public class ValueBuilder<E extends Exchange> implements Expression<E> {
 29  
     private Expression<E> expression;
 30  
 
 31  183
     public ValueBuilder(Expression<E> expression) {
 32  183
         this.expression = expression;
 33  183
     }
 34  
 
 35  
     public Object evaluate(E exchange) {
 36  516
         return expression.evaluate(exchange);
 37  
     }
 38  
 
 39  
     public Expression<E> getExpression() {
 40  0
         return expression;
 41  
     }
 42  
 
 43  
     @Override
 44  
     public String toString() {
 45  78
         return expression.toString();
 46  
     }
 47  
 
 48  
     // Predicate builders
 49  
     // -------------------------------------------------------------------------
 50  
 
 51  
     public Predicate<E> isNotEqualTo(Object value) {
 52  0
         Expression<E> right = asExpression(value);
 53  0
         return onNewPredicate(PredicateBuilder.isNotEqualTo(expression, right));
 54  
     }
 55  
 
 56  
     public Predicate<E> isEqualTo(Object value) {
 57  87
         Expression<E> right = asExpression(value);
 58  87
         return onNewPredicate(PredicateBuilder.isEqualTo(expression, right));
 59  
     }
 60  
 
 61  
     public Predicate<E> isLessThan(Object value) {
 62  0
         Expression<E> right = asExpression(value);
 63  0
         return onNewPredicate(PredicateBuilder.isLessThan(expression, right));
 64  
     }
 65  
 
 66  
     public Predicate<E> isLessThanOrEqualTo(Object value) {
 67  0
         Expression<E> right = asExpression(value);
 68  0
         return onNewPredicate(PredicateBuilder.isLessThanOrEqualTo(expression, right));
 69  
     }
 70  
 
 71  
     public Predicate<E> isGreaterThan(Object value) {
 72  6
         Expression<E> right = asExpression(value);
 73  6
         return onNewPredicate(PredicateBuilder.isGreaterThan(expression, right));
 74  
     }
 75  
 
 76  
     public Predicate<E> isGreaterThanOrEqualTo(Object value) {
 77  0
         Expression<E> right = asExpression(value);
 78  0
         return onNewPredicate(PredicateBuilder.isGreaterThanOrEqualTo(expression, right));
 79  
     }
 80  
 
 81  
     public Predicate<E> isInstanceOf(Class type) {
 82  3
         return onNewPredicate(PredicateBuilder.isInstanceOf(expression, type));
 83  
     }
 84  
 
 85  
     public Predicate<E> matchesRegex(String regex) {
 86  0
         return onNewPredicate(PredicateBuilder.regex(expression, regex));
 87  
     }
 88  
 
 89  
     public Predicate<E> isNull() {
 90  0
         return onNewPredicate(PredicateBuilder.isNull(expression));
 91  
     }
 92  
 
 93  
     public Predicate<E> isNotNull() {
 94  0
         return onNewPredicate(PredicateBuilder.isNotNull(expression));
 95  
     }
 96  
 
 97  
     /**
 98  
      * Create a predicate that the left hand expression contains the value of
 99  
      * the right hand expression
 100  
      * 
 101  
      * @param value the element which is compared to be contained within this
 102  
      *                expression
 103  
      * @return a predicate which evaluates to true if the given value expression
 104  
      *         is contained within this expression value
 105  
      */
 106  
 
 107  
     public Predicate<E> contains(Object value) {
 108  0
         Expression<E> right = asExpression(value);
 109  0
         return onNewPredicate(PredicateBuilder.contains(expression, right));
 110  
     }
 111  
 
 112  
     /**
 113  
      * Creates a predicate which is true if this expression matches the given
 114  
      * regular expression
 115  
      * 
 116  
      * @param regex the regular expression to match
 117  
      * @return a predicate which evaluates to true if the expression matches the
 118  
      *         regex
 119  
      */
 120  
 
 121  
     public Predicate<E> regex(String regex) {
 122  6
         return onNewPredicate(PredicateBuilder.regex(expression, regex));
 123  
     }
 124  
 
 125  
     // Expression builders
 126  
     // -------------------------------------------------------------------------
 127  
 
 128  
     public ValueBuilder<E> tokenize() {
 129  0
         return tokenize("\n");
 130  
     }
 131  
 
 132  
     public ValueBuilder<E> tokenize(String token) {
 133  9
         Expression<E> newExp = ExpressionBuilder.tokenizeExpression(expression, token);
 134  9
         return new ValueBuilder<E>(newExp);
 135  
     }
 136  
 
 137  
     /**
 138  
      * Tokenizes the string conversion of this expression using the given
 139  
      * regular expression
 140  
      */
 141  
 
 142  
     public ValueBuilder<E> regexTokenize(String regex) {
 143  0
         Expression<E> newExp = ExpressionBuilder.regexTokenize(expression, regex);
 144  0
         return new ValueBuilder<E>(newExp);
 145  
     }
 146  
 
 147  
     /**
 148  
      * Replaces all occurrencies of the regular expression with the given
 149  
      * replacement
 150  
      */
 151  
 
 152  
     public ValueBuilder<E> regexReplaceAll(String regex, String replacement) {
 153  0
         Expression<E> newExp = ExpressionBuilder.regexReplaceAll(expression, regex, replacement);
 154  0
         return new ValueBuilder<E>(newExp);
 155  
     }
 156  
 
 157  
     /**
 158  
      * Replaces all occurrencies of the regular expression with the given
 159  
      * replacement
 160  
      */
 161  
 
 162  
     public ValueBuilder<E> regexReplaceAll(String regex, Expression<E> replacement) {
 163  0
         Expression<E> newExp = ExpressionBuilder.regexReplaceAll(expression, regex, replacement);
 164  0
         return new ValueBuilder<E>(newExp);
 165  
     }
 166  
 
 167  
     /**
 168  
      * Converts the current value to the given type using the registered type
 169  
      * converters
 170  
      * 
 171  
      * @param type the type to convert the value to
 172  
      * @return the current builder
 173  
      */
 174  
 
 175  
     public ValueBuilder<E> convertTo(Class type) {
 176  12
         Expression<E> newExp = ExpressionBuilder.convertTo(expression, type);
 177  12
         return new ValueBuilder<E>(newExp);
 178  
     }
 179  
 
 180  
     /**
 181  
      * Converts the current value a String using the registered type converters
 182  
      * 
 183  
      * @return the current builder
 184  
      */
 185  
 
 186  
     public ValueBuilder<E> convertToString() {
 187  0
         return convertTo(String.class);
 188  
     }
 189  
 
 190  
     /**
 191  
      * Appends the string evaluation of this expression with the given value
 192  
      * 
 193  
      * @param value the value or expression to append
 194  
      * @return the current builder
 195  
      */
 196  
 
 197  
     public ValueBuilder<E> append(Object value) {
 198  3
         return new ValueBuilder<E>(ExpressionBuilder.append(expression, asExpression(value)));
 199  
     }
 200  
 
 201  
     // Implementation methods
 202  
     // -------------------------------------------------------------------------
 203  
 
 204  
     /**
 205  
      * A stategy method to allow derived classes to deal with the newly created
 206  
      * predicate in different ways
 207  
      */
 208  
     protected Predicate<E> onNewPredicate(Predicate<E> predicate) {
 209  90
         return predicate;
 210  
     }
 211  
 
 212  
     protected Expression<E> asExpression(Object value) {
 213  96
         if (value instanceof Expression) {
 214  9
             return (Expression<E>)value;
 215  
         } else {
 216  87
             return ExpressionBuilder.constantExpression(value);
 217  
         }
 218  
     }
 219  
 }