< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2013, 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


 862     }
 863 
 864     /**
 865      * Returns an infinite sequential ordered {@code LongStream} produced by iterative
 866      * application of a function {@code f} to an initial element {@code seed},
 867      * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 868      * {@code f(f(seed))}, etc.
 869      *
 870      * <p>The first element (position {@code 0}) in the {@code LongStream} will
 871      * be the provided {@code seed}.  For {@code n > 0}, the element at position
 872      * {@code n}, will be the result of applying the function {@code f} to the
 873      * element at position {@code n - 1}.
 874      *
 875      * @param seed the initial element
 876      * @param f a function to be applied to the previous element to produce
 877      *          a new element
 878      * @return a new sequential {@code LongStream}
 879      */
 880     public static LongStream iterate(final long seed, final LongUnaryOperator f) {
 881         Objects.requireNonNull(f);
 882         final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
 883             long t = seed;


 884 
 885             @Override
 886             public boolean hasNext() {






































































 887                 return true;
 888             }
 889 
 890             @Override
 891             public long nextLong() {
 892                 long v = t;






 893                 t = f.applyAsLong(t);
 894                 return v;
 895             }
 896         };
 897         return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
 898                 iterator,
 899                 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
 900     }
 901 
 902     /**
 903      * Returns an infinite sequential unordered stream where each element is
 904      * generated by the provided {@code LongSupplier}.  This is suitable for
 905      * generating constant streams, streams of random elements, etc.
 906      *
 907      * @param s the {@code LongSupplier} for generated elements
 908      * @return a new infinite sequential unordered {@code LongStream}
 909      */
 910     public static LongStream generate(LongSupplier s) {
 911         Objects.requireNonNull(s);
 912         return StreamSupport.longStream(
 913                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfLong(Long.MAX_VALUE, s), false);
 914     }
 915 
 916     /**
 917      * Returns a sequential ordered {@code LongStream} from {@code startInclusive}
 918      * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of
 919      * {@code 1}.


   1 /*
   2  * Copyright (c) 2013, 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


 862     }
 863 
 864     /**
 865      * Returns an infinite sequential ordered {@code LongStream} produced by iterative
 866      * application of a function {@code f} to an initial element {@code seed},
 867      * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 868      * {@code f(f(seed))}, etc.
 869      *
 870      * <p>The first element (position {@code 0}) in the {@code LongStream} will
 871      * be the provided {@code seed}.  For {@code n > 0}, the element at position
 872      * {@code n}, will be the result of applying the function {@code f} to the
 873      * element at position {@code n - 1}.
 874      *
 875      * @param seed the initial element
 876      * @param f a function to be applied to the previous element to produce
 877      *          a new element
 878      * @return a new sequential {@code LongStream}
 879      */
 880     public static LongStream iterate(final long seed, final LongUnaryOperator f) {
 881         Objects.requireNonNull(f);
 882         Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE, 
 883                Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
 884             long prev;
 885             boolean started;
 886 
 887             @Override
 888             public boolean tryAdvance(LongConsumer action) {
 889                 Objects.requireNonNull(action);
 890                 long t;
 891                 if (started)
 892                     t = f.applyAsLong(prev);
 893                 else {
 894                     t = seed;
 895                     started = true;
 896                 }
 897                 action.accept(prev = t);
 898                 return true;
 899             }
 900         };
 901         return StreamSupport.longStream(spliterator, false);
 902     }
 903 
 904     /**
 905      * Returns a sequential ordered {@code LongStream} produced by iterative
 906      * application of a function to an initial element, conditioned on 
 907      * satisfying the supplied predicate.  The stream terminates as soon as
 908      * the predicate function returns false.
 909      *
 910      * <p>
 911      * {@code LongStream.iterate} should produce the same sequence of elements
 912      * as produced by the corresponding for-loop:
 913      * <pre>{@code
 914      *     for (long index=seed; predicate.test(index); index = f.apply(index)) { 
 915      *         ... 
 916      *     }
 917      * }</pre>
 918      *
 919      * <p>
 920      * The resulting sequence may be empty if the predicate does not hold on 
 921      * the seed value.  Otherwise the first element will be the supplied seed
 922      * value, the next element (if present) will be the result of applying the
 923      * function f to the seed value, and so on iteratively until the predicate
 924      * indicates that the stream should terminate.
 925      *
 926      * @param seed the initial element
 927      * @param predicate a predicate to apply to elements to determine when the 
 928      *          stream must terminate.
 929      * @param f a function to be applied to the previous element to produce
 930      *          a new element
 931      * @return a new sequential {@code LongStream}
 932      * @since 9
 933      */
 934     public static LongStream iterate(long seed, LongPredicate predicate, LongUnaryOperator f) {
 935         Objects.requireNonNull(f);
 936         Objects.requireNonNull(predicate);
 937         Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE, 
 938                Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
 939             long prev;
 940             boolean started, finished;
 941 
 942             @Override
 943             public boolean tryAdvance(LongConsumer action) {
 944                 Objects.requireNonNull(action);
 945                 if (finished)
 946                     return false;
 947                 long t;
 948                 if (started)
 949                     t = f.applyAsLong(prev);
 950                 else {
 951                     t = seed;
 952                     started = true;
 953                 }
 954                 if (!predicate.test(t)) {
 955                     finished = true;
 956                     return false;
 957                 }
 958                 action.accept(prev = t);
 959                 return true;
 960             }
 961 
 962             @Override
 963             public void forEachRemaining(LongConsumer action) {
 964                 Objects.requireNonNull(action);
 965                 if (finished)
 966                     return;
 967                 finished = true;
 968                 long t = started ? f.applyAsLong(prev) : seed;
 969                 while (predicate.test(t)) {
 970                     action.accept(t);
 971                     t = f.applyAsLong(t);
 972                 }
 973             }
 974         };
 975         return StreamSupport.longStream(spliterator, false);


 976     }
 977 
 978     /**
 979      * Returns an infinite sequential unordered stream where each element is
 980      * generated by the provided {@code LongSupplier}.  This is suitable for
 981      * generating constant streams, streams of random elements, etc.
 982      *
 983      * @param s the {@code LongSupplier} for generated elements
 984      * @return a new infinite sequential unordered {@code LongStream}
 985      */
 986     public static LongStream generate(LongSupplier s) {
 987         Objects.requireNonNull(s);
 988         return StreamSupport.longStream(
 989                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfLong(Long.MAX_VALUE, s), false);
 990     }
 991 
 992     /**
 993      * Returns a sequential ordered {@code LongStream} from {@code startInclusive}
 994      * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of
 995      * {@code 1}.


< prev index next >