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