JAMES-2183 Add CommutativityChecker class

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

Branch: refs/heads/master
Commit: d1238fc24ea0894634299ddd9b89531a0ae3c2c9
Parents: 747e3f1
Author: quynhn <[email protected]>
Authored: Fri Oct 13 11:45:36 2017 +0700
Committer: benwa <[email protected]>
Committed: Wed Oct 18 09:00:38 2017 +0700

----------------------------------------------------------------------
 pom.xml                                         |   5 +
 server/container/util-java8/pom.xml             |   5 +
 .../apache/james/util/CommutativityChecker.java |  60 +++++++++++
 .../james/util/CommutativityCheckerTest.java    | 101 +++++++++++++++++++
 server/protocols/jmap/pom.xml                   |   5 +
 5 files changed, 176 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d1238fc2/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 11d1d90..fa6b127 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1419,6 +1419,11 @@
                 <version>2.6.3</version>
             </dependency>
             <dependency>
+                <groupId>com.github.dpaukov</groupId>
+                <artifactId>combinatoricslib3</artifactId>
+                <version>3.2.0</version>
+            </dependency>
+            <dependency>
                 <groupId>com.github.fge</groupId>
                 <artifactId>throwing-lambdas</artifactId>
                 <version>0.5.0</version>

http://git-wip-us.apache.org/repos/asf/james-project/blob/d1238fc2/server/container/util-java8/pom.xml
----------------------------------------------------------------------
diff --git a/server/container/util-java8/pom.xml 
b/server/container/util-java8/pom.xml
index 43bfcf3..c00ebe2 100644
--- a/server/container/util-java8/pom.xml
+++ b/server/container/util-java8/pom.xml
@@ -83,6 +83,11 @@
             <artifactId>testcontainers</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>com.github.dpaukov</groupId>
+            <artifactId>combinatoricslib3</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>

http://git-wip-us.apache.org/repos/asf/james-project/blob/d1238fc2/server/container/util-java8/src/test/java/org/apache/james/util/CommutativityChecker.java
----------------------------------------------------------------------
diff --git 
a/server/container/util-java8/src/test/java/org/apache/james/util/CommutativityChecker.java
 
b/server/container/util-java8/src/test/java/org/apache/james/util/CommutativityChecker.java
new file mode 100644
index 0000000..5620af5
--- /dev/null
+++ 
b/server/container/util-java8/src/test/java/org/apache/james/util/CommutativityChecker.java
@@ -0,0 +1,60 @@
+/****************************************************************
+ * 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.james.util;
+
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.BinaryOperator;
+import java.util.stream.Stream;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.paukov.combinatorics3.Generator;
+
+import com.github.steveash.guavate.Guavate;
+import com.google.common.base.Preconditions;
+
+public class CommutativityChecker<T> {
+    private final Set<T> valuesToTest;
+    private final BinaryOperator<T> operationToTest;
+
+    public CommutativityChecker(Set<T> valuesToTest, BinaryOperator<T> 
operationToTest) {
+        Preconditions.checkNotNull(valuesToTest);
+        Preconditions.checkNotNull(operationToTest);
+        Preconditions.checkArgument(valuesToTest.size() > 1, "You must to pass 
more than one value to check commutativity");
+        this.valuesToTest = valuesToTest;
+        this.operationToTest = operationToTest;
+    }
+
+    public Set<Pair<T, T>> findNonCommutativeInput() {
+        return Generator.combination(valuesToTest)
+            .simple(2)
+            .stream()
+            .map(list -> Pair.of(list.get(0), list.get(1)))
+            .filter(this::isNotCommutative)
+            .collect(Guavate.toImmutableSet());
+    }
+
+    private boolean isNotCommutative(Pair<T, T> pair) {
+        T leftThenRight = operationToTest.apply(pair.getLeft(), 
pair.getRight());
+        T rightThenLeft = operationToTest.apply(pair.getRight(), 
pair.getLeft());
+        return !leftThenRight.equals(rightThenLeft);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/d1238fc2/server/container/util-java8/src/test/java/org/apache/james/util/CommutativityCheckerTest.java
----------------------------------------------------------------------
diff --git 
a/server/container/util-java8/src/test/java/org/apache/james/util/CommutativityCheckerTest.java
 
b/server/container/util-java8/src/test/java/org/apache/james/util/CommutativityCheckerTest.java
new file mode 100644
index 0000000..a77b8e5
--- /dev/null
+++ 
b/server/container/util-java8/src/test/java/org/apache/james/util/CommutativityCheckerTest.java
@@ -0,0 +1,101 @@
+/****************************************************************
+ * 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.james.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Set;
+import java.util.function.BinaryOperator;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.junit.Test;
+import org.testcontainers.shaded.com.google.common.collect.ImmutableSet;
+
+public class CommutativityCheckerTest {
+    @Test
+    public void constructorShouldThrowWhenNullValuesToTest() throws Exception {
+        BinaryOperator<Integer> binaryOperator = (a, b) -> a * a + b;
+
+        assertThatThrownBy(() -> new CommutativityChecker<>(null, 
binaryOperator))
+            .isInstanceOf(NullPointerException.class);
+    }
+
+    @Test
+    public void constructorShouldThrowWhenEmptyValuesToTest() throws Exception 
{
+        BinaryOperator<Integer> binaryOperator = (a, b) -> a * a + b;
+
+        assertThatThrownBy(() -> new CommutativityChecker<>(ImmutableSet.of(), 
binaryOperator))
+            .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    public void constructorShouldThrowWhenSingleValueToTest() throws Exception 
{
+        BinaryOperator<Integer> binaryOperator = (a, b) -> a * a + b;
+
+        assertThatThrownBy(() -> new 
CommutativityChecker<>(ImmutableSet.of(0), binaryOperator))
+            .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    public void constructorShouldThrowWhenNullOperation() throws Exception {
+        assertThatThrownBy(() -> new CommutativityChecker<>(ImmutableSet.of(0, 
1), null))
+            .isInstanceOf(NullPointerException.class);
+    }
+
+    @Test
+    public void 
findNonCommutativeInputShouldReturnEmptyWhenCommutativeOperation() throws 
Exception {
+        Set<Integer> integers = ImmutableSet.of(5, 4, 3, 2, 1);
+        BinaryOperator<Integer> commutativeOperator = (a, b) -> a + b;
+        CommutativityChecker<Integer> commutativityChecker = new 
CommutativityChecker<>(integers, commutativeOperator);
+
+        assertThat(commutativityChecker.findNonCommutativeInput()).isEmpty();
+    }
+
+    @Test
+    public void 
findNonCommutativeInputShouldReturnDataWhenNonCommutativeOperation() throws 
Exception {
+        Set<Integer> integers = ImmutableSet.of(2, 1);
+        BinaryOperator<Integer> nonCommutativeOperator = (a, b) -> 2 * a + b;
+        CommutativityChecker<Integer> commutativityChecker = new 
CommutativityChecker<>(integers, nonCommutativeOperator);
+
+        assertThat(commutativityChecker.findNonCommutativeInput())
+            .containsOnly(Pair.of(2, 1));
+    }
+
+    @Test
+    public void findNonCommutativeInputShouldNotReturnStableValues() throws 
Exception {
+        Set<Integer> integers = ImmutableSet.of(0, 1, 2);
+        BinaryOperator<Integer> nonCommutativeOperatorWithStableValues = (a, 
b) -> a * a + b;
+        CommutativityChecker<Integer> commutativityChecker = new 
CommutativityChecker<>(integers, nonCommutativeOperatorWithStableValues);
+
+        assertThat(commutativityChecker.findNonCommutativeInput())
+            .containsOnly(Pair.of(1, 2),
+                Pair.of(0, 2));
+    }
+
+    @Test
+    public void 
findNonCommutativeInputShouldReturnEmptyWhenNonCommutativeOperationButOnlyStableValues()
 throws Exception {
+        Set<Integer> stableValues = ImmutableSet.of(0, 1);
+        BinaryOperator<Integer> nonCommutativeOperatorWithStableValues = (a, 
b) -> a * a + b;
+        CommutativityChecker<Integer> commutativityChecker = new 
CommutativityChecker<>(stableValues, nonCommutativeOperatorWithStableValues);
+
+        assertThat(commutativityChecker.findNonCommutativeInput()).isEmpty();
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/d1238fc2/server/protocols/jmap/pom.xml
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/pom.xml b/server/protocols/jmap/pom.xml
index 52ea6f1..5cebbd0 100644
--- a/server/protocols/jmap/pom.xml
+++ b/server/protocols/jmap/pom.xml
@@ -143,6 +143,11 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>com.github.dpaukov</groupId>
+            <artifactId>combinatoricslib3</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>com.fasterxml.jackson.core</groupId>
             <artifactId>jackson-databind</artifactId>
         </dependency>


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to