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 java.util.ArrayList; 020 import java.util.Comparator; 021 import java.util.List; 022 023 import org.apache.camel.Exchange; 024 import org.apache.camel.Expression; 025 import org.apache.camel.Predicate; 026 027 /** 028 * A builder of expressions or predicates based on values. 029 * 030 * @version $Revision: 790462 $ 031 */ 032 public class ValueBuilder implements Expression { 033 private Expression expression; 034 035 public ValueBuilder(Expression expression) { 036 this.expression = expression; 037 } 038 039 public <T> T evaluate(Exchange exchange, Class<T> type) { 040 return expression.evaluate(exchange, type); 041 } 042 043 public Expression getExpression() { 044 return expression; 045 } 046 047 @Override 048 public String toString() { 049 return expression.toString(); 050 } 051 052 // Predicate builders 053 // ------------------------------------------------------------------------- 054 055 public Predicate isNotEqualTo(Object value) { 056 Expression right = asExpression(value); 057 return onNewPredicate(PredicateBuilder.isNotEqualTo(expression, right)); 058 } 059 060 public Predicate isEqualTo(Object value) { 061 Expression right = asExpression(value); 062 return onNewPredicate(PredicateBuilder.isEqualTo(expression, right)); 063 } 064 065 public Predicate isLessThan(Object value) { 066 Expression right = asExpression(value); 067 return onNewPredicate(PredicateBuilder.isLessThan(expression, right)); 068 } 069 070 public Predicate isLessThanOrEqualTo(Object value) { 071 Expression right = asExpression(value); 072 return onNewPredicate(PredicateBuilder.isLessThanOrEqualTo(expression, right)); 073 } 074 075 public Predicate isGreaterThan(Object value) { 076 Expression right = asExpression(value); 077 return onNewPredicate(PredicateBuilder.isGreaterThan(expression, right)); 078 } 079 080 public Predicate isGreaterThanOrEqualTo(Object value) { 081 Expression right = asExpression(value); 082 return onNewPredicate(PredicateBuilder.isGreaterThanOrEqualTo(expression, right)); 083 } 084 085 public Predicate isInstanceOf(Class type) { 086 return onNewPredicate(PredicateBuilder.isInstanceOf(expression, type)); 087 } 088 089 public Predicate isNull() { 090 return onNewPredicate(PredicateBuilder.isNull(expression)); 091 } 092 093 public Predicate isNotNull() { 094 return onNewPredicate(PredicateBuilder.isNotNull(expression)); 095 } 096 097 public Predicate not(Predicate predicate) { 098 return onNewPredicate(PredicateBuilder.not(predicate)); 099 } 100 101 public Predicate in(Object... values) { 102 List<Predicate> predicates = new ArrayList<Predicate>(); 103 for (Object value : values) { 104 Expression right = asExpression(value); 105 right = ExpressionBuilder.convertToExpression(right, expression); 106 Predicate predicate = onNewPredicate(PredicateBuilder.isEqualTo(expression, right)); 107 predicates.add(predicate); 108 } 109 return in(predicates.toArray(new Predicate[predicates.size()])); 110 } 111 112 public Predicate in(Predicate... predicates) { 113 return onNewPredicate(PredicateBuilder.in(predicates)); 114 } 115 116 public Predicate startsWith(Object value) { 117 Expression right = asExpression(value); 118 return onNewPredicate(PredicateBuilder.startsWith(expression, right)); 119 } 120 121 public Predicate endsWith(Object value) { 122 Expression right = asExpression(value); 123 return onNewPredicate(PredicateBuilder.endsWith(expression, right)); 124 } 125 126 /** 127 * Create a predicate that the left hand expression contains the value of 128 * the right hand expression 129 * 130 * @param value the element which is compared to be contained within this 131 * expression 132 * @return a predicate which evaluates to true if the given value expression 133 * is contained within this expression value 134 */ 135 public Predicate contains(Object value) { 136 Expression right = asExpression(value); 137 return onNewPredicate(PredicateBuilder.contains(expression, right)); 138 } 139 140 /** 141 * Creates a predicate which is true if this expression matches the given 142 * regular expression 143 * 144 * @param regex the regular expression to match 145 * @return a predicate which evaluates to true if the expression matches the 146 * regex 147 */ 148 public Predicate regex(String regex) { 149 return onNewPredicate(PredicateBuilder.regex(expression, regex)); 150 } 151 152 // Expression builders 153 // ------------------------------------------------------------------------- 154 155 public ValueBuilder tokenize() { 156 return tokenize("\n"); 157 } 158 159 public ValueBuilder tokenize(String token) { 160 Expression newExp = ExpressionBuilder.tokenizeExpression(expression, token); 161 return new ValueBuilder(newExp); 162 } 163 164 /** 165 * Tokenizes the string conversion of this expression using the given 166 * regular expression 167 */ 168 public ValueBuilder regexTokenize(String regex) { 169 Expression newExp = ExpressionBuilder.regexTokenizeExpression(expression, regex); 170 return new ValueBuilder(newExp); 171 } 172 173 /** 174 * Replaces all occurrences of the regular expression with the given 175 * replacement 176 */ 177 public ValueBuilder regexReplaceAll(String regex, String replacement) { 178 Expression newExp = ExpressionBuilder.regexReplaceAll(expression, regex, replacement); 179 return new ValueBuilder(newExp); 180 } 181 182 /** 183 * Replaces all occurrences of the regular expression with the given 184 * replacement 185 */ 186 public ValueBuilder regexReplaceAll(String regex, Expression replacement) { 187 Expression newExp = ExpressionBuilder.regexReplaceAll(expression, regex, replacement); 188 return new ValueBuilder(newExp); 189 } 190 191 /** 192 * Converts the current value to the given type using the registered type 193 * converters 194 * 195 * @param type the type to convert the value to 196 * @return the current builder 197 */ 198 public ValueBuilder convertTo(Class<?> type) { 199 Expression newExp = ExpressionBuilder.convertToExpression(expression, type); 200 return new ValueBuilder(newExp); 201 } 202 203 /** 204 * Converts the current value to a String using the registered type converters 205 * 206 * @return the current builder 207 */ 208 public ValueBuilder convertToString() { 209 return convertTo(String.class); 210 } 211 212 /** 213 * Appends the string evaluation of this expression with the given value 214 * 215 * @param value the value or expression to append 216 * @return the current builder 217 */ 218 public ValueBuilder append(Object value) { 219 return new ValueBuilder(ExpressionBuilder.append(expression, asExpression(value))); 220 } 221 222 /** 223 * Prepends the string evaluation of this expression with the given value 224 * 225 * @param value the value or expression to prepend 226 * @return the current builder 227 */ 228 public ValueBuilder prepend(Object value) { 229 return new ValueBuilder(ExpressionBuilder.prepend(expression, asExpression(value))); 230 } 231 232 /** 233 * Sorts the current value using the given comparator. The current value must be convertable 234 * to a {@link List} to allow sorting using the comparator. 235 * 236 * @param comparator the comparator used by sorting 237 * @return the current builder 238 */ 239 public ValueBuilder sort(Comparator comparator) { 240 Expression newExp = ExpressionBuilder.sortExpression(expression, comparator); 241 return new ValueBuilder(newExp); 242 } 243 244 // Implementation methods 245 // ------------------------------------------------------------------------- 246 247 /** 248 * A strategy method to allow derived classes to deal with the newly created 249 * predicate in different ways 250 */ 251 protected Predicate onNewPredicate(Predicate predicate) { 252 return predicate; 253 } 254 255 protected Expression asExpression(Object value) { 256 if (value instanceof Expression) { 257 return (Expression)value; 258 } else { 259 return ExpressionBuilder.constantExpression(value); 260 } 261 } 262 }