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: 2665 $ 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 /** 086 * @deprecated use {@link #regex(String)}. Will be removed in Camel 2.0 087 */ 088 public Predicate<E> matchesRegex(String regex) { 089 return onNewPredicate(PredicateBuilder.regex(expression, regex)); 090 } 091 092 public Predicate<E> isNull() { 093 return onNewPredicate(PredicateBuilder.isNull(expression)); 094 } 095 096 public Predicate<E> isNotNull() { 097 return onNewPredicate(PredicateBuilder.isNotNull(expression)); 098 } 099 100 /** 101 * Create a predicate that the left hand expression contains the value of 102 * the right hand expression 103 * 104 * @param value the element which is compared to be contained within this 105 * expression 106 * @return a predicate which evaluates to true if the given value expression 107 * is contained within this expression value 108 */ 109 public Predicate<E> contains(Object value) { 110 Expression<E> right = asExpression(value); 111 return onNewPredicate(PredicateBuilder.contains(expression, right)); 112 } 113 114 /** 115 * Creates a predicate which is true if this expression matches the given 116 * regular expression 117 * 118 * @param regex the regular expression to match 119 * @return a predicate which evaluates to true if the expression matches the 120 * regex 121 */ 122 public Predicate<E> regex(String regex) { 123 return onNewPredicate(PredicateBuilder.regex(expression, regex)); 124 } 125 126 // Expression builders 127 // ------------------------------------------------------------------------- 128 129 public ValueBuilder<E> tokenize() { 130 return tokenize("\n"); 131 } 132 133 public ValueBuilder<E> tokenize(String token) { 134 Expression<E> newExp = ExpressionBuilder.tokenizeExpression(expression, token); 135 return new ValueBuilder<E>(newExp); 136 } 137 138 /** 139 * Tokenizes the string conversion of this expression using the given 140 * regular expression 141 */ 142 public ValueBuilder<E> regexTokenize(String regex) { 143 Expression<E> newExp = ExpressionBuilder.regexTokenize(expression, regex); 144 return new ValueBuilder<E>(newExp); 145 } 146 147 /** 148 * Replaces all occurrences of the regular expression with the given 149 * replacement 150 */ 151 public ValueBuilder<E> regexReplaceAll(String regex, String replacement) { 152 Expression<E> newExp = ExpressionBuilder.regexReplaceAll(expression, regex, replacement); 153 return new ValueBuilder<E>(newExp); 154 } 155 156 /** 157 * Replaces all occurrences of the regular expression with the given 158 * replacement 159 */ 160 public ValueBuilder<E> regexReplaceAll(String regex, Expression<E> replacement) { 161 Expression<E> newExp = ExpressionBuilder.regexReplaceAll(expression, regex, replacement); 162 return new ValueBuilder<E>(newExp); 163 } 164 165 /** 166 * Converts the current value to the given type using the registered type 167 * converters 168 * 169 * @param type the type to convert the value to 170 * @return the current builder 171 */ 172 public ValueBuilder<E> convertTo(Class type) { 173 Expression<E> newExp = ExpressionBuilder.convertTo(expression, type); 174 return new ValueBuilder<E>(newExp); 175 } 176 177 /** 178 * Converts the current value a String using the registered type converters 179 * 180 * @return the current builder 181 */ 182 public ValueBuilder<E> convertToString() { 183 return convertTo(String.class); 184 } 185 186 /** 187 * Appends the string evaluation of this expression with the given value 188 * 189 * @param value the value or expression to append 190 * @return the current builder 191 */ 192 public ValueBuilder<E> append(Object value) { 193 return new ValueBuilder<E>(ExpressionBuilder.append(expression, asExpression(value))); 194 } 195 196 // Implementation methods 197 // ------------------------------------------------------------------------- 198 199 /** 200 * A strategy method to allow derived classes to deal with the newly created 201 * predicate in different ways 202 */ 203 protected Predicate<E> onNewPredicate(Predicate<E> predicate) { 204 return predicate; 205 } 206 207 protected Expression<E> asExpression(Object value) { 208 if (value instanceof Expression) { 209 return (Expression<E>)value; 210 } else { 211 return ExpressionBuilder.constantExpression(value); 212 } 213 } 214 }