You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/12/24 09:46:09 UTC

[6/6] camel git commit: Refactor to improve readability of comparisons

Refactor to improve readability of comparisons


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

Branch: refs/heads/master
Commit: 0570a6ca30fa3bf20a3606d91b4e021ecc9c6aa6
Parents: 8e05657
Author: Candle <ca...@candle.me.uk>
Authored: Mon Dec 21 18:44:33 2015 +0000
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Dec 24 09:45:32 2015 +0100

----------------------------------------------------------------------
 .../aws/ddbstream/AtAfterCondition.java         | 44 -------------
 .../aws/ddbstream/BigIntComparisons.java        | 44 +++++++++++++
 .../aws/ddbstream/DdbStreamConsumer.java        | 15 ++---
 .../component/aws/ddbstream/ShardList.java      |  6 +-
 .../aws/ddbstream/AtAfterConditionTest.java     | 67 --------------------
 .../aws/ddbstream/BigIntComparisonsTest.java    | 67 ++++++++++++++++++++
 6 files changed, 121 insertions(+), 122 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0570a6ca/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/AtAfterCondition.java
----------------------------------------------------------------------
diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/AtAfterCondition.java b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/AtAfterCondition.java
deleted file mode 100644
index a6798b5..0000000
--- a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/AtAfterCondition.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.camel.component.aws.ddbstream;
-
-import java.math.BigInteger;
-
-interface AtAfterCondition {
-
-    /**
-     * @return true if sequenceNumber is (at,after) the endpointSequenceNumber.
-     */
-    boolean matches(BigInteger endpointSequenceNumber, BigInteger sequenceNumber);
-
-    static enum Conditions implements AtAfterCondition {
-        AFTER() {
-            @Override
-            public boolean matches(BigInteger endpointSequenceNumber, BigInteger sequenceNumber) {
-                return endpointSequenceNumber.compareTo(sequenceNumber) < 0;
-            }
-        },
-
-        AT() {
-            @Override
-            public boolean matches(BigInteger endpointSequenceNumber, BigInteger sequenceNumber) {
-                return endpointSequenceNumber.compareTo(sequenceNumber) <= 0;
-            }
-        }
-        // TODO rename to LT/LTEQ/EQ/GTEQ/GT
-    }
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/0570a6ca/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/BigIntComparisons.java
----------------------------------------------------------------------
diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/BigIntComparisons.java b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/BigIntComparisons.java
new file mode 100644
index 0000000..a06bd0a
--- /dev/null
+++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/BigIntComparisons.java
@@ -0,0 +1,44 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.aws.ddbstream;
+
+import java.math.BigInteger;
+
+interface BigIntComparisons {
+
+    /**
+     * @return true if the first parameter is LT/LTEQ/EQ/GTEQ/GT the second
+     */
+    boolean matches(BigInteger first, BigInteger second);
+
+    static enum Conditions implements BigIntComparisons {
+        LT() {
+            @Override
+            public boolean matches(BigInteger first, BigInteger second) {
+                return first.compareTo(second) < 0;
+            }
+        },
+
+        LTEQ() {
+            @Override
+            public boolean matches(BigInteger first, BigInteger second) {
+                return first.compareTo(second) <= 0;
+            }
+        }
+        // TODO Add EQ/GTEQ/GT as needed, but note that GTEQ == !LT and GT == !LTEQ and EQ == (!LT && !GT)
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/0570a6ca/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/DdbStreamConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/DdbStreamConsumer.java b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/DdbStreamConsumer.java
index d520abf..aa8224f 100644
--- a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/DdbStreamConsumer.java
+++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/DdbStreamConsumer.java
@@ -140,18 +140,17 @@ public class DdbStreamConsumer extends ScheduledBatchPollingConsumer {
                 // if you request with a sequence number that is LESS than the
                 // start of the shard, you get a HTTP 400 from AWS.
                 // So only add the sequence number if the endpoints
-                // sequence number is AT or AFTER the starting sequence for
-                // the shard.
+                // sequence number is less than or equal to the starting
+                // sequence for the shard.
                 // Otherwise change the shart iterator type to trim_horizon
                 // because we get a 400 when we use one of the
                 // {at,after}_sequence_number iterator types and don't supply
                 // a sequence number.
-                if (AtAfterCondition.Conditions.AT.matches(
+                if (BigIntComparisons.Conditions.LTEQ.matches(
                         new BigInteger(currentShard.getSequenceNumberRange().getStartingSequenceNumber()),
                         new BigInteger(getEndpoint().getSequenceNumber())
                 )) {
-                    req = req.withSequenceNumber(getEndpoint().getSequenceNumber())
-                        .withShardIteratorType(getEndpoint().getIteratorType());
+                    req = req.withSequenceNumber(getEndpoint().getSequenceNumber());
                 } else {
                     req = req.withShardIteratorType(ShardIteratorType.TRIM_HORIZON);
                 }
@@ -167,15 +166,15 @@ public class DdbStreamConsumer extends ScheduledBatchPollingConsumer {
 
     private Queue<Exchange> createExchanges(List<Record> records) {
         Queue<Exchange> exchanges = new ArrayDeque<>();
-        AtAfterCondition condition;
+        BigIntComparisons condition;
         BigInteger providedSeqNum = null;
         switch(getEndpoint().getIteratorType()) {
         case AFTER_SEQUENCE_NUMBER:
-            condition = AtAfterCondition.Conditions.AFTER;
+            condition = BigIntComparisons.Conditions.LT;
             providedSeqNum = new BigInteger(getEndpoint().getSequenceNumberProvider().getSequenceNumber());
             break;
         case AT_SEQUENCE_NUMBER:
-            condition = AtAfterCondition.Conditions.AT;
+            condition = BigIntComparisons.Conditions.LTEQ;
             providedSeqNum = new BigInteger(getEndpoint().getSequenceNumberProvider().getSequenceNumber());
             break;
         default:

http://git-wip-us.apache.org/repos/asf/camel/blob/0570a6ca/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/ShardList.java
----------------------------------------------------------------------
diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/ShardList.java b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/ShardList.java
index 5c5bd29..8852ada 100644
--- a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/ShardList.java
+++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ddbstream/ShardList.java
@@ -79,14 +79,14 @@ class ShardList {
     }
 
     Shard afterSeq(String sequenceNumber) {
-        return atAfterSeq(sequenceNumber, AtAfterCondition.Conditions.AFTER);
+        return atAfterSeq(sequenceNumber, BigIntComparisons.Conditions.LT);
     }
 
     Shard atSeq(String sequenceNumber) {
-        return atAfterSeq(sequenceNumber, AtAfterCondition.Conditions.AT);
+        return atAfterSeq(sequenceNumber, BigIntComparisons.Conditions.LTEQ);
     }
 
-    Shard atAfterSeq(String sequenceNumber, AtAfterCondition condition) {
+    Shard atAfterSeq(String sequenceNumber, BigIntComparisons condition) {
         BigInteger atAfter = new BigInteger(sequenceNumber);
         List<Shard> sorted = new ArrayList<>();
         sorted.addAll(shards.values());

http://git-wip-us.apache.org/repos/asf/camel/blob/0570a6ca/components/camel-aws/src/test/java/org/apache/camel/component/aws/ddbstream/AtAfterConditionTest.java
----------------------------------------------------------------------
diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ddbstream/AtAfterConditionTest.java b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ddbstream/AtAfterConditionTest.java
deleted file mode 100644
index 53aee40..0000000
--- a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ddbstream/AtAfterConditionTest.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.camel.component.aws.ddbstream;
-
-import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-
-@RunWith(Parameterized.class)
-public class AtAfterConditionTest {
-
-
-    private final AtAfterCondition condition;
-    private final int smaller;
-    private final int bigger;
-    private final boolean result;
-
-    public AtAfterConditionTest(AtAfterCondition condition, int smaller, int bigger, boolean result) {
-        this.condition = condition;
-        this.smaller = smaller;
-        this.bigger = bigger;
-        this.result = result;
-    }
-
-    @Parameterized.Parameters
-    public static Collection<Object[]> parameters() {
-        List<Object[]> results = new ArrayList<>();
-
-        results.add(new Object[]{AtAfterCondition.Conditions.AFTER, 1, 5, true});
-        results.add(new Object[]{AtAfterCondition.Conditions.AT   , 1, 5, true});
-        results.add(new Object[]{AtAfterCondition.Conditions.AFTER, 1, 1, false});
-        results.add(new Object[]{AtAfterCondition.Conditions.AT   , 1, 1, true});
-        results.add(new Object[]{AtAfterCondition.Conditions.AFTER, 5, 1, false});
-        results.add(new Object[]{AtAfterCondition.Conditions.AT   , 5, 1, false});
-
-        return results;
-    }
-
-    @Test
-    public void test() throws Exception {
-        assertThat(condition.matches(BigInteger.valueOf(smaller), BigInteger.valueOf(bigger)), is(result));
-    }
-
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/0570a6ca/components/camel-aws/src/test/java/org/apache/camel/component/aws/ddbstream/BigIntComparisonsTest.java
----------------------------------------------------------------------
diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ddbstream/BigIntComparisonsTest.java b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ddbstream/BigIntComparisonsTest.java
new file mode 100644
index 0000000..c881d41
--- /dev/null
+++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ddbstream/BigIntComparisonsTest.java
@@ -0,0 +1,67 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.aws.ddbstream;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+
+@RunWith(Parameterized.class)
+public class BigIntComparisonsTest {
+
+
+    private final BigIntComparisons condition;
+    private final int smaller;
+    private final int bigger;
+    private final boolean result;
+
+    public BigIntComparisonsTest(BigIntComparisons condition, int smaller, int bigger, boolean result) {
+        this.condition = condition;
+        this.smaller = smaller;
+        this.bigger = bigger;
+        this.result = result;
+    }
+
+    @Parameterized.Parameters
+    public static Collection<Object[]> parameters() {
+        List<Object[]> results = new ArrayList<>();
+
+        results.add(new Object[]{BigIntComparisons.Conditions.LT  , 1, 5, true});
+        results.add(new Object[]{BigIntComparisons.Conditions.LTEQ, 1, 5, true});
+        results.add(new Object[]{BigIntComparisons.Conditions.LT  , 1, 1, false});
+        results.add(new Object[]{BigIntComparisons.Conditions.LTEQ, 1, 1, true});
+        results.add(new Object[]{BigIntComparisons.Conditions.LT  , 5, 1, false});
+        results.add(new Object[]{BigIntComparisons.Conditions.LTEQ, 5, 1, false});
+
+        return results;
+    }
+
+    @Test
+    public void test() throws Exception {
+        assertThat(condition.matches(BigInteger.valueOf(smaller), BigInteger.valueOf(bigger)), is(result));
+    }
+
+}
\ No newline at end of file