Java Code Examples for java.util.Spliterators#AbstractSpliterator
The following examples show how to use
java.util.Spliterators#AbstractSpliterator .
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: DesignerIteratorUtil.java From pmd-designer with BSD 2-Clause "Simplified" License | 6 votes |
private static <T> Spliterator<T> takeWhile(Spliterator<T> splitr, Predicate<? super T> predicate) { return new Spliterators.AbstractSpliterator<T>(splitr.estimateSize(), 0) { boolean stillGoing = true; @Override public boolean tryAdvance(Consumer<? super T> consumer) { if (stillGoing) { boolean hadNext = splitr.tryAdvance(elem -> { if (predicate.test(elem)) { consumer.accept(elem); } else { stillGoing = false; } }); return hadNext && stillGoing; } return false; } }; }
Example 2
Source File: Stream.java From Bytecoder with Apache License 2.0 | 6 votes |
/** * Returns an infinite sequential ordered {@code Stream} 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 Stream} 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 <T> the type of stream elements * @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 Stream} */ public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) { Objects.requireNonNull(f); Spliterator<T> spliterator = new Spliterators.AbstractSpliterator<>(Long.MAX_VALUE, Spliterator.ORDERED | Spliterator.IMMUTABLE) { T prev; boolean started; @Override public boolean tryAdvance(Consumer<? super T> action) { Objects.requireNonNull(action); T t; if (started) t = f.apply(prev); else { t = seed; started = true; } action.accept(prev = t); return true; } }; return StreamSupport.stream(spliterator, false); }
Example 3
Source File: Stream.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Returns an infinite sequential ordered {@code Stream} 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 Stream} 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 <T> the type of stream elements * @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 Stream} */ public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) { Objects.requireNonNull(f); Spliterator<T> spliterator = new Spliterators.AbstractSpliterator<>(Long.MAX_VALUE, Spliterator.ORDERED | Spliterator.IMMUTABLE) { T prev; boolean started; @Override public boolean tryAdvance(Consumer<? super T> action) { Objects.requireNonNull(action); T t; if (started) t = f.apply(prev); else { t = seed; started = true; } action.accept(prev = t); return true; } }; return StreamSupport.stream(spliterator, false); }
Example 4
Source File: Streamer.java From FHIR with Apache License 2.0 | 5 votes |
/** * Wrap the JDBC {@link ResultSet} as a stream object. Note that the stream has to be consumed within * the boundaries of the statement execution * @param rs * @return */ public Stream<Row> wrap(final ResultSet rs) throws SQLException { ResultSetMetaData md = rs.getMetaData(); final String[] columnNames = new String[md.getColumnCount()]; for (int i=0; i<columnNames.length; i++) { columnNames[i] = md.getColumnName(i+1); } Spliterator<Row> s = new Spliterators.AbstractSpliterator<Row>(Long.MAX_VALUE, Spliterator.NONNULL | Spliterator.IMMUTABLE) { @Override public boolean tryAdvance(Consumer<? super Row> action) { try { if (!rs.next()) { return false; } // wrap the current result record as a Row which is then handed // off to the stream action.accept(new Row(rs, columnNames)); return true; } catch (SQLException x) { // ouch logger.log(Level.SEVERE, x.getMessage(), x); return false; } } }; // Create a sequential stream of the rows from the result set return StreamSupport.stream(s, false); }
Example 5
Source File: TestHelpers.java From streamex with Apache License 2.0 | 5 votes |
static <T> Spliterator<T> emptySpliteratorWithExactSize(long exactSize) { return new Spliterators.AbstractSpliterator<T>(0, Spliterator.SIZED) { @Override public long getExactSizeIfKnown() { return exactSize; } @Override public boolean tryAdvance(Consumer<? super T> ignored) { return false; } }; }