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