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


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.util.stream;
  26 
  27 import java.util.Comparator;

  28 import java.util.Spliterator;
  29 import java.util.function.Consumer;
  30 import java.util.function.DoubleConsumer;
  31 import java.util.function.IntConsumer;
  32 import java.util.function.LongConsumer;
  33 
  34 /**
  35  * Utility methods for operating on and creating streams.
  36  *
  37  * <p>Unless otherwise stated, streams are created as sequential streams.  A
  38  * sequential stream can be transformed into a parallel stream by calling the
  39  * {@code parallel()} method on the created stream.
  40  *
  41  * @since 1.8
  42  */
  43 final class Streams {
  44 
  45     private Streams() {
  46         throw new Error("no instances");
  47     }


 792             OfInt(Spliterator.OfInt aSpliterator, Spliterator.OfInt bSpliterator) {
 793                 super(aSpliterator, bSpliterator);
 794             }
 795         }
 796 
 797         static class OfLong
 798                 extends ConcatSpliterator.OfPrimitive<Long, LongConsumer, Spliterator.OfLong>
 799                 implements Spliterator.OfLong {
 800             OfLong(Spliterator.OfLong aSpliterator, Spliterator.OfLong bSpliterator) {
 801                 super(aSpliterator, bSpliterator);
 802             }
 803         }
 804 
 805         static class OfDouble
 806                 extends ConcatSpliterator.OfPrimitive<Double, DoubleConsumer, Spliterator.OfDouble>
 807                 implements Spliterator.OfDouble {
 808             OfDouble(Spliterator.OfDouble aSpliterator, Spliterator.OfDouble bSpliterator) {
 809                 super(aSpliterator, bSpliterator);
 810             }
 811         }

























































 812     }
 813 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.util.stream;
  26 
  27 import java.util.Comparator;
  28 import java.util.MayHoldCloseableResource;
  29 import java.util.Spliterator;
  30 import java.util.function.Consumer;
  31 import java.util.function.DoubleConsumer;
  32 import java.util.function.IntConsumer;
  33 import java.util.function.LongConsumer;
  34 
  35 /**
  36  * Utility methods for operating on and creating streams.
  37  *
  38  * <p>Unless otherwise stated, streams are created as sequential streams.  A
  39  * sequential stream can be transformed into a parallel stream by calling the
  40  * {@code parallel()} method on the created stream.
  41  *
  42  * @since 1.8
  43  */
  44 final class Streams {
  45 
  46     private Streams() {
  47         throw new Error("no instances");
  48     }


 793             OfInt(Spliterator.OfInt aSpliterator, Spliterator.OfInt bSpliterator) {
 794                 super(aSpliterator, bSpliterator);
 795             }
 796         }
 797 
 798         static class OfLong
 799                 extends ConcatSpliterator.OfPrimitive<Long, LongConsumer, Spliterator.OfLong>
 800                 implements Spliterator.OfLong {
 801             OfLong(Spliterator.OfLong aSpliterator, Spliterator.OfLong bSpliterator) {
 802                 super(aSpliterator, bSpliterator);
 803             }
 804         }
 805 
 806         static class OfDouble
 807                 extends ConcatSpliterator.OfPrimitive<Double, DoubleConsumer, Spliterator.OfDouble>
 808                 implements Spliterator.OfDouble {
 809             OfDouble(Spliterator.OfDouble aSpliterator, Spliterator.OfDouble bSpliterator) {
 810                 super(aSpliterator, bSpliterator);
 811             }
 812         }
 813     }
 814 
 815     /**
 816      * Given two Runnables, return a Runnable that executes both in sequence,
 817      * even if the first throws an exception, and if both throw exceptions, add
 818      * any exceptions thrown by the second as suppressed exceptions of the first.
 819      */
 820     static Runnable composeWithExceptions(Runnable a, Runnable b) {
 821         return new Runnable() {
 822             @Override
 823             public void run() {
 824                 try {
 825                     a.run();
 826                 }
 827                 catch (Error|RuntimeException e1) {
 828                     try {
 829                         b.run();
 830                     }
 831                     catch (Error|RuntimeException e2) {
 832                         e1.addSuppressed(e2);
 833                     }
 834                     finally {
 835                         throw e1;
 836                     }
 837                 }
 838                 b.run();
 839             }
 840         };
 841     }
 842 
 843     /**
 844      * Given two MayHoldCloseableResource objects, return a Runnable that
 845      * executes both of their close methods in sequence,
 846      * even if the first throws an exception, and if both throw exceptions, add
 847      * any exceptions thrown by the second as suppressed exceptions of the first.
 848      */
 849     static Runnable composedClose(MayHoldCloseableResource a, MayHoldCloseableResource b) {
 850         return new Runnable() {
 851             @Override
 852             public void run() {
 853                 try {
 854                     a.close();
 855                 }
 856                 catch (Error|RuntimeException e1) {
 857                     try {
 858                         b.close();
 859                     }
 860                     catch (Error|RuntimeException e2) {
 861                         e1.addSuppressed(e2);
 862                     }
 863                     finally {
 864                         throw e1;
 865                     }
 866                 }
 867                 b.close();
 868             }
 869         };
 870     }
 871 }