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