You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/01/11 09:58:16 UTC
[3/8] james-project git commit: JAMES-2630 Add Functional utils
JAMES-2630 Add Functional utils
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/08c2f450
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/08c2f450
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/08c2f450
Branch: refs/heads/master
Commit: 08c2f450ad1151426ecc78010f407960a0d21834
Parents: ebab03a
Author: Gautier DI FOLCO <gd...@linagora.com>
Authored: Tue Jan 8 09:46:01 2019 +0100
Committer: Benoit Tellier <bt...@linagora.com>
Committed: Fri Jan 11 16:57:08 2019 +0700
----------------------------------------------------------------------
.../org/apache/james/util/FunctionalUtils.java | 37 ++++++++++
.../apache/james/util/FunctionalUtilsTest.java | 72 ++++++++++++++++++++
2 files changed, 109 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/08c2f450/server/container/util/src/main/java/org/apache/james/util/FunctionalUtils.java
----------------------------------------------------------------------
diff --git a/server/container/util/src/main/java/org/apache/james/util/FunctionalUtils.java b/server/container/util/src/main/java/org/apache/james/util/FunctionalUtils.java
new file mode 100644
index 0000000..54c8305
--- /dev/null
+++ b/server/container/util/src/main/java/org/apache/james/util/FunctionalUtils.java
@@ -0,0 +1,37 @@
+/****************************************************************
+ * 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.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.function.UnaryOperator;
+
+public class FunctionalUtils {
+ public static <T> UnaryOperator<T> toFunction(Consumer<T> consumer) {
+ return argument -> {
+ consumer.accept(argument);
+ return argument;
+ };
+ }
+
+ public static <T> Predicate<T> toPredicate(Function<T, Boolean> function) {
+ return value -> function.apply(value);
+ }
+}
http://git-wip-us.apache.org/repos/asf/james-project/blob/08c2f450/server/container/util/src/test/java/org/apache/james/util/FunctionalUtilsTest.java
----------------------------------------------------------------------
diff --git a/server/container/util/src/test/java/org/apache/james/util/FunctionalUtilsTest.java b/server/container/util/src/test/java/org/apache/james/util/FunctionalUtilsTest.java
new file mode 100644
index 0000000..8387a53
--- /dev/null
+++ b/server/container/util/src/test/java/org/apache/james/util/FunctionalUtilsTest.java
@@ -0,0 +1,72 @@
+/****************************************************************
+ * 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 org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Predicate;
+
+class FunctionalUtilsTest {
+
+ @Nested
+ class ToFunction {
+ @Test
+ void shouldCallConsumerAndReturnTheGivenParameter() {
+ Counter counter = new Counter(26);
+ Consumer<Integer> consumer = counter::increment;
+ Function<Integer, Integer> function = FunctionalUtils.toFunction(consumer);
+
+ assertThat(function.apply(16)).isEqualTo(16);
+ assertThat(counter.getCounter()).isEqualTo(42);
+ }
+
+ private class Counter {
+ private Integer counter;
+
+ public Counter(Integer counter) {
+ this.counter = counter;
+ }
+
+ public void increment(Integer other) {
+ counter += other;
+ }
+
+ public Integer getCounter() {
+ return counter;
+ }
+ }
+ }
+
+ @Nested
+ class ToPredicate {
+ @Test
+ void shouldKeepProperty() {
+ Function<Integer, Boolean> function = value -> value % 42 == 0;
+ Predicate<Integer> predicate = FunctionalUtils.toPredicate(function);
+
+ assertThat(predicate.test(5)).isFalse();
+ assertThat(predicate.test(42)).isTrue();
+ }
+ }
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org