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