jdk/src/java.base/share/classes/java/util/stream/LongStream.java
changeset 31644 dbca10d053fd
parent 29489 fe7624d92790
child 32002 e378c0dc767e
equal deleted inserted replaced
31643:abad00f2c027 31644:dbca10d053fd
     1 /*
     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     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.
     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
    22  * or visit www.oracle.com if you need additional information or have any
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 package java.util.stream;
    25 package java.util.stream;
    26 
    26 
    27 import java.nio.charset.Charset;
       
    28 import java.nio.file.Files;
       
    29 import java.nio.file.Path;
       
    30 import java.util.Arrays;
    27 import java.util.Arrays;
    31 import java.util.Collection;
       
    32 import java.util.LongSummaryStatistics;
    28 import java.util.LongSummaryStatistics;
    33 import java.util.Objects;
    29 import java.util.Objects;
    34 import java.util.OptionalDouble;
    30 import java.util.OptionalDouble;
    35 import java.util.OptionalLong;
    31 import java.util.OptionalLong;
    36 import java.util.PrimitiveIterator;
    32 import java.util.PrimitiveIterator;
    37 import java.util.Spliterator;
    33 import java.util.Spliterator;
    38 import java.util.Spliterators;
    34 import java.util.Spliterators;
    39 import java.util.concurrent.ConcurrentHashMap;
       
    40 import java.util.function.BiConsumer;
    35 import java.util.function.BiConsumer;
    41 import java.util.function.Function;
    36 import java.util.function.Function;
    42 import java.util.function.LongBinaryOperator;
    37 import java.util.function.LongBinaryOperator;
    43 import java.util.function.LongConsumer;
    38 import java.util.function.LongConsumer;
    44 import java.util.function.LongFunction;
    39 import java.util.function.LongFunction;
   276      * @throws IllegalArgumentException if {@code n} is negative
   271      * @throws IllegalArgumentException if {@code n} is negative
   277      */
   272      */
   278     LongStream skip(long n);
   273     LongStream skip(long n);
   279 
   274 
   280     /**
   275     /**
       
   276      * Returns, if this stream is ordered, a stream consisting of the longest
       
   277      * prefix of elements taken from this stream that match the given predicate.
       
   278      * Otherwise returns, if this stream is unordered, a stream consisting of a
       
   279      * subset of elements taken from this stream that match the given predicate.
       
   280      *
       
   281      * <p>If this stream is ordered then the longest prefix is a contiguous
       
   282      * sequence of elements of this stream that match the given predicate.  The
       
   283      * first element of the sequence is the first element of this stream, and
       
   284      * the element immediately following the last element of the sequence does
       
   285      * not match the given predicate.
       
   286      *
       
   287      * <p>If this stream is unordered, and some (but not all) elements of this
       
   288      * stream match the given predicate, then the behavior of this operation is
       
   289      * nondeterministic; it is free to take any subset of matching elements
       
   290      * (which includes the empty set).
       
   291      *
       
   292      * <p>Independent of whether this stream is ordered or unordered if all
       
   293      * elements of this stream match the given predicate then this operation
       
   294      * takes all elements (the result is the same is the input), or if no
       
   295      * elements of the stream match the given predicate then no elements are
       
   296      * taken (the result is an empty stream).
       
   297      *
       
   298      * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
       
   299      * stateful intermediate operation</a>.
       
   300      *
       
   301      * @implSpec
       
   302      * The default implementation obtains the {@link #spliterator() spliterator}
       
   303      * of this stream, wraps that spliterator so as to support the semantics
       
   304      * of this operation on traversal, and returns a new stream associated with
       
   305      * the wrapped spliterator.  The returned stream preserves the execution
       
   306      * characteristics of this stream (namely parallel or sequential execution
       
   307      * as per {@link #isParallel()}) but the wrapped spliterator may choose to
       
   308      * not support splitting.  When the returned stream is closed, the close
       
   309      * handlers for both the returned and this stream are invoked.
       
   310      *
       
   311      * @apiNote
       
   312      * While {@code takeWhile()} is generally a cheap operation on sequential
       
   313      * stream pipelines, it can be quite expensive on ordered parallel
       
   314      * pipelines, since the operation is constrained to return not just any
       
   315      * valid prefix, but the longest prefix of elements in the encounter order.
       
   316      * Using an unordered stream source (such as
       
   317      * {@link #generate(LongSupplier)}) or removing the ordering constraint with
       
   318      * {@link #unordered()} may result in significant speedups of
       
   319      * {@code takeWhile()} in parallel pipelines, if the semantics of your
       
   320      * situation permit.  If consistency with encounter order is required, and
       
   321      * you are experiencing poor performance or memory utilization with
       
   322      * {@code takeWhile()} in parallel pipelines, switching to sequential
       
   323      * execution with {@link #sequential()} may improve performance.
       
   324      *
       
   325      * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
       
   326      *                  <a href="package-summary.html#Statelessness">stateless</a>
       
   327      *                  predicate to apply to elements to determine the longest
       
   328      *                  prefix of elements.
       
   329      * @return the new stream
       
   330      */
       
   331     default LongStream takeWhile(LongPredicate predicate) {
       
   332         Objects.requireNonNull(predicate);
       
   333         // Reuses the unordered spliterator, which, when encounter is present,
       
   334         // is safe to use as long as it configured not to split
       
   335         return StreamSupport.longStream(
       
   336                 new WhileOps.UnorderedWhileSpliterator.OfLong.Taking(spliterator(), true, predicate),
       
   337                 isParallel()).onClose(this::close);
       
   338     }
       
   339 
       
   340     /**
       
   341      * Returns, if this stream is ordered, a stream consisting of the remaining
       
   342      * elements of this stream after dropping the longest prefix of elements
       
   343      * that match the given predicate.  Otherwise returns, if this stream is
       
   344      * unordered, a stream consisting of the remaining elements of this stream
       
   345      * after dropping a subset of elements that match the given predicate.
       
   346      *
       
   347      * <p>If this stream is ordered then the longest prefix is a contiguous
       
   348      * sequence of elements of this stream that match the given predicate.  The
       
   349      * first element of the sequence is the first element of this stream, and
       
   350      * the element immediately following the last element of the sequence does
       
   351      * not match the given predicate.
       
   352      *
       
   353      * <p>If this stream is unordered, and some (but not all) elements of this
       
   354      * stream match the given predicate, then the behavior of this operation is
       
   355      * nondeterministic; it is free to drop any subset of matching elements
       
   356      * (which includes the empty set).
       
   357      *
       
   358      * <p>Independent of whether this stream is ordered or unordered if all
       
   359      * elements of this stream match the given predicate then this operation
       
   360      * drops all elements (the result is an empty stream), or if no elements of
       
   361      * the stream match the given predicate then no elements are dropped (the
       
   362      * result is the same is the input).
       
   363      *
       
   364      * <p>This is a <a href="package-summary.html#StreamOps">stateful
       
   365      * intermediate operation</a>.
       
   366      *
       
   367      * @implSpec
       
   368      * The default implementation obtains the {@link #spliterator() spliterator}
       
   369      * of this stream, wraps that spliterator so as to support the semantics
       
   370      * of this operation on traversal, and returns a new stream associated with
       
   371      * the wrapped spliterator.  The returned stream preserves the execution
       
   372      * characteristics of this stream (namely parallel or sequential execution
       
   373      * as per {@link #isParallel()}) but the wrapped spliterator may choose to
       
   374      * not support splitting.  When the returned stream is closed, the close
       
   375      * handlers for both the returned and this stream are invoked.
       
   376      *
       
   377      * @apiNote
       
   378      * While {@code dropWhile()} is generally a cheap operation on sequential
       
   379      * stream pipelines, it can be quite expensive on ordered parallel
       
   380      * pipelines, since the operation is constrained to return not just any
       
   381      * valid prefix, but the longest prefix of elements in the encounter order.
       
   382      * Using an unordered stream source (such as
       
   383      * {@link #generate(LongSupplier)}) or removing the ordering constraint with
       
   384      * {@link #unordered()} may result in significant speedups of
       
   385      * {@code dropWhile()} in parallel pipelines, if the semantics of your
       
   386      * situation permit.  If consistency with encounter order is required, and
       
   387      * you are experiencing poor performance or memory utilization with
       
   388      * {@code dropWhile()} in parallel pipelines, switching to sequential
       
   389      * execution with {@link #sequential()} may improve performance.
       
   390      *
       
   391      * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
       
   392      *                  <a href="package-summary.html#Statelessness">stateless</a>
       
   393      *                  predicate to apply to elements to determine the longest
       
   394      *                  prefix of elements.
       
   395      * @return the new stream
       
   396      */
       
   397     default LongStream dropWhile(LongPredicate predicate) {
       
   398         Objects.requireNonNull(predicate);
       
   399         // Reuses the unordered spliterator, which, when encounter is present,
       
   400         // is safe to use as long as it configured not to split
       
   401         return StreamSupport.longStream(
       
   402                 new WhileOps.UnorderedWhileSpliterator.OfLong.Dropping(spliterator(), true, predicate),
       
   403                 isParallel()).onClose(this::close);
       
   404     }
       
   405 
       
   406     /**
   281      * Performs an action for each element of this stream.
   407      * Performs an action for each element of this stream.
   282      *
   408      *
   283      * <p>This is a <a href="package-summary.html#StreamOps">terminal
   409      * <p>This is a <a href="package-summary.html#StreamOps">terminal
   284      * operation</a>.
   410      * operation</a>.
   285      *
   411      *