jdk/src/java.base/share/classes/java/util/stream/LongStream.java
changeset 36223 de2976b3614c
parent 35302 e4d2275861c3
child 38442 387a4457880c
equal deleted inserted replaced
36222:dfed693e648b 36223:de2976b3614c
     1 /*
     1 /*
     2  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   877      *          a new element
   877      *          a new element
   878      * @return a new sequential {@code LongStream}
   878      * @return a new sequential {@code LongStream}
   879      */
   879      */
   880     public static LongStream iterate(final long seed, final LongUnaryOperator f) {
   880     public static LongStream iterate(final long seed, final LongUnaryOperator f) {
   881         Objects.requireNonNull(f);
   881         Objects.requireNonNull(f);
   882         final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
   882         Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE,
   883             long t = seed;
   883                Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
       
   884             long prev;
       
   885             boolean started;
   884 
   886 
   885             @Override
   887             @Override
   886             public boolean hasNext() {
   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);
   887                 return true;
   898                 return true;
   888             }
   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 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;
   889 
   941 
   890             @Override
   942             @Override
   891             public long nextLong() {
   943             public boolean tryAdvance(LongConsumer action) {
   892                 long v = t;
   944                 Objects.requireNonNull(action);
   893                 t = f.applyAsLong(t);
   945                 if (finished)
   894                 return v;
   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                 }
   895             }
   973             }
   896         };
   974         };
   897         return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
   975         return StreamSupport.longStream(spliterator, false);
   898                 iterator,
       
   899                 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
       
   900     }
   976     }
   901 
   977 
   902     /**
   978     /**
   903      * Returns an infinite sequential unordered stream where each element is
   979      * Returns an infinite sequential unordered stream where each element is
   904      * generated by the provided {@code LongSupplier}.  This is suitable for
   980      * generated by the provided {@code LongSupplier}.  This is suitable for