< 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


 932     }
 933 
 934     /**
 935      * Returns an infinite sequential ordered {@code DoubleStream} produced by iterative
 936      * application of a function {@code f} to an initial element {@code seed},
 937      * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 938      * {@code f(f(seed))}, etc.
 939      *
 940      * <p>The first element (position {@code 0}) in the {@code DoubleStream}
 941      * will be the provided {@code seed}.  For {@code n > 0}, the element at
 942      * position {@code n}, will be the result of applying the function {@code f}
 943      *  to the element at position {@code n - 1}.
 944      *
 945      * @param seed the initial element
 946      * @param f a function to be applied to the previous element to produce
 947      *          a new element
 948      * @return a new sequential {@code DoubleStream}
 949      */
 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


 932     }
 933 
 934     /**
 935      * Returns an infinite sequential ordered {@code DoubleStream} produced by iterative
 936      * application of a function {@code f} to an initial element {@code seed},
 937      * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 938      * {@code f(f(seed))}, etc.
 939      *
 940      * <p>The first element (position {@code 0}) in the {@code DoubleStream}
 941      * will be the provided {@code seed}.  For {@code n > 0}, the element at
 942      * position {@code n}, will be the result of applying the function {@code f}
 943      *  to the element at position {@code n - 1}.
 944      *
 945      * @param seed the initial element
 946      * @param f a function to be applied to the previous element to produce
 947      *          a new element
 948      * @return a new sequential {@code DoubleStream}
 949      */
 950     public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) {
 951         Objects.requireNonNull(f);
 952         Spliterator.OfDouble spliterator = new Spliterators.AbstractDoubleSpliterator(Long.MAX_VALUE, 
 953                Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
 954             double prev;
 955             boolean started;
 956 
 957             @Override
 958             public boolean tryAdvance(DoubleConsumer action) {
 959                 Objects.requireNonNull(action);
 960                 double t;
 961                 if (started)
 962                     t = f.applyAsDouble(prev);
 963                 else {
 964                     t = seed;
 965                     started = true;
 966                 }
 967                 action.accept(prev = t);
 968                 return true;
 969             }
 970         };
 971         return StreamSupport.doubleStream(spliterator, false);
 972     }
 973 
 974     /**
 975      * Returns a sequential ordered {@code DoubleStream} produced by iterative
 976      * application of a function to an initial element, conditioned on 
 977      * satisfying the supplied predicate.  The stream terminates as soon as
 978      * the predicate function returns false.
 979      *
 980      * <p>
 981      * {@code DoubleStream.iterate} should produce the same sequence of
 982      * elements as produced by the corresponding for-loop:
 983      * <pre>{@code
 984      *     for (double index=seed; predicate.test(index); index = f.apply(index)) {
 985      *         ... 
 986      *     }
 987      * }</pre>
 988      *
 989      * <p>
 990      * The resulting sequence may be empty if the predicate does not hold on 
 991      * the seed value.  Otherwise the first element will be the supplied seed
 992      * value, the next element (if present) will be the result of applying the
 993      * function f to the seed value, and so on iteratively until the predicate
 994      * indicates that the stream should terminate.
 995      *
 996      * @param seed the initial element
 997      * @param predicate a predicate to apply to elements to determine when the 
 998      *          stream must terminate.
 999      * @param f a function to be applied to the previous element to produce
1000      *          a new element
1001      * @return a new sequential {@code DoubleStream}
1002      * @since 9
1003      */
1004     public static DoubleStream iterate(double seed, DoublePredicate predicate, DoubleUnaryOperator f) {
1005         Objects.requireNonNull(f);
1006         Objects.requireNonNull(predicate);
1007         Spliterator.OfDouble spliterator = new Spliterators.AbstractDoubleSpliterator(Long.MAX_VALUE, 
1008                Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
1009             double prev;
1010             boolean started, finished;
1011 
1012             @Override
1013             public boolean tryAdvance(DoubleConsumer action) {
1014                 Objects.requireNonNull(action);
1015                 if (finished)
1016                     return false;
1017                 double t;
1018                 if (started)
1019                     t = f.applyAsDouble(prev);
1020                 else {
1021                     t = seed;
1022                     started = true;
1023                 }
1024                 if (!predicate.test(t)) {
1025                     finished = true;
1026                     return false;
1027                 }
1028                 action.accept(prev = t);
1029                 return true;
1030             }
1031 
1032             @Override
1033             public void forEachRemaining(DoubleConsumer action) {
1034                 Objects.requireNonNull(action);
1035                 if (finished)
1036                     return;
1037                 finished = true;
1038                 double t = started ? f.applyAsDouble(prev) : seed;
1039                 while (predicate.test(t)) {
1040                     action.accept(t);
1041                     t = f.applyAsDouble(t);
1042                 }
1043             }
1044         };
1045         return StreamSupport.doubleStream(spliterator, false);


1046     }
1047 
1048     /**
1049      * Returns an infinite sequential unordered stream where each element is
1050      * generated by the provided {@code DoubleSupplier}.  This is suitable for
1051      * generating constant streams, streams of random elements, etc.
1052      *
1053      * @param s the {@code DoubleSupplier} for generated elements
1054      * @return a new infinite sequential unordered {@code DoubleStream}
1055      */
1056     public static DoubleStream generate(DoubleSupplier s) {
1057         Objects.requireNonNull(s);
1058         return StreamSupport.doubleStream(
1059                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfDouble(Long.MAX_VALUE, s), false);
1060     }
1061 
1062     /**
1063      * Creates a lazily concatenated stream whose elements are all the
1064      * elements of the first stream followed by all the elements of the
1065      * second stream.  The resulting stream is ordered if both


< prev index next >