Java Code Examples for java.util.Spliterators#AbstractLongSpliterator

The following examples show how to use java.util.Spliterators#AbstractLongSpliterator . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LongStream.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code LongStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code LongStream} will
 * be the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * <p>The action of applying {@code f} for one element
 * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a>
 * the action of applying {@code f} for subsequent elements.  For any given
 * element the action may be performed in whatever thread the library
 * chooses.
 *
 * @param seed the initial element
 * @param f a function to be applied to the previous element to produce
 *          a new element
 * @return a new sequential {@code LongStream}
 */
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
    Objects.requireNonNull(f);
    Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE,
           Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
        long prev;
        boolean started;

        @Override
        public boolean tryAdvance(LongConsumer action) {
            Objects.requireNonNull(action);
            long t;
            if (started)
                t = f.applyAsLong(prev);
            else {
                t = seed;
                started = true;
            }
            action.accept(prev = t);
            return true;
        }
    };
    return StreamSupport.longStream(spliterator, false);
}
 
Example 2
Source File: LongStream.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code LongStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code LongStream} will
 * be the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * <p>The action of applying {@code f} for one element
 * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a>
 * the action of applying {@code f} for subsequent elements.  For any given
 * element the action may be performed in whatever thread the library
 * chooses.
 *
 * @param seed the initial element
 * @param f a function to be applied to the previous element to produce
 *          a new element
 * @return a new sequential {@code LongStream}
 */
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
    Objects.requireNonNull(f);
    Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE,
           Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
        long prev;
        boolean started;

        @Override
        public boolean tryAdvance(LongConsumer action) {
            Objects.requireNonNull(action);
            long t;
            if (started)
                t = f.applyAsLong(prev);
            else {
                t = seed;
                started = true;
            }
            action.accept(prev = t);
            return true;
        }
    };
    return StreamSupport.longStream(spliterator, false);
}