< prev index next >

src/java.base/share/classes/java/util/stream/DoubleStream.java

Print this page


   1 /*
   2  * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   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


 950     public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) {
 951         Objects.requireNonNull(f);
 952         final PrimitiveIterator.OfDouble iterator = new PrimitiveIterator.OfDouble() {
 953             double t = seed;
 954 
 955             @Override
 956             public boolean hasNext() {
 957                 return true;
 958             }
 959 
 960             @Override
 961             public double nextDouble() {
 962                 double v = t;
 963                 t = f.applyAsDouble(t);
 964                 return v;
 965             }
 966         };
 967         return StreamSupport.doubleStream(Spliterators.spliteratorUnknownSize(
 968                 iterator,
 969                 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
































































 970     }
 971 
 972     /**
 973      * Returns an infinite sequential unordered stream where each element is
 974      * generated by the provided {@code DoubleSupplier}.  This is suitable for
 975      * generating constant streams, streams of random elements, etc.
 976      *
 977      * @param s the {@code DoubleSupplier} for generated elements
 978      * @return a new infinite sequential unordered {@code DoubleStream}
 979      */
 980     public static DoubleStream generate(DoubleSupplier s) {
 981         Objects.requireNonNull(s);
 982         return StreamSupport.doubleStream(
 983                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfDouble(Long.MAX_VALUE, s), false);
 984     }
 985 
 986     /**
 987      * Creates a lazily concatenated stream whose elements are all the
 988      * elements of the first stream followed by all the elements of the
 989      * second stream.  The resulting stream is ordered if both


   1 /*
   2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   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


 950     public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) {
 951         Objects.requireNonNull(f);
 952         final PrimitiveIterator.OfDouble iterator = new PrimitiveIterator.OfDouble() {
 953             double t = seed;
 954 
 955             @Override
 956             public boolean hasNext() {
 957                 return true;
 958             }
 959 
 960             @Override
 961             public double nextDouble() {
 962                 double v = t;
 963                 t = f.applyAsDouble(t);
 964                 return v;
 965             }
 966         };
 967         return StreamSupport.doubleStream(Spliterators.spliteratorUnknownSize(
 968                 iterator,
 969                 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
 970     }
 971 
 972     /**
 973      * Returns a sequential ordered {@code DoubleStream} produced by iterative
 974      * application of a function {@code f} to an initial element {@code seed},
 975      * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 976      * {@code f(f(seed))}, etc.  The stream terminates when {@code predicate}
 977      * returns false.
 978      *
 979      * <p>The first element (position {@code 0}) in the {@code DoubleStream} will be
 980      * the provided {@code seed}.  For {@code n > 0}, the element at position
 981      * {@code n}, will be the result of applying the function {@code f} to the
 982      * element at position {@code n - 1}.
 983      *
 984      * @param seed the initial element
 985      * @param predicate a predicate to apply to elements to determine when the 
 986      *          stream must terminate.
 987      * @param f a function to be applied to the previous element to produce
 988      *          a new element
 989      * @return a new sequential {@code DoubleStream}
 990      * @since 9
 991      */
 992     public static DoubleStream iterate(double seed, DoublePredicate predicate, DoubleUnaryOperator f) {
 993         Objects.requireNonNull(f);
 994         Objects.requireNonNull(predicate);
 995         Spliterator.OfDouble spliterator = new Spliterators.AbstractDoubleSpliterator(Long.MAX_VALUE, 
 996                Spliterator.ORDERED | Spliterator.IMMUTABLE) {
 997             double prev;
 998             boolean started, finished;
 999 
1000             @Override
1001             public boolean tryAdvance(DoubleConsumer action) {
1002                 Objects.requireNonNull(action);
1003                 if (finished)
1004                     return false;
1005                 double t;
1006                 if (started)
1007                     t = f.applyAsDouble(prev);
1008                 else {
1009                     t = seed;
1010                     started = true;
1011                 }
1012                 if (!predicate.test(t)) {
1013                     finished = true;
1014                     return false;
1015                 }
1016                 action.accept(prev = t);
1017                 return true;
1018             }
1019 
1020             @Override
1021             public void forEachRemaining(DoubleConsumer action) {
1022                 Objects.requireNonNull(action);
1023                 if (finished)
1024                     return;
1025                 double t = started ? f.applyAsDouble(prev) : seed;
1026                 finished = true;
1027                 while (predicate.test(t)) {
1028                     action.accept(t);
1029                     t = f.applyAsDouble(t);
1030                 }
1031             }
1032         };
1033         return StreamSupport.doubleStream(spliterator, false);
1034     }
1035 
1036     /**
1037      * Returns an infinite sequential unordered stream where each element is
1038      * generated by the provided {@code DoubleSupplier}.  This is suitable for
1039      * generating constant streams, streams of random elements, etc.
1040      *
1041      * @param s the {@code DoubleSupplier} for generated elements
1042      * @return a new infinite sequential unordered {@code DoubleStream}
1043      */
1044     public static DoubleStream generate(DoubleSupplier s) {
1045         Objects.requireNonNull(s);
1046         return StreamSupport.doubleStream(
1047                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfDouble(Long.MAX_VALUE, s), false);
1048     }
1049 
1050     /**
1051      * Creates a lazily concatenated stream whose elements are all the
1052      * elements of the first stream followed by all the elements of the
1053      * second stream.  The resulting stream is ordered if both


< prev index next >