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