Repository: yegor256/cactoos
Pull request #32 (2017-05-26 08:15:33)

31: Proc removed
as per #31: `Proc` removed


Patch:
diff --git a/README.md b/README.md
index 0402ab32..c2e97f60 100644
--- a/README.md
+++ b/README.md
@@ -143,9 +143,12 @@ To iterate a collection:
 new AllOf(
   new TransformedIterable<>(
     new ArrayAsIterable<>("how", "are", "you"),
-    new ProcAsFunc<>(
-      i -> System.out.printf("Item: %s\n", i)
-    )
+    new Func.Quiet<String>() {
+      @Override
+      public void exec(final String input) throws Exception {
+        System.out.printf("Item: %s\n", input);
+      }
+    }
   )
 ).asValue();
 ```
@@ -155,7 +158,7 @@ Or even more compact:
 ```java
 new IterableAsBoolean(
   new ArrayAsIterable<>("how", "are", "you"),
-  i -> System.out.printf("Item: %s\n", i)
+  (Func.Quiet<String>) i -> System.out.printf("Item: %s\n", i)
 ).asValue();
 ```
 
diff --git a/src/main/java/org/cactoos/Func.java b/src/main/java/org/cactoos/Func.java
index 00e67c8c..6ec98b8f 100644
--- a/src/main/java/org/cactoos/Func.java
+++ b/src/main/java/org/cactoos/Func.java
@@ -23,7 +23,7 @@
  */
 package org.cactoos;
 
-import java.io.IOException;
+import java.util.concurrent.Callable;
 
 /**
  * Function.
@@ -42,8 +42,71 @@ public interface Func<X, Y> {
      * Apply it.
      * @param input The argument
      * @return The result
-     * @throws IOException If fails
+     * @throws Exception If fails
      */
-    Y apply(X input) throws IOException;
+    Y apply(X input) throws Exception;
+
+    /**
+     * Quiet func that returns nothing, but is still a function.
+     * @param <X> Input type
+     */
+    interface Quiet<X> extends Func<X, Boolean> {
+        @Override
+        default Boolean apply(X input) throws Exception {
+            this.exec(input);
+            return true;
+        }
+        /**
+         * Execute it.
+         * @param input The argument
+         * @throws Exception If fails
+         */
+        void exec(X input) throws Exception;
+    }
+
+    /**
+     * Func that returns something but doesn't expect any input.
+     * @param <Y> Output type
+     */
+    interface Value<Y> extends Func<Void, Y>, Scalar<Y>, Callable<Y> {
+        @Override
+        default Y apply(Void input) throws Exception {
+            return this.asValue();
+        }
+        @Override
+        default Y call() throws Exception {
+            return this.asValue();
+        }
+    }
+
+    /**
+     * Func that really is a procedure that does something and returns nothing.
+     */
+    interface Proc extends Func<Void, Void>, Runnable, Callable<Void> {
+        @Override
+        default Void apply(Void input) throws Exception {
+            return this.call();
+        }
+        @Override
+        default Void call() throws Exception {
+            this.run();
+            return null;
+        }
+        @Override
+        @SuppressWarnings("PMD.AvoidCatchingGenericException")
+        default void run() {
+            try {
+                this.exec();
+                // @checkstyle IllegalCatchCheck (1 line)
+            } catch (final Exception ex) {
+                throw new IllegalStateException(ex);
+            }
+        }
+        /**
+         * Execute it.
+         * @throws Exception If fails
+         */
+        void exec() throws Exception;
+    }
 
 }
diff --git a/src/main/java/org/cactoos/Proc.java b/src/main/java/org/cactoos/Proc.java
deleted file mode 100644
index 2e3ac730..00000000
--- a/src/main/java/org/cactoos/Proc.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/**
- * 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;
-
-import java.io.IOException;
-
-/**
- * Procedure.
- *
- * <p>There is no thread-safety guarantee.
- *
- * @author Yegor Bugayenko (yegor256@gmail.com)
- * @version $Id$
- * @param <X> Type of input
- * @since 0.1
- */
-public interface Proc<X> {
-
-    /**
-     * Apply it.
-     * @param input The argument
-     * @throws IOException If fails
-     */
-    void apply(X input) throws IOException;
-
-}
diff --git a/src/main/java/org/cactoos/func/DeadProc.java b/src/main/java/org/cactoos/func/DeadProc.java
deleted file mode 100644
index b81510e4..00000000
--- a/src/main/java/org/cactoos/func/DeadProc.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/**
- * 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.Proc;
-
-/**
- * Proc that does nothing.
- *
- * <p>There is no thread-safety guarantee.
- *
- * @author Yegor Bugayenko (yegor256@gmail.com)
- * @version $Id$
- * @param <T> Type of items
- * @since 0.1
- */
-public final class DeadProc<T> implements Proc<T> {
-
-    @Override
-    public void apply(final T input) {
-        // nothing to do
-    }
-
-}
diff --git a/src/main/java/org/cactoos/func/ProcAsFunc.java b/src/main/java/org/cactoos/func/ProcAsFunc.java
deleted file mode 100644
index ca3ef2cf..00000000
--- a/src/main/java/org/cactoos/func/ProcAsFunc.java
+++ /dev/null
@@ -1,85 +0,0 @@
-/**
- * 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.cactoos.Proc;
-
-/**
- * Proc that is a {@link Func} that returns {@code boolean}.
- *
- * <p>You may need this class when you iterate a collection, for example:</p>
- *
- * <pre> new AllOf(
- *   new TransformedIterable&lt;String&gt;(
- *     Arrays.asList("hello", "world"),
- *     new ProcAsFunc(i -&gt; System.out.println(i))
- *   )
- * ).asValue();</pre>
- *
- * <p>There is no thread-safety guarantee.
- *
- * @author Yegor Bugayenko (yegor256@gmail.com)
- * @version $Id$
- * @param <T> Type of items
- * @since 0.1
- */
-public final class ProcAsFunc<T> implements Func<T, Boolean> {
-
-    /**
-     * Proc.
-     */
-    private final Proc<T> proc;
-
-    /**
-     * The result to return.
-     */
-    private final Boolean result;
-
-    /**
-     * Ctor.
-     * @param cns Consumer
-     */
-    public ProcAsFunc(final Proc<T> cns) {
-        this(cns, true);
-    }
-
-    /**
-     * Ctor.
-     * @param cns Consumer
-     * @param rslt Result to return
-     */
-    public ProcAsFunc(final Proc<T> cns, final Boolean rslt) {
-        this.proc = cns;
-        this.result = rslt;
-    }
-
-    @Override
-    public Boolean apply(final T input) throws IOException {
-        this.proc.apply(input);
-        return this.result;
-    }
-
-}
diff --git a/src/main/java/org/cactoos/list/StickyFunc.java b/src/main/java/org/cactoos/func/StickyFunc.java
similarity index 94%
rename from src/main/java/org/cactoos/list/StickyFunc.java
rename to src/main/java/org/cactoos/func/StickyFunc.java
index 0d7f7311..5a4645ec 100644
--- a/src/main/java/org/cactoos/list/StickyFunc.java
+++ b/src/main/java/org/cactoos/func/StickyFunc.java
@@ -21,9 +21,8 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-package org.cactoos.list;
+package org.cactoos.func;
 
-import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 import org.cactoos.Func;
@@ -62,7 +61,7 @@ public final class StickyFunc<X, Y> implements Func<X, Y> {
     }
 
     @Override
-    public Y apply(final X input) throws IOException {
+    public Y apply(final X input) throws Exception {
         if (!this.cache.containsKey(input)) {
             this.cache.put(input, this.func.apply(input));
         }
diff --git a/src/main/java/org/cactoos/list/ConcatenatedIterable.java b/src/main/java/org/cactoos/list/ConcatenatedIterable.java
index 01fdd2af..b2b2c898 100644
--- a/src/main/java/org/cactoos/list/ConcatenatedIterable.java
+++ b/src/main/java/org/cactoos/list/ConcatenatedIterable.java
@@ -25,6 +25,7 @@ package org.cactoos.list;
 
 import java.util.Arrays;
 import java.util.Iterator;
+import org.cactoos.func.StickyFunc;
 
 /**
  * A few Iterables joined together.
diff --git a/src/main/java/org/cactoos/list/FilteredIterator.java b/src/main/java/org/cactoos/list/FilteredIterator.java
index 0740ad36..7efc282e 100644
--- a/src/main/java/org/cactoos/list/FilteredIterator.java
+++ b/src/main/java/org/cactoos/list/FilteredIterator.java
@@ -23,7 +23,6 @@
  */
 package org.cactoos.list;
 
-import java.io.IOException;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.NoSuchElementException;
@@ -69,6 +68,7 @@ public final class FilteredIterator<X> implements Iterator<X> {
     }
 
     @Override
+    @SuppressWarnings("PMD.AvoidCatchingGenericException")
     public boolean hasNext() {
         if (this.buffer.isEmpty()) {
             while (this.iterator.hasNext()) {
@@ -78,7 +78,8 @@ public final class FilteredIterator<X> implements Iterator<X> {
                         this.buffer.add(object);
                         break;
                     }
-                } catch (final IOException ex) {
+                    // @checkstyle IllegalCatchCheck (1 line)
+                } catch (final Exception ex) {
                     throw new IllegalStateException(ex);
                 }
             }
diff --git a/src/main/java/org/cactoos/list/First.java b/src/main/java/org/cactoos/list/FirstOf.java
similarity index 94%
rename from src/main/java/org/cactoos/list/First.java
rename to src/main/java/org/cactoos/list/FirstOf.java
index 1788ace4..597a9048 100644
--- a/src/main/java/org/cactoos/list/First.java
+++ b/src/main/java/org/cactoos/list/FirstOf.java
@@ -35,7 +35,7 @@ import org.cactoos.Scalar;
  * @param <T> Scalar type
  * @since 0.1
  */
-public final class First<T> implements Scalar<T> {
+public final class FirstOf<T> implements Scalar<T> {
 
     /**
      * Source iterable.
@@ -47,7 +47,7 @@ public final class First<T> implements Scalar<T> {
      *
      * @param source Iterable
      */
-    public First(final Iterable<T> source) {
+    public FirstOf(final Iterable<T> source) {
         this.source = source;
     }
 
diff --git a/src/main/java/org/cactoos/list/IterableAsBoolean.java b/src/main/java/org/cactoos/list/IterableAsBoolean.java
index 41717f97..31b91f4e 100644
--- a/src/main/java/org/cactoos/list/IterableAsBoolean.java
+++ b/src/main/java/org/cactoos/list/IterableAsBoolean.java
@@ -24,9 +24,7 @@
 package org.cactoos.list;
 
 import org.cactoos.Func;
-import org.cactoos.Proc;
 import org.cactoos.Scalar;
-import org.cactoos.func.ProcAsFunc;
 
 /**
  * Iterable into boolean.
@@ -58,15 +56,6 @@ public final class IterableAsBoolean<X> implements Scalar<Boolean> {
      */
     private final Func<X, Boolean> func;
 
-    /**
-     * Ctor.
-     * @param src Source iterable
-     * @param prc Func
-     */
-    public IterableAsBoolean(final Iterable<X> src, final Proc<X> prc) {
-        this(src, new ProcAsFunc<>(prc));
-    }
-
     /**
      * Ctor.
      * @param src Source iterable
diff --git a/src/main/java/org/cactoos/list/IterableAsBooleans.java b/src/main/java/org/cactoos/list/IterableAsBooleans.java
index 43f26ebe..ee1efd9c 100644
--- a/src/main/java/org/cactoos/list/IterableAsBooleans.java
+++ b/src/main/java/org/cactoos/list/IterableAsBooleans.java
@@ -25,8 +25,6 @@ package org.cactoos.list;
 
 import java.util.Iterator;
 import org.cactoos.Func;
-import org.cactoos.Proc;
-import org.cactoos.func.ProcAsFunc;
 
 /**
  * Iterable into booleans.
@@ -62,15 +60,6 @@ public final class IterableAsBooleans<X> implements Iterable<Boolean> {
      */
     private final Func<X, Boolean> func;
 
-    /**
-     * Ctor.
-     * @param src Source iterable
-     * @param prc Func
-     */
-    public IterableAsBooleans(final Iterable<X> src, final Proc<X> prc) {
-        this(src, new ProcAsFunc<>(prc));
-    }
-
     /**
      * Ctor.
      * @param src Source iterable
diff --git a/src/main/java/org/cactoos/list/TransformedIterator.java b/src/main/java/org/cactoos/list/TransformedIterator.java
index 8dfb2caa..b4768525 100644
--- a/src/main/java/org/cactoos/list/TransformedIterator.java
+++ b/src/main/java/org/cactoos/list/TransformedIterator.java
@@ -23,7 +23,6 @@
  */
 package org.cactoos.list;
 
-import java.io.IOException;
 import java.util.Iterator;
 import org.cactoos.Func;
 
@@ -66,10 +65,12 @@ public final class TransformedIterator<X, Y> implements Iterator<Y> {
     }
 
     @Override
+    @SuppressWarnings("PMD.AvoidCatchingGenericException")
     public Y next() {
         try {
             return this.func.apply(this.iterator.next());
-        } catch (final IOException ex) {
+            // @checkstyle IllegalCatchCheck (1 line)
+        } catch (final Exception ex) {
             throw new IllegalStateException(ex);
         }
     }


Test patch:
diff --git a/src/test/java/org/cactoos/list/AllOfTest.java b/src/test/java/org/cactoos/list/AllOfTest.java
index f355f60f..0df90fa4 100644
--- a/src/test/java/org/cactoos/list/AllOfTest.java
+++ b/src/test/java/org/cactoos/list/AllOfTest.java
@@ -26,8 +26,7 @@ package org.cactoos.list;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
-import org.cactoos.Proc;
-import org.cactoos.func.ProcAsFunc;
+import org.cactoos.Func;
 import org.hamcrest.MatcherAssert;
 import org.hamcrest.Matchers;
 import org.junit.Test;
@@ -50,7 +49,7 @@ public final class AllOfTest {
             new AllOf(
                 new TransformedIterable<>(
                     new ArrayAsIterable<>("hello", "world"),
-                    new ProcAsFunc<>(list::add)
+                    (Func.Quiet<String>) list::add
                 )
             ).asValue(),
             Matchers.allOf(
@@ -70,7 +69,7 @@ public final class AllOfTest {
             new AllOf(
                 new IterableAsBooleans<>(
                     Collections.emptyList(),
-                    (Proc<String>) list::add
+                    (Func.Quiet<String>) list::add
                 )
             ).asValue(),
             Matchers.allOf(
diff --git a/src/test/java/org/cactoos/list/FirstTest.java b/src/test/java/org/cactoos/list/FirstOfTest.java
similarity index 93%
rename from src/test/java/org/cactoos/list/FirstTest.java
rename to src/test/java/org/cactoos/list/FirstOfTest.java
index 0364c22b..522cb894 100644
--- a/src/test/java/org/cactoos/list/FirstTest.java
+++ b/src/test/java/org/cactoos/list/FirstOfTest.java
@@ -31,13 +31,13 @@ import org.hamcrest.Matchers;
 import org.junit.Test;
 
 /**
- * Test case for {@link First}.
+ * Test case for {@link FirstOf}.
  *
  * @author Kirill (g4s8.public@gmail.com)
  * @version $Id$
  * @since 0.1
  */
-public final class FirstTest {
+public final class FirstOfTest {
 
     /**
      * Get first element in collection.
@@ -47,7 +47,7 @@ public final class FirstTest {
     @Test
     public void firstElementTest() throws Exception {
         MatcherAssert.assertThat(
-            new First<>(
+            new FirstOf<>(
                 // @checkstyle MagicNumber (1 line)
                 Arrays.asList(1, 2, 3)
             ).asValue(),
@@ -62,6 +62,6 @@ public final class FirstTest {
      */
     @Test(expected = IOException.class)
     public void failForEmptyCollectionTest() throws Exception {
-        new First<>(Collections.emptyList()).asValue();
+        new FirstOf<>(Collections.emptyList()).asValue();
     }
 }