camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [1/2] camel git commit: CAMEL-8983: Add random function to simple
Date Tue, 21 Jul 2015 15:44:17 GMT
Repository: camel
Updated Branches:
  refs/heads/master edb9fd303 -> b63db70a5


CAMEL-8983: Add random function to simple


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

Branch: refs/heads/master
Commit: 7ee11a60c7972214d1b79cb7e4206e2d79475549
Parents: edb9fd3
Author: Andrea Cosentino <ancosen@gmail.com>
Authored: Tue Jul 21 17:41:53 2015 +0200
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Tue Jul 21 17:41:53 2015 +0200

----------------------------------------------------------------------
 .../apache/camel/builder/ExpressionBuilder.java | 37 ++++++++++++++++++++
 .../simple/ast/SimpleFunctionExpression.java    | 21 +++++++++++
 .../camel/language/simple/SimpleTest.java       | 27 ++++++++++++++
 3 files changed, 85 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/7ee11a60/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
index 98b6087..6d722fb3 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
@@ -25,6 +25,7 @@ import java.util.Comparator;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Random;
 import java.util.Scanner;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.regex.Pattern;
@@ -1869,6 +1870,42 @@ public final class ExpressionBuilder {
             }
         };
     }
+    
+    /**
+     * Returns a random number between min and max
+     */
+    public static Expression randomExpression(final int min, final int max) {
+        return new ExpressionAdapter() {
+            public Object evaluate(Exchange exchange) {
+                Random random = new Random();
+                int randomNum = random.nextInt((max - min)) + min;
+                return randomNum;
+            }
+
+            @Override
+            public String toString() {
+                return "random";
+            }
+        };
+    }
+    
+    /**
+     * Returns a random number between 0 and upperbound (exclusive)
+     */
+    public static Expression randomExpression(final int upperbound) {
+        return new ExpressionAdapter() {
+            public Object evaluate(Exchange exchange) {
+                Random random = new Random();
+                int randomNum = random.nextInt(upperbound);
+                return randomNum;
+            }
+
+            @Override
+            public String toString() {
+                return "random";
+            }
+        };
+    }
 
     /**
      * Expression adapter for OGNL expression from Message Header or Exchange property

http://git-wip-us.apache.org/repos/asf/camel/blob/7ee11a60/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
index 8d10ca7..2bb52ce 100644
--- a/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
+++ b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
@@ -321,6 +321,27 @@ public class SimpleFunctionExpression extends LiteralExpression {
         if (remainder != null) {
             return ExpressionBuilder.outHeaderExpression(remainder);
         }
+        
+        // random
+        remainder = ifStartsWithReturnRemainder("random", function);
+        if (remainder != null) {
+            String values = ObjectHelper.between(remainder, "(", ")");
+            if (values == null || ObjectHelper.isEmpty(values)) {
+                throw new SimpleParserException("Valid syntax: ${random(min,max)} or ${random(max)}
was: " + function, token.getIndex());
+            }
+            if (values.contains(",")) {
+                String[] tokens = values.split(",", -1);
+                if (tokens.length > 2) {
+                    throw new SimpleParserException("Valid syntax: ${random(min,max)} or
${random(max)} was: " + function, token.getIndex());
+                }
+                int min = Integer.parseInt(tokens[0]);
+                int max = Integer.parseInt(tokens[1]);
+                return ExpressionBuilder.randomExpression(min,max);
+            } else {
+                int max = Integer.parseInt(values);
+                return ExpressionBuilder.randomExpression(max);
+            }
+        }
 
         return null;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/7ee11a60/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index c73fbfe..04da41c 100644
--- a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++ b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -1439,6 +1439,33 @@ public class SimpleTest extends LanguageTestSupport {
         exchange.getIn().setBody("87444549697");
         assertPredicate("${body} regex '^(tel:\\+)(974)(44)(\\d+)|^(974)(44)(\\d+)'", false);
     }
+    
+    public void testRandomExpression() throws Exception {
+        int min = 1;
+        int max = 10;
+        int iterations = 30;
+        int i = 0;
+        for (i=0;i<iterations;i++) {
+            Expression expression = SimpleLanguage.simple("random(1,10)", Integer.class);
+            assertTrue(min <= expression.evaluate(exchange, Integer.class) &&
expression.evaluate(exchange, Integer.class) < max);
+        }
+        for (i=0;i<iterations;i++) {
+            Expression expression = SimpleLanguage.simple("random(10)", Integer.class);
+            assertTrue(0 <= expression.evaluate(exchange, Integer.class) && expression.evaluate(exchange,
Integer.class) < max);
+        }
+        try {
+            assertExpression("random(10,21,30)", null);
+            fail("Should have thrown exception");
+        } catch (SimpleParserException e) {
+            assertEquals("Valid syntax: ${random(min,max)} or ${random(max)} was: random(10,21,30)",
e.getMessage());
+        }
+        try {
+            assertExpression("random()", null);
+            fail("Should have thrown exception");
+        } catch (SimpleParserException e) {
+            assertEquals("Valid syntax: ${random(min,max)} or ${random(max)} was: random()",
e.getMessage());
+        }
+    }
 
     protected String getLanguageName() {
         return "simple";


Mime
View raw message