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

Print this page
rev 7982 : 8017513: Support for closeable streams
8022237: j.u.s.BaseStream.onClose() has an issue in implementation or requires spec clarification
8022572: Same exception instances thrown from j.u.stream.Stream.onClose() handlers are not listed as suppressed
Summary: BaseStream implements AutoCloseable; Remove CloseableStream and DelegatingStream
Reviewed-by: alanb, mduigou, psandoz
Contributed-by: brian.goetz@oracle.com


 816                 super(aSpliterator, bSpliterator);
 817             }
 818         }
 819 
 820         static class OfLong
 821                 extends ConcatSpliterator.OfPrimitive<Long, LongConsumer, Spliterator.OfLong>
 822                 implements Spliterator.OfLong {
 823             OfLong(Spliterator.OfLong aSpliterator, Spliterator.OfLong bSpliterator) {
 824                 super(aSpliterator, bSpliterator);
 825             }
 826         }
 827 
 828         static class OfDouble
 829                 extends ConcatSpliterator.OfPrimitive<Double, DoubleConsumer, Spliterator.OfDouble>
 830                 implements Spliterator.OfDouble {
 831             OfDouble(Spliterator.OfDouble aSpliterator, Spliterator.OfDouble bSpliterator) {
 832                 super(aSpliterator, bSpliterator);
 833             }
 834         }
 835     }

























































 836 }


 816                 super(aSpliterator, bSpliterator);
 817             }
 818         }
 819 
 820         static class OfLong
 821                 extends ConcatSpliterator.OfPrimitive<Long, LongConsumer, Spliterator.OfLong>
 822                 implements Spliterator.OfLong {
 823             OfLong(Spliterator.OfLong aSpliterator, Spliterator.OfLong bSpliterator) {
 824                 super(aSpliterator, bSpliterator);
 825             }
 826         }
 827 
 828         static class OfDouble
 829                 extends ConcatSpliterator.OfPrimitive<Double, DoubleConsumer, Spliterator.OfDouble>
 830                 implements Spliterator.OfDouble {
 831             OfDouble(Spliterator.OfDouble aSpliterator, Spliterator.OfDouble bSpliterator) {
 832                 super(aSpliterator, bSpliterator);
 833             }
 834         }
 835     }
 836 
 837     /**
 838      * Given two Runnables, return a Runnable that executes both in sequence,
 839      * even if the first throws an exception, and if both throw exceptions, add
 840      * any exceptions thrown by the second as suppressed exceptions of the first.
 841      */
 842     static Runnable composeWithExceptions(Runnable a, Runnable b) {
 843         return new Runnable() {
 844             @Override
 845             public void run() {
 846                 try {
 847                     a.run();
 848                 }
 849                 catch (Throwable e1) {
 850                     try {
 851                         b.run();
 852                     }
 853                     catch (Throwable e2) {
 854                         try {
 855                             e1.addSuppressed(e2);
 856                         } catch (Throwable ignore) {}
 857                     }
 858                     throw e1;
 859                 }
 860                 b.run();
 861             }
 862         };
 863     }
 864 
 865     /**
 866      * Given two streams, return a Runnable that
 867      * executes both of their {@link BaseStream#close} methods in sequence,
 868      * even if the first throws an exception, and if both throw exceptions, add
 869      * any exceptions thrown by the second as suppressed exceptions of the first.
 870      */
 871     static Runnable composedClose(BaseStream<?, ?> a, BaseStream<?, ?> b) {
 872         return new Runnable() {
 873             @Override
 874             public void run() {
 875                 try {
 876                     a.close();
 877                 }
 878                 catch (Throwable e1) {
 879                     try {
 880                         b.close();
 881                     }
 882                     catch (Throwable e2) {
 883                         try {
 884                             e1.addSuppressed(e2);
 885                         } catch (Throwable ignore) {}
 886                     }
 887                     throw e1;
 888                 }
 889                 b.close();
 890             }
 891         };
 892     }
 893 }