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