camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r545076 - in /activemq/camel/trunk/camel-core/src/main/java/org/apache/camel: builder/ExpressionBuilder.java builder/FromBuilder.java builder/ResequencerBuilder.java processor/Resequencer.java util/ExpressionListComparator.java
Date Thu, 07 Jun 2007 05:56:06 GMT
Author: jstrachan
Date: Wed Jun  6 22:56:05 2007
New Revision: 545076

URL: http://svn.apache.org/viewvc?view=rev&rev=545076
Log:
allow the resequencer and comparator to take a list of expressions

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExpressionListComparator.java
      - copied, changed from r541717, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExpressionComparator.java
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ResequencerBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Resequencer.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?view=diff&rev=545076&r1=545075&r2=545076
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
Wed Jun  6 22:56:05 2007
@@ -47,6 +47,7 @@
                 }
                 return header;
             }
+            
 
             @Override
             public String toString() {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java?view=diff&rev=545076&r1=545075&r2=545076
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java
Wed Jun  6 22:56:05 2007
@@ -36,6 +36,7 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 
 /**
@@ -220,11 +221,40 @@
      * @param expression the expression on which to compare messages in order
      * @return the builder
      */
+    public ResequencerBuilder resequencer(Expression<Exchange> expression) {
+        return resequencer(Collections.<Expression<Exchange>>singletonList(expression));
+    }
+
+    /**
+     * A builder for the <a href="http://activemq.apache.org/camel/resequencer.html">Resequencer</a>
pattern
+     * where a list of expressions are evaluated to be able to compare the message exchanges
to reorder them. e.g. you
+     * may wish to sort by some headers
+     *
+     * @param expressions the expressions on which to compare messages in order
+     * @return the builder
+     */
     @Fluent
-    public ResequencerBuilder resequencer(@FluentArg(value = "expression")Expression expression)
{
-        ResequencerBuilder answer = new ResequencerBuilder(this, expression);
-        setRouteBuilder(answer);        
+    public ResequencerBuilder resequencer(@FluentArg(value = "expressions")List<Expression<Exchange>>
expressions) {
+        ResequencerBuilder answer = new ResequencerBuilder(this, expressions);
+        setRouteBuilder(answer);
         return answer;
+    }
+
+    /**
+     * A builder for the <a href="http://activemq.apache.org/camel/resequencer.html">Resequencer</a>
pattern
+     * where a list of expressions are evaluated to be able to compare the message exchanges
to reorder them. e.g. you
+     * may wish to sort by some headers
+     *
+     * @param expressions the expressions on which to compare messages in order
+     * @return the builder
+     */
+    @Fluent
+    public ResequencerBuilder resequencer(Expression<Exchange>... expressions) {
+        List<Expression<Exchange>> list = new ArrayList<Expression<Exchange>>();
+        for (Expression<Exchange> expression : expressions) {
+            list.add(expression);
+        }
+        return resequencer(list);
     }
 
     /**

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ResequencerBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ResequencerBuilder.java?view=diff&rev=545076&r1=545075&r2=545076
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ResequencerBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ResequencerBuilder.java
Wed Jun  6 22:56:05 2007
@@ -30,19 +30,19 @@
  * @version $Revision: 1.1 $
  */
 public class ResequencerBuilder extends FromBuilder {
-    private final Expression expression;
+    private final List<Expression<Exchange>> expressions;
     private long batchTimeout = 1000L;
     private int batchSize = 100;
 
-    public ResequencerBuilder(FromBuilder builder, Expression expression) {
+    public ResequencerBuilder(FromBuilder builder, List<Expression<Exchange>>
expressions) {
         super(builder);
-        this.expression = expression;
+        this.expressions = expressions;
     }
 
     @Override
     public Route createRoute() throws Exception {
         final Processor processor = super.createProcessor();
-        final Resequencer resequencer = new Resequencer(getFrom(), processor, expression);
+        final Resequencer resequencer = new Resequencer(getFrom(), processor, expressions);
 
         return new Route<Exchange>(getFrom()) {
             protected void addServices(List<Service> list) throws Exception {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Resequencer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Resequencer.java?view=diff&rev=545076&r1=545075&r2=545076
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Resequencer.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/Resequencer.java
Wed Jun  6 22:56:05 2007
@@ -26,12 +26,14 @@
 import org.apache.camel.impl.ServiceSupport;
 import org.apache.camel.spi.ExceptionHandler;
 import org.apache.camel.util.ExpressionComparator;
+import org.apache.camel.util.ExpressionListComparator;
 import org.apache.camel.util.ServiceHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 import java.util.Comparator;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 import java.util.TreeSet;
 
@@ -54,6 +56,10 @@
         this(endpoint, processor, createSet(expression));
     }
 
+    public Resequencer(Endpoint endpoint, Processor processor, List<Expression<Exchange>>
expressions) {
+        this(endpoint, processor, createSet(expressions));
+    }
+
     public Resequencer(Endpoint endpoint, Processor processor, Set<Exchange> set) {
         this.endpoint = endpoint;
         this.processor = processor;
@@ -155,7 +161,17 @@
     }
 
     protected static Set<Exchange> createSet(Expression<Exchange> expression)
{
-        Comparator<? super Exchange> comparator = new ExpressionComparator<Exchange>(expression);
+        return createSet(new ExpressionComparator<Exchange>(expression));
+    }
+
+    protected static Set<Exchange> createSet(List<Expression<Exchange>>
expressions) {
+        if (expressions.size() == 1) {
+            return createSet(expressions.get(0));
+        }
+        return createSet(new ExpressionListComparator<Exchange>(expressions));
+    }
+
+    protected static Set<Exchange> createSet(Comparator<? super Exchange> comparator)
{
         return new TreeSet<Exchange>(comparator);
     }
 }

Copied: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExpressionListComparator.java
(from r541717, activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExpressionComparator.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExpressionListComparator.java?view=diff&rev=545076&p1=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExpressionComparator.java&r1=541717&p2=activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExpressionListComparator.java&r2=545076
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExpressionComparator.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExpressionListComparator.java
Wed Jun  6 22:56:05 2007
@@ -21,23 +21,31 @@
 import org.apache.camel.Expression;
 
 import java.util.Comparator;
+import java.util.List;
 
 /**
- * An implementation of {@link Comparator} which takes an {@link Expression} which is evaluated
- * on each exchange to compare
- *  
+ * An implementation of {@link java.util.Comparator} which takes a list of
+ * {@link org.apache.camel.Expression} objects which is evaluated
+ * on each exchange to compare them
+ *
  * @version $Revision: 1.1 $
  */
-public class ExpressionComparator<E extends Exchange> implements Comparator<E>
{
-    private final Expression<E> expression;
+public class ExpressionListComparator<E extends Exchange> implements Comparator<E>
{
+    private final List<Expression<E>> expressions;
 
-    public ExpressionComparator(Expression<E> expression) {
-        this.expression = expression;
+    public ExpressionListComparator(List<Expression<E>> expressions) {
+        this.expressions = expressions;
     }
 
     public int compare(E e1, E e2) {
-        Object o1 = expression.evaluate(e1);
-        Object o2 = expression.evaluate(e2);
-        return ObjectHelper.compare(o1, o2);
+        for (Expression<E> expression : expressions) {
+            Object o1 = expression.evaluate(e1);
+            Object o2 = expression.evaluate(e2);
+            int answer = ObjectHelper.compare(o1, o2);
+            if (answer != 0) {
+                return answer;
+            }
+        }
+        return 0;
     }
-}
+}
\ No newline at end of file



Mime
View raw message