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.builder; 018 019 import org.apache.camel.Exchange; 020 import org.apache.camel.Expression; 021 import org.apache.camel.Predicate; 022 023 /** 024 * A builder of expressions or predicates based on values. 025 * 026 * @version $Revision: 63317 $ 027 */ 028 public class ValueBuilder<E extends Exchange> implements Expression<E> { 029 private Expression<E> expression; 030 031 public ValueBuilder(Expression<E> expression) { 032 this.expression = expression; 033 } 034 035 public Object evaluate(E exchange) { 036 return expression.evaluate(exchange); 037 } 038 039 public Expression<E> getExpression() { 040 return expression; 041 } 042 043 @Override 044 public String toString() { 045 return expression.toString(); 046 } 047 048 // Predicate builders 049 // ------------------------------------------------------------------------- 050 051 public Predicate<E> isNotEqualTo(Object value) { 052 Expression<E> right = asExpression(value); 053 return onNewPredicate(PredicateBuilder.isNotEqualTo(expression, right)); 054 } 055 056 public Predicate<E> isEqualTo(Object value) { 057 Expression<E> right = asExpression(value); 058 return onNewPredicate(PredicateBuilder.isEqualTo(expression, right)); 059 } 060 061 public Predicate<E> isLessThan(Object value) { 062 Expression<E> right = asExpression(value); 063 return onNewPredicate(PredicateBuilder.isLessThan(expression, right)); 064 } 065 066 public Predicate<E> isLessThanOrEqualTo(Object value) { 067 Expression<E> right = asExpression(value); 068 return onNewPredicate(PredicateBuilder.isLessThanOrEqualTo(expression, right)); 069 } 070 071 public Predicate<E> isGreaterThan(Object value) { 072 Expression<E> right = asExpression(value); 073 return onNewPredicate(PredicateBuilder.isGreaterThan(expression, right)); 074 } 075 076 public Predicate<E> isGreaterThanOrEqualTo(Object value) { 077 Expression<E> right = asExpression(value); 078 return onNewPredicate(PredicateBuilder.isGreaterThanOrEqualTo(expression, right)); 079 } 080 081 public Predicate<E> isInstanceOf(Class type) { 082 return onNewPredicate(PredicateBuilder.isInstanceOf(expression, type)); 083 } 084 085 public Predicate<E> matchesRegex(String regex) { 086 return onNewPredicate(PredicateBuilder.regex(expression, regex)); 087 } 088 089 public Predicate<E> isNull() { 090 return onNewPredicate(PredicateBuilder.isNull(expression)); 091 } 092 093 public Predicate<E> isNotNull() { 094 return onNewPredicate(PredicateBuilder.isNotNull(expression)); 095 } 096 097 /** 098 * Create a predicate that the left hand expression contains the value of 099 * 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 public Predicate<E> contains(Object value) { 107 Expression<E> right = asExpression(value); 108 return onNewPredicate(PredicateBuilder.contains(expression, right)); 109 } 110 111 /** 112 * Creates a predicate which is true if this expression matches the given 113 * regular expression 114 * 115 * @param regex the regular expression to match 116 * @return a predicate which evaluates to true if the expression matches the 117 * regex 118 */ 119 public Predicate<E> regex(String regex) { 120 return onNewPredicate(PredicateBuilder.regex(expression, regex)); 121 } 122 123 // Expression builders 124 // ------------------------------------------------------------------------- 125 126 public ValueBuilder<E> tokenize() { 127 return tokenize("\n"); 128 } 129 130 public ValueBuilder<E> tokenize(String token) { 131 Expression<E> newExp = ExpressionBuilder.tokenizeExpression(expression, token); 132 return new ValueBuilder<E>(newExp); 133 } 134 135 /** 136 * Tokenizes the string conversion of this expression using the given 137 * regular expression 138 */ 139 public ValueBuilder<E> regexTokenize(String regex) { 140 Expression<E> newExp = ExpressionBuilder.regexTokenize(expression, regex); 141 return new ValueBuilder<E>(newExp); 142 } 143 144 /** 145 * Replaces all occurrencies of the regular expression with the given 146 * replacement 147 */ 148 public ValueBuilder<E> regexReplaceAll(String regex, String replacement) { 149 Expression<E> newExp = ExpressionBuilder.regexReplaceAll(expression, regex, replacement); 150 return new ValueBuilder<E>(newExp); 151 } 152 153 /** 154 * Replaces all occurrencies of the regular expression with the given 155 * replacement 156 */ 157 public ValueBuilder<E> regexReplaceAll(String regex, Expression<E> replacement) { 158 Expression<E> newExp = ExpressionBuilder.regexReplaceAll(expression, regex, replacement); 159 return new ValueBuilder<E>(newExp); 160 } 161 162 /** 163 * Converts the current value to the given type using the registered type 164 * converters 165 * 166 * @param type the type to convert the value to 167 * @return the current builder 168 */ 169 public ValueBuilder<E> convertTo(Class type) { 170 Expression<E> newExp = ExpressionBuilder.convertTo(expression, type); 171 return new ValueBuilder<E>(newExp); 172 } 173 174 /** 175 * Converts the current value a String using the registered type converters 176 * 177 * @return the current builder 178 */ 179 public ValueBuilder<E> convertToString() { 180 return convertTo(String.class); 181 } 182 183 /** 184 * Appends the string evaluation of this expression with the given value 185 * 186 * @param value the value or expression to append 187 * @return the current builder 188 */ 189 public ValueBuilder<E> append(Object value) { 190 return new ValueBuilder<E>(ExpressionBuilder.append(expression, asExpression(value))); 191 } 192 193 // Implementation methods 194 // ------------------------------------------------------------------------- 195 196 /** 197 * A strategy method to allow derived classes to deal with the newly created 198 * predicate in different ways 199 */ 200 protected Predicate<E> onNewPredicate(Predicate<E> predicate) { 201 return predicate; 202 } 203 204 protected Expression<E> asExpression(Object value) { 205 if (value instanceof Expression) { 206 return (Expression<E>)value; 207 } else { 208 return ExpressionBuilder.constantExpression(value); 209 } 210 } 211 }