< prev index next >

src/java.base/share/classes/java/util/stream/IntStream.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


 868      * @return the new stream
 869      */
 870     public static IntStream of(int... values) {
 871         return Arrays.stream(values);
 872     }
 873 
 874     /**
 875      * Returns an infinite sequential ordered {@code IntStream} produced by iterative
 876      * application of a function {@code f} to an initial element {@code seed},
 877      * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 878      * {@code f(f(seed))}, etc.
 879      *
 880      * <p>The first element (position {@code 0}) in the {@code IntStream} will be
 881      * the provided {@code seed}.  For {@code n > 0}, the element at position
 882      * {@code n}, will be the result of applying the function {@code f} to the
 883      * element at position {@code n - 1}.
 884      *
 885      * @param seed the initial element
 886      * @param f a function to be applied to the previous element to produce
 887      *          a new element
 888      * @return A new sequential {@code IntStream}
 889      */
 890     public static IntStream iterate(final int seed, final IntUnaryOperator f) {
 891         Objects.requireNonNull(f);
 892         final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() {
 893             int t = seed;
 894 
 895             @Override
 896             public boolean hasNext() {
 897                 return true;
 898             }
 899 
 900             @Override
 901             public int nextInt() {
 902                 int v = t;
 903                 t = f.applyAsInt(t);
 904                 return v;
 905             }
 906         };
 907         return StreamSupport.intStream(Spliterators.spliteratorUnknownSize(
 908                 iterator,
 909                 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
































































 910     }
 911 
 912     /**
 913      * Returns an infinite sequential unordered stream where each element is
 914      * generated by the provided {@code IntSupplier}.  This is suitable for
 915      * generating constant streams, streams of random elements, etc.
 916      *
 917      * @param s the {@code IntSupplier} for generated elements
 918      * @return a new infinite sequential unordered {@code IntStream}
 919      */
 920     public static IntStream generate(IntSupplier s) {
 921         Objects.requireNonNull(s);
 922         return StreamSupport.intStream(
 923                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfInt(Long.MAX_VALUE, s), false);
 924     }
 925 
 926     /**
 927      * Returns a sequential ordered {@code IntStream} from {@code startInclusive}
 928      * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of
 929      * {@code 1}.


   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


 868      * @return the new stream
 869      */
 870     public static IntStream of(int... values) {
 871         return Arrays.stream(values);
 872     }
 873 
 874     /**
 875      * Returns an infinite sequential ordered {@code IntStream} produced by iterative
 876      * application of a function {@code f} to an initial element {@code seed},
 877      * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 878      * {@code f(f(seed))}, etc.
 879      *
 880      * <p>The first element (position {@code 0}) in the {@code IntStream} will be
 881      * the provided {@code seed}.  For {@code n > 0}, the element at position
 882      * {@code n}, will be the result of applying the function {@code f} to the
 883      * element at position {@code n - 1}.
 884      *
 885      * @param seed the initial element
 886      * @param f a function to be applied to the previous element to produce
 887      *          a new element
 888      * @return a new sequential {@code IntStream}
 889      */
 890     public static IntStream iterate(final int seed, final IntUnaryOperator f) {
 891         Objects.requireNonNull(f);
 892         final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() {
 893             int t = seed;
 894 
 895             @Override
 896             public boolean hasNext() {
 897                 return true;
 898             }
 899 
 900             @Override
 901             public int nextInt() {
 902                 int v = t;
 903                 t = f.applyAsInt(t);
 904                 return v;
 905             }
 906         };
 907         return StreamSupport.intStream(Spliterators.spliteratorUnknownSize(
 908                 iterator,
 909                 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
 910     }
 911 
 912     /**
 913      * Returns a sequential ordered {@code IntStream} produced by iterative
 914      * application of a function {@code f} to an initial element {@code seed},
 915      * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 916      * {@code f(f(seed))}, etc.  The stream terminates when {@code predicate}
 917      * returns false.
 918      *
 919      * <p>The first element (position {@code 0}) in the {@code IntStream} will be
 920      * the provided {@code seed}.  For {@code n > 0}, the element at position
 921      * {@code n}, will be the result of applying the function {@code f} to the
 922      * element at position {@code n - 1}.
 923      *
 924      * @param seed the initial element
 925      * @param predicate a predicate to apply to elements to determine when the 
 926      *          stream must terminate.
 927      * @param f a function to be applied to the previous element to produce
 928      *          a new element
 929      * @return a new sequential {@code IntStream}
 930      * @since 9
 931      */
 932     public static IntStream iterate(int seed, IntPredicate predicate, IntUnaryOperator f) {
 933         Objects.requireNonNull(f);
 934         Objects.requireNonNull(predicate);
 935         Spliterator.OfInt spliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE, 
 936                Spliterator.ORDERED | Spliterator.IMMUTABLE) {
 937             int prev;
 938             boolean started, finished;
 939 
 940             @Override
 941             public boolean tryAdvance(IntConsumer action) {
 942                 Objects.requireNonNull(action);
 943                 if (finished)
 944                     return false;
 945                 int t;
 946                 if (started)
 947                     t = f.applyAsInt(prev);
 948                 else {
 949                     t = seed;
 950                     started = true;
 951                 }
 952                 if (!predicate.test(t)) {
 953                     finished = true;
 954                     return false;
 955                 }
 956                 action.accept(prev = t);
 957                 return true;
 958             }
 959 
 960             @Override
 961             public void forEachRemaining(IntConsumer action) {
 962                 Objects.requireNonNull(action);
 963                 if (finished)
 964                     return;
 965                 int t = started ? f.applyAsInt(prev) : seed;
 966                 finished = true;
 967                 while (predicate.test(t)) {
 968                     action.accept(t);
 969                     t = f.applyAsInt(t);
 970                 }
 971             }
 972         };
 973         return StreamSupport.intStream(spliterator, false);
 974     }
 975 
 976     /**
 977      * Returns an infinite sequential unordered stream where each element is
 978      * generated by the provided {@code IntSupplier}.  This is suitable for
 979      * generating constant streams, streams of random elements, etc.
 980      *
 981      * @param s the {@code IntSupplier} for generated elements
 982      * @return a new infinite sequential unordered {@code IntStream}
 983      */
 984     public static IntStream generate(IntSupplier s) {
 985         Objects.requireNonNull(s);
 986         return StreamSupport.intStream(
 987                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfInt(Long.MAX_VALUE, s), false);
 988     }
 989 
 990     /**
 991      * Returns a sequential ordered {@code IntStream} from {@code startInclusive}
 992      * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of
 993      * {@code 1}.


< prev index next >