下面列出了java.util.Spliterator# IMMUTABLE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
public int characteristics() {
return (Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
public int characteristics() {
return (Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
public int characteristics() {
return (Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
@Override
public int characteristics() {
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.IMMUTABLE | Spliterator.NONNULL |
Spliterator.DISTINCT | Spliterator.SORTED;
}
@Override
public int characteristics() {
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.IMMUTABLE | Spliterator.NONNULL |
Spliterator.DISTINCT | Spliterator.SORTED;
}
@Override
public int characteristics() {
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.IMMUTABLE | Spliterator.NONNULL |
Spliterator.DISTINCT | Spliterator.SORTED;
}
@Override
public int characteristics() {
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.IMMUTABLE | Spliterator.NONNULL |
Spliterator.DISTINCT | Spliterator.SORTED;
}
@Override
public int characteristics() {
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.IMMUTABLE | Spliterator.NONNULL |
Spliterator.DISTINCT | Spliterator.SORTED;
}
public int characteristics() {
return (Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
@Override
public int characteristics() {
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.IMMUTABLE | Spliterator.NONNULL |
Spliterator.DISTINCT | Spliterator.SORTED;
}
public int characteristics() {
return (Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
@Override
public int characteristics() {
return Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.ORDERED | Spliterator.IMMUTABLE;
}
public int characteristics() {
return (Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
public int characteristics() {
return (Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
@Override
public int characteristics() {
return Spliterator.DISTINCT + Spliterator.NONNULL + Spliterator.IMMUTABLE;
}
@Override
public int characteristics() {
return Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.ORDERED | Spliterator.IMMUTABLE;
}
/**
* Returns a stream whose elements are the URLs of all the resources with
* the given name. A resource is some data (images, audio, text, etc) that
* can be accessed by class code in a way that is independent of the
* location of the code.
*
* <p> The name of a resource is a {@code /}-separated path name that
* identifies the resource.
*
* <p> The resources will be located when the returned stream is evaluated.
* If the evaluation results in an {@code IOException} then the I/O
* exception is wrapped in an {@link UncheckedIOException} that is then
* thrown.
*
* <p> Resources in named modules are subject to the encapsulation rules
* specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
* Additionally, and except for the special case where the resource has a
* name ending with "{@code .class}", this method will only find resources in
* packages of named modules when the package is {@link Module#isOpen(String)
* opened} unconditionally (even if the caller of this method is in the
* same module as the resource). </p>
*
* @implSpec The default implementation invokes {@link #getResources(String)
* getResources} to find all the resources with the given name and returns
* a stream with the elements in the enumeration as the source.
*
* @apiNote When overriding this method it is recommended that an
* implementation ensures that any delegation is consistent with the {@link
* #getResource(java.lang.String) getResource(String)} method. This should
* ensure that the first element returned by the stream is the same
* resource that the {@code getResource(String)} method would return.
*
* @param name
* The resource name
*
* @return A stream of resource {@link java.net.URL URL} objects. If no
* resources could be found, the stream will be empty. Resources
* for which a {@code URL} cannot be constructed, are in a package
* that is not opened unconditionally, or access to the resource
* is denied by the security manager, will not be in the stream.
*
* @throws NullPointerException If {@code name} is {@code null}
*
* @since 9
*/
public Stream<URL> resources(String name) {
Objects.requireNonNull(name);
int characteristics = Spliterator.NONNULL | Spliterator.IMMUTABLE;
Supplier<Spliterator<URL>> si = () -> {
try {
return Spliterators.spliteratorUnknownSize(
getResources(name).asIterator(), characteristics);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
return StreamSupport.stream(si, characteristics, false);
}
/**
* Returns a stream whose elements are the URLs of all the resources with
* the given name. A resource is some data (images, audio, text, etc) that
* can be accessed by class code in a way that is independent of the
* location of the code.
*
* <p> The name of a resource is a {@code /}-separated path name that
* identifies the resource.
*
* <p> The resources will be located when the returned stream is evaluated.
* If the evaluation results in an {@code IOException} then the I/O
* exception is wrapped in an {@link UncheckedIOException} that is then
* thrown.
*
* <p> Resources in named modules are subject to the encapsulation rules
* specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
* Additionally, and except for the special case where the resource has a
* name ending with "{@code .class}", this method will only find resources in
* packages of named modules when the package is {@link Module#isOpen(String)
* opened} unconditionally (even if the caller of this method is in the
* same module as the resource). </p>
*
* @implSpec The default implementation invokes {@link #getResources(String)
* getResources} to find all the resources with the given name and returns
* a stream with the elements in the enumeration as the source.
*
* @apiNote When overriding this method it is recommended that an
* implementation ensures that any delegation is consistent with the {@link
* #getResource(java.lang.String) getResource(String)} method. This should
* ensure that the first element returned by the stream is the same
* resource that the {@code getResource(String)} method would return.
*
* @param name
* The resource name
*
* @return A stream of resource {@link java.net.URL URL} objects. If no
* resources could be found, the stream will be empty. Resources
* for which a {@code URL} cannot be constructed, are in a package
* that is not opened unconditionally, or access to the resource
* is denied by the security manager, will not be in the stream.
*
* @throws NullPointerException If {@code name} is {@code null}
*
* @since 9
*/
public Stream<URL> resources(String name) {
Objects.requireNonNull(name);
int characteristics = Spliterator.NONNULL | Spliterator.IMMUTABLE;
Supplier<Spliterator<URL>> si = () -> {
try {
return Spliterators.spliteratorUnknownSize(
getResources(name).asIterator(), characteristics);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
return StreamSupport.stream(si, characteristics, false);
}