camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-8832: Added equalis ignore case operation to value builder and simple language
Date Wed, 01 Jul 2015 18:24:25 GMT
Repository: camel
Updated Branches:
  refs/heads/master 864bb35d7 -> a13ff4a4d


CAMEL-8832: Added equalis ignore case operation to value builder and simple language


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a13ff4a4
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a13ff4a4
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a13ff4a4

Branch: refs/heads/master
Commit: a13ff4a4de40f7d7c51ec1588be9bd3f25f0a20b
Parents: 864bb35
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Wed Jul 1 18:08:18 2015 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Jul 1 20:30:35 2015 +0200

----------------------------------------------------------------------
 .../apache/camel/builder/PredicateBuilder.java  | 21 +++++++++++++++
 .../org/apache/camel/builder/ValueBuilder.java  |  5 ++++
 .../camel/language/simple/SimpleTokenizer.java  |  1 +
 .../language/simple/ast/BinaryExpression.java   |  2 ++
 .../simple/types/BinaryOperatorType.java        |  8 +++++-
 .../org/apache/camel/util/ObjectHelper.java     | 28 +++++++++++++++++---
 .../apache/camel/builder/ValueBuilderTest.java  |  2 ++
 .../language/simple/SimpleOperatorTest.java     | 16 +++++++++++
 8 files changed, 79 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a13ff4a4/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
index c2eb5c9..ec4c0db 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
@@ -185,6 +185,27 @@ public final class PredicateBuilder {
         };
     }
 
+    public static Predicate isEqualToIgnoreCase(final Expression left, final Expression right)
{
+        return new BinaryPredicateSupport(left, right) {
+
+            protected boolean matches(Exchange exchange, Object leftValue, Object rightValue)
{
+                if (leftValue == null && rightValue == null) {
+                    // they are equal
+                    return true;
+                } else if (leftValue == null || rightValue == null) {
+                    // only one of them is null so they are not equal
+                    return false;
+                }
+
+                return ObjectHelper.typeCoerceEquals(exchange.getContext().getTypeConverter(),
leftValue, rightValue, true);
+            }
+
+            protected String getOperationText() {
+                return "~=";
+            }
+        };
+    }
+
     public static Predicate isNotEqualTo(final Expression left, final Expression right) {
         return new BinaryPredicateSupport(left, right) {
 

http://git-wip-us.apache.org/repos/asf/camel/blob/a13ff4a4/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
index 60782c3..a8faf61 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
@@ -80,6 +80,11 @@ public class ValueBuilder implements Expression, Predicate {
         return onNewPredicate(PredicateBuilder.isEqualTo(expression, right));
     }
 
+    public Predicate isEqualToIgnoreCase(Object value) {
+        Expression right = asExpression(value);
+        return onNewPredicate(PredicateBuilder.isEqualToIgnoreCase(expression, right));
+    }
+
     public Predicate isLessThan(Object value) {
         Expression right = asExpression(value);
         return onNewPredicate(PredicateBuilder.isLessThan(expression, right));

http://git-wip-us.apache.org/repos/asf/camel/blob/a13ff4a4/camel-core/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java
b/camel-core/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java
index cd38aea..3977260 100644
--- a/camel-core/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java
+++ b/camel-core/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java
@@ -50,6 +50,7 @@ public final class SimpleTokenizer {
 
         // binary operators
         KNOWN_TOKENS.add(new SimpleTokenType(TokenType.binaryOperator, "=="));
+        KNOWN_TOKENS.add(new SimpleTokenType(TokenType.binaryOperator, "=~"));
         KNOWN_TOKENS.add(new SimpleTokenType(TokenType.binaryOperator, ">="));
         KNOWN_TOKENS.add(new SimpleTokenType(TokenType.binaryOperator, "<="));
         KNOWN_TOKENS.add(new SimpleTokenType(TokenType.binaryOperator, ">"));

http://git-wip-us.apache.org/repos/asf/camel/blob/a13ff4a4/camel-core/src/main/java/org/apache/camel/language/simple/ast/BinaryExpression.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/language/simple/ast/BinaryExpression.java
b/camel-core/src/main/java/org/apache/camel/language/simple/ast/BinaryExpression.java
index ab1ddc4..6d0e779 100644
--- a/camel-core/src/main/java/org/apache/camel/language/simple/ast/BinaryExpression.java
+++ b/camel-core/src/main/java/org/apache/camel/language/simple/ast/BinaryExpression.java
@@ -80,6 +80,8 @@ public class BinaryExpression extends BaseSimpleNode {
 
         if (operator == BinaryOperatorType.EQ) {
             return createExpression(leftExp, rightExp, PredicateBuilder.isEqualTo(leftExp,
rightExp));
+        } else if (operator == BinaryOperatorType.EQ_IGNORE) {
+            return createExpression(leftExp, rightExp, PredicateBuilder.isEqualToIgnoreCase(leftExp,
rightExp));
         } else if (operator == BinaryOperatorType.GT) {
             return createExpression(leftExp, rightExp, PredicateBuilder.isGreaterThan(leftExp,
rightExp));
         } else if (operator == BinaryOperatorType.GTE) {

http://git-wip-us.apache.org/repos/asf/camel/blob/a13ff4a4/camel-core/src/main/java/org/apache/camel/language/simple/types/BinaryOperatorType.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/language/simple/types/BinaryOperatorType.java
b/camel-core/src/main/java/org/apache/camel/language/simple/types/BinaryOperatorType.java
index c25f033..2156c30 100644
--- a/camel-core/src/main/java/org/apache/camel/language/simple/types/BinaryOperatorType.java
+++ b/camel-core/src/main/java/org/apache/camel/language/simple/types/BinaryOperatorType.java
@@ -21,12 +21,14 @@ package org.apache.camel.language.simple.types;
  */
 public enum BinaryOperatorType {
 
-    EQ, GT, GTE, LT, LTE, NOT_EQ, CONTAINS, NOT_CONTAINS, REGEX, NOT_REGEX,
+    EQ, EQ_IGNORE, GT, GTE, LT, LTE, NOT_EQ, CONTAINS, NOT_CONTAINS, REGEX, NOT_REGEX,
     IN, NOT_IN, IS, NOT_IS, RANGE, NOT_RANGE;
 
     public static BinaryOperatorType asOperator(String text) {
         if ("==".equals(text)) {
             return EQ;
+        } else if ("=~".equals(text)) {
+            return EQ_IGNORE;
         } else if (">".equals(text)) {
             return GT;
         } else if (">=".equals(text)) {
@@ -64,6 +66,8 @@ public enum BinaryOperatorType {
     public static String getOperatorText(BinaryOperatorType operator) {
         if (operator == EQ) {
             return "==";
+        } else if (operator == EQ_IGNORE) {
+            return "=~";
         } else if (operator == GT) {
             return ">";
         } else if (operator == GTE) {
@@ -146,6 +150,8 @@ public enum BinaryOperatorType {
     public static ParameterType[] supportedParameterTypes(BinaryOperatorType operator) {
         if (operator == EQ) {
             return null;
+        } else if (operator == EQ_IGNORE) {
+            return null;
         } else if (operator == GT) {
             return null;
         } else if (operator == GTE) {

http://git-wip-us.apache.org/repos/asf/camel/blob/a13ff4a4/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
index 4e9ad05..0e49e32 100644
--- a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
@@ -80,6 +80,15 @@ public final class ObjectHelper {
      * a String and Integer type as Camel will be able to coerce the types.
      */
     public static boolean typeCoerceEquals(TypeConverter converter, Object leftValue, Object
rightValue) {
+        return typeCoerceEquals(converter, leftValue, rightValue, false);
+    }
+
+    /**
+     * A helper method for comparing objects for equality in which it uses type coercion
to coerce
+     * types between the left and right values. This allows you test for equality for example
with
+     * a String and Integer type as Camel will be able to coerce the types.
+     */
+    public static boolean typeCoerceEquals(TypeConverter converter, Object leftValue, Object
rightValue, boolean ignoreCase) {
         // sanity check
         if (leftValue == null && rightValue == null) {
             // they are equal
@@ -90,7 +99,7 @@ public final class ObjectHelper {
         }
 
         // try without type coerce
-        boolean answer = equal(leftValue, rightValue);
+        boolean answer = equal(leftValue, rightValue, ignoreCase);
         if (answer) {
             return true;
         }
@@ -102,14 +111,14 @@ public final class ObjectHelper {
 
         // convert left to right
         Object value = converter.tryConvertTo(rightValue.getClass(), leftValue);
-        answer = equal(value, rightValue);
+        answer = equal(value, rightValue, ignoreCase);
         if (answer) {
             return true;
         }
 
         // convert right to left
         value = converter.tryConvertTo(leftValue.getClass(), rightValue);
-        answer = equal(leftValue, value);
+        answer = equal(leftValue, value, ignoreCase);
         return answer;
     }
 
@@ -178,6 +187,13 @@ public final class ObjectHelper {
      * A helper method for comparing objects for equality while handling nulls
      */
     public static boolean equal(Object a, Object b) {
+        return equal(a, b, false);
+    }
+
+    /**
+     * A helper method for comparing objects for equality while handling nulls
+     */
+    public static boolean equal(Object a, Object b, boolean ignoreCase) {
         if (a == b) {
             return true;
         }
@@ -186,6 +202,12 @@ public final class ObjectHelper {
             return equalByteArray((byte[])a, (byte[])b);
         }
 
+        if (ignoreCase) {
+            if (a instanceof String && b instanceof String) {
+                return ((String) a).equalsIgnoreCase((String) b);
+            }
+        }
+
         return a != null && b != null && a.equals(b);
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/a13ff4a4/camel-core/src/test/java/org/apache/camel/builder/ValueBuilderTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/builder/ValueBuilderTest.java b/camel-core/src/test/java/org/apache/camel/builder/ValueBuilderTest.java
index b07ebbe..58846b4 100644
--- a/camel-core/src/test/java/org/apache/camel/builder/ValueBuilderTest.java
+++ b/camel-core/src/test/java/org/apache/camel/builder/ValueBuilderTest.java
@@ -42,6 +42,7 @@ public class ValueBuilderTest extends ContextTestSupport {
 
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedBodiesReceived("Hello World");
+        mock.message(0).body().isEqualToIgnoreCase("hello WORLD");
 
         template.sendBody("direct:start", "Hello");
 
@@ -59,6 +60,7 @@ public class ValueBuilderTest extends ContextTestSupport {
 
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedBodiesReceived("Hello World");
+        mock.message(0).body().isEqualToIgnoreCase("hello world");
 
         template.sendBody("direct:start", "World");
 

http://git-wip-us.apache.org/repos/asf/camel/blob/a13ff4a4/camel-core/src/test/java/org/apache/camel/language/simple/SimpleOperatorTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleOperatorTest.java
b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleOperatorTest.java
index fb64858..e9e4d6f 100644
--- a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleOperatorTest.java
+++ b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleOperatorTest.java
@@ -152,6 +152,22 @@ public class SimpleOperatorTest extends LanguageTestSupport {
         assertPredicate("${in.header.bar} == '1'", false);
     }
 
+    public void testEqualIgnoreOperator() throws Exception {
+        // string to string comparison
+        assertPredicate("${in.header.foo} =~ 'abc'", true);
+        assertPredicate("${in.header.foo} =~ 'ABC'", true);
+        assertPredicate("${in.header.foo} =~ 'Abc'", true);
+        assertPredicate("${in.header.foo} =~ 'Def'", false);
+        assertPredicate("${in.header.foo} =~ '1'", false);
+
+        // integer to string comparison
+        assertPredicate("${in.header.bar} =~ '123'", true);
+        assertPredicate("${in.header.bar} =~ 123", true);
+        assertPredicate("${in.header.bar} =~ '444'", false);
+        assertPredicate("${in.header.bar} =~ 444", false);
+        assertPredicate("${in.header.bar} =~ '1'", false);
+    }
+
     public void testNotEqualOperator() throws Exception {
         // string to string comparison
         assertPredicate("${in.header.foo} != 'abc'", false);


Mime
View raw message