下面列出了java.util.Spliterators#AbstractSpliterator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
};
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
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;
}
};
}