Repository: yegor256/cactoos
Pull request #51 (2017-05-29 15:40:36)

#50: FuncWithCallback
as per #50 
  
  * `FuncWithCallback` introduced


Patch:
diff --git a/src/main/java/org/cactoos/Func.java b/src/main/java/org/cactoos/Func.java
index 5c5042b2..46d48913 100644
--- a/src/main/java/org/cactoos/Func.java
+++ b/src/main/java/org/cactoos/Func.java
@@ -36,6 +36,7 @@ import java.util.concurrent.Callable;
  * @param <Y> Type of output
  * @since 0.1
  */
+@SuppressWarnings("PMD.TooManyMethods")
 public interface Func<X, Y> {
 
     /**
@@ -46,6 +47,31 @@ public interface Func<X, Y> {
      */
     Y apply(X input) throws Exception;
 
+    /**
+     * Func that doesn't throw.
+     * @param <X> Input type
+     * @param <Y> Output type
+     */
+    interface Safe<X, Y> extends Func<X, Y> {
+        /**
+         * Apply it safely.
+         * @param input The argument
+         * @return The result
+         */
+        @SuppressWarnings("PMD.AvoidCatchingGenericException")
+        default Y safeApply(X input) {
+            try {
+                return this.apply(input);
+            } catch (final InterruptedException ex) {
+                Thread.currentThread().interrupt();
+                throw new IllegalStateException(ex);
+                // @checkstyle IllegalCatchCheck (1 line)
+            } catch (final Exception ex) {
+                throw new IllegalStateException(ex);
+            }
+        }
+    }
+
     /**
      * Quiet func that returns nothing, but is still a function.
      * @param <X> Input type
@@ -82,15 +108,15 @@ public interface Func<X, Y> {
     /**
      * Func that really is a procedure that does something and returns nothing.
      */
-    interface Proc extends Func<Void, Void>, Runnable, Callable<Void> {
+    interface Proc extends Func<Boolean, Boolean>, Runnable, Callable<Boolean> {
         @Override
-        default Void apply(Void input) throws Exception {
+        default Boolean apply(Boolean input) throws Exception {
             return this.call();
         }
         @Override
-        default Void call() throws Exception {
+        default Boolean call() throws Exception {
             this.run();
-            return null;
+            return true;
         }
         @Override
         @SuppressWarnings("PMD.AvoidCatchingGenericException")
diff --git a/src/main/java/org/cactoos/func/FuncWithCallback.java b/src/main/java/org/cactoos/func/FuncWithCallback.java
new file mode 100644
index 00000000..2e0dc53e
--- /dev/null
+++ b/src/main/java/org/cactoos/func/FuncWithCallback.java
@@ -0,0 +1,78 @@
+/**
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Yegor Bugayenko
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package org.cactoos.func;
+
+import org.cactoos.Func;
+
+/**
+ * Func with Callback.
+ *
+ * <p>There is no thread-safety guarantee.
+ *
+ * @author Yegor Bugayenko (yegor256@gmail.com)
+ * @version $Id$
+ * @param <X> Type of input
+ * @param <Y> Type of output
+ * @since 0.2
+ */
+public final class FuncWithCallback<X, Y> implements Func<X, Y> {
+
+    /**
+     * The func.
+     */
+    private final Func<X, Y> func;
+
+    /**
+     * The callback.
+     */
+    private final Func<Throwable, Y> callback;
+
+    /**
+     * Ctor.
+     * @param fnc The func
+     * @param cbk The callback
+     */
+    public FuncWithCallback(final Func<X, Y> fnc,
+        final Func<Throwable, Y> cbk) {
+        this.func = fnc;
+        this.callback = cbk;
+    }
+
+    @Override
+    @SuppressWarnings("PMD.AvoidCatchingThrowable")
+    public Y apply(final X input) throws Exception {
+        Y result;
+        try {
+            result =  this.func.apply(input);
+        } catch (final InterruptedException ex) {
+            Thread.currentThread().interrupt();
+            result = this.callback.apply(ex);
+            // @checkstyle IllegalCatchCheck (1 line)
+        } catch (final Throwable ex) {
+            result = this.callback.apply(ex);
+        }
+        return result;
+    }
+
+}
diff --git a/src/main/java/org/cactoos/func/RunnableAsProc.java b/src/main/java/org/cactoos/func/RunnableAsProc.java
new file mode 100644
index 00000000..114f51a3
--- /dev/null
+++ b/src/main/java/org/cactoos/func/RunnableAsProc.java
@@ -0,0 +1,57 @@
+/**
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Yegor Bugayenko
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package org.cactoos.func;
+
+import org.cactoos.Func;
+
+/**
+ * Runnable as a Func.
+ *
+ * <p>There is no thread-safety guarantee.
+ *
+ * @author Yegor Bugayenko (yegor256@gmail.com)
+ * @version $Id$
+ * @since 0.2
+ */
+public final class RunnableAsProc implements Func.Proc {
+
+    /**
+     * The runnable.
+     */
+    private final Runnable runnable;
+
+    /**
+     * Ctor.
+     * @param rnbl The runnable
+     */
+    public RunnableAsProc(final Runnable rnbl) {
+        this.runnable = rnbl;
+    }
+
+    @Override
+    public void exec() {
+        this.runnable.run();
+    }
+
+}


Test patch:
diff --git a/src/test/java/org/cactoos/func/FuncWithCallbackTest.java b/src/test/java/org/cactoos/func/FuncWithCallbackTest.java
new file mode 100644
index 00000000..53b1c48e
--- /dev/null
+++ b/src/test/java/org/cactoos/func/FuncWithCallbackTest.java
@@ -0,0 +1,73 @@
+/**
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Yegor Bugayenko
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package org.cactoos.func;
+
+import java.io.IOException;
+import org.cactoos.Func;
+import org.hamcrest.MatcherAssert;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+
+/**
+ * Test case for {@link FuncWithCallback}.
+ *
+ * @author Yegor Bugayenko (yegor256@gmail.com)
+ * @version $Id$
+ * @since 0.2
+ */
+public final class FuncWithCallbackTest {
+
+    /**
+     * FuncWithCallback can use main func.
+     * @throws Exception If some problem inside
+     */
+    @Test
+    public void usesMainFunc() throws Exception {
+        MatcherAssert.assertThat(
+            new FuncWithCallback<>(
+                (Func<Integer, String>) input -> "It's success",
+                ex -> "In case of failure..."
+            ).apply(1),
+            Matchers.containsString("success")
+        );
+    }
+
+    /**
+     * FuncWithCallback can use a callback.
+     * @throws Exception If some problem inside
+     */
+    @Test
+    public void usesCallback() throws Exception {
+        MatcherAssert.assertThat(
+            new FuncWithCallback<>(
+                (Func<Integer, String>) input -> {
+                    throw new IOException("Failure");
+                },
+                ex -> "Never mind"
+            ).apply(1),
+            Matchers.containsString("Never")
+        );
+    }
+
+}
diff --git a/src/test/java/org/cactoos/func/package-info.java b/src/test/java/org/cactoos/func/package-info.java
new file mode 100644
index 00000000..2399f17c
--- /dev/null
+++ b/src/test/java/org/cactoos/func/package-info.java
@@ -0,0 +1,32 @@
+/**
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Yegor Bugayenko
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+/**
+ * Functions and procedures, tests.
+ *
+ * @author Yegor Bugayenko (yegor256@gmail.com)
+ * @version $Id$
+ * @since 0.2
+ */
+package org.cactoos.func;