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: 780937 $
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        /**
117         * Create a predicate that the left hand expression contains the value of
118         * the right hand expression
119         * 
120         * @param value the element which is compared to be contained within this
121         *                expression
122         * @return a predicate which evaluates to true if the given value expression
123         *         is contained within this expression value
124         */
125        public Predicate contains(Object value) {
126            Expression right = asExpression(value);
127            return onNewPredicate(PredicateBuilder.contains(expression, right));
128        }
129    
130        /**
131         * Creates a predicate which is true if this expression matches the given
132         * regular expression
133         * 
134         * @param regex the regular expression to match
135         * @return a predicate which evaluates to true if the expression matches the
136         *         regex
137         */
138        public Predicate regex(String regex) {
139            return onNewPredicate(PredicateBuilder.regex(expression, regex));
140        }
141    
142        // Expression builders
143        // -------------------------------------------------------------------------
144    
145        public ValueBuilder tokenize() {
146            return tokenize("\n");
147        }
148    
149        public ValueBuilder tokenize(String token) {
150            Expression newExp = ExpressionBuilder.tokenizeExpression(expression, token);
151            return new ValueBuilder(newExp);
152        }
153    
154        /**
155         * Tokenizes the string conversion of this expression using the given
156         * regular expression
157         */
158        public ValueBuilder regexTokenize(String regex) {
159            Expression newExp = ExpressionBuilder.regexTokenizeExpression(expression, regex);
160            return new ValueBuilder(newExp);
161        }
162    
163        /**
164         * Replaces all occurrences of the regular expression with the given
165         * replacement
166         */
167        public ValueBuilder regexReplaceAll(String regex, String replacement) {
168            Expression newExp = ExpressionBuilder.regexReplaceAll(expression, regex, replacement);
169            return new ValueBuilder(newExp);
170        }
171    
172        /**
173         * Replaces all occurrences of the regular expression with the given
174         * replacement
175         */
176        public ValueBuilder regexReplaceAll(String regex, Expression replacement) {
177            Expression newExp = ExpressionBuilder.regexReplaceAll(expression, regex, replacement);
178            return new ValueBuilder(newExp);
179        }
180    
181        /**
182         * Converts the current value to the given type using the registered type
183         * converters
184         * 
185         * @param type the type to convert the value to
186         * @return the current builder
187         */
188        public ValueBuilder convertTo(Class<?> type) {
189            Expression newExp = ExpressionBuilder.convertToExpression(expression, type);
190            return new ValueBuilder(newExp);
191        }
192    
193        /**
194         * Converts the current value to a String using the registered type converters
195         * 
196         * @return the current builder
197         */
198        public ValueBuilder convertToString() {
199            return convertTo(String.class);
200        }
201    
202        /**
203         * Appends the string evaluation of this expression with the given value
204         *
205         * @param value the value or expression to append
206         * @return the current builder
207         */
208        public ValueBuilder append(Object value) {
209            return new ValueBuilder(ExpressionBuilder.append(expression, asExpression(value)));
210        }
211    
212        /**
213         * Prepends the string evaluation of this expression with the given value
214         *
215         * @param value the value or expression to prepend
216         * @return the current builder
217         */
218        public ValueBuilder prepend(Object value) {
219            return new ValueBuilder(ExpressionBuilder.prepend(expression, asExpression(value)));
220        }
221    
222        /**
223         * Sorts the current value using the given comparator. The current value must be convertable
224         * to a {@link List} to allow sorting using the comparator.
225         *
226         * @param comparator  the comparator used by sorting
227         * @return the current builder
228         */
229        public ValueBuilder sort(Comparator comparator) {
230            Expression newExp = ExpressionBuilder.sortExpression(expression, comparator);
231            return new ValueBuilder(newExp);
232        }
233    
234        // Implementation methods
235        // -------------------------------------------------------------------------
236    
237        /**
238         * A strategy method to allow derived classes to deal with the newly created
239         * predicate in different ways
240         */
241        protected Predicate onNewPredicate(Predicate predicate) {
242            return predicate;
243        }
244    
245        protected Expression asExpression(Object value) {
246            if (value instanceof Expression) {
247                return (Expression)value;
248            } else {
249                return ExpressionBuilder.constantExpression(value);
250            }
251        }
252    }