src/share/classes/java/util/stream/Streams.java

Print this page
rev 7627 : 8015315: Stream.concat methods
Reviewed-by: psandoz
Contributed-by: brian.goetz@oracle.com, henry.jen@oracle.com
rev 7630 : 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
Reviewed-by: henryjen
rev 7631 : 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
Reviewed-by: psandoz
Contributed-by: brian goetz <brian.goetz@oracle.com>
rev 7633 : 8017513: Support for closeable streams
Reviewed-by:
Contributed-by: brian.goetz@oracle.com

@@ -23,10 +23,11 @@
  * questions.
  */
 package java.util.stream;
 
 import java.util.Comparator;
+import java.util.MayHoldCloseableResource;
 import java.util.Spliterator;
 import java.util.function.Consumer;
 import java.util.function.DoubleConsumer;
 import java.util.function.IntConsumer;
 import java.util.function.LongConsumer;

@@ -808,6 +809,63 @@
             OfDouble(Spliterator.OfDouble aSpliterator, Spliterator.OfDouble bSpliterator) {
                 super(aSpliterator, bSpliterator);
             }
         }
     }
+
+    /**
+     * Given two Runnables, return a Runnable that executes both in sequence,
+     * even if the first throws an exception, and if both throw exceptions, add
+     * any exceptions thrown by the second as suppressed exceptions of the first.
+     */
+    static Runnable composeWithExceptions(Runnable a, Runnable b) {
+        return new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    a.run();
+                }
+                catch (Error|RuntimeException e1) {
+                    try {
+                        b.run();
+                    }
+                    catch (Error|RuntimeException e2) {
+                        e1.addSuppressed(e2);
+                    }
+                    finally {
+                        throw e1;
+                    }
+                }
+                b.run();
+            }
+        };
+    }
+
+    /**
+     * Given two MayHoldCloseableResource objects, return a Runnable that
+     * executes both of their close methods in sequence,
+     * even if the first throws an exception, and if both throw exceptions, add
+     * any exceptions thrown by the second as suppressed exceptions of the first.
+     */
+    static Runnable composedClose(MayHoldCloseableResource a, MayHoldCloseableResource b) {
+        return new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    a.close();
+                }
+                catch (Error|RuntimeException e1) {
+                    try {
+                        b.close();
+                    }
+                    catch (Error|RuntimeException e2) {
+                        e1.addSuppressed(e2);
+                    }
+                    finally {
+                        throw e1;
+                    }
+                }
+                b.close();
+            }
+        };
+    }
 }