本文整理匯總了Java中java.util.stream.Stream.onClose方法的典型用法代碼示例。如果您正苦於以下問題:Java Stream.onClose方法的具體用法?Java Stream.onClose怎麽用?Java Stream.onClose使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.stream.Stream
的用法示例。
在下文中一共展示了Stream.onClose方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: withCloseHandler
import java.util.stream.Stream; //導入方法依賴的package包/類
@SafeVarargs
private static <T> Stream<T> withCloseHandler(Stream<T> stream, Iterator<T>... iterators) {
return stream.onClose(() -> {
for (Iterator<T> iterator : iterators) {
if (iterator instanceof AutoCloseable) {
try {
((AutoCloseable) iterator).close();
} catch (Exception ex) {
throw new MemgraphException(
String.format("exception occurred when closing %s", iterator.getClass().getName()),
ex
);
}
}
}
});
}
示例2: testZip_close
import java.util.stream.Stream; //導入方法依賴的package包/類
@Test public void testZip_close() {
Stream<?> left = Stream.of("a");
Stream<?> right = Stream.of(1);
AtomicBoolean leftClosed = new AtomicBoolean();
AtomicBoolean rightClosed = new AtomicBoolean();
left.onClose(() -> leftClosed.set(true));
right.onClose(() -> rightClosed.set(true));
try (BiStream<?, ?> stream = BiStream.zip(left, right)) {}
assertThat(leftClosed.get()).isTrue();
assertThat(rightClosed.get()).isTrue();
}
示例3: testIndexed_close
import java.util.stream.Stream; //導入方法依賴的package包/類
@Test public void testIndexed_close() {
Stream<?> stream = Stream.of("a");
AtomicBoolean closed = new AtomicBoolean();
stream.onClose(() -> closed.set(true));
try (BiStream<?, ?> indexed = BiStream.indexed(stream).distinct()) {}
assertThat(closed.get()).isTrue();
}
示例4: close
import java.util.stream.Stream; //導入方法依賴的package包/類
@Test public void close() {
Stream<?> stream = kind.natural(0);
AtomicBoolean closed = new AtomicBoolean();
stream.onClose(() -> closed.set(true));
try (Stream<?> diced = MoreStreams.dice(stream, 1)) {}
assertThat(closed.get()).isTrue();
}
示例5: asResultsStream
import java.util.stream.Stream; //導入方法依賴的package包/類
/**
* Turn a query results {@link Iterable} into a {@link Stream}.
* @param <T> Results type
* @param queryResultsIterable Iterable to convert (not null)
* @param closeHandler Optional close handler to perform closing operations when Stream is closed
* @return Stream from Iterable
*/
public static <T> Stream<T> asResultsStream(Iterable<T> queryResultsIterable, Runnable closeHandler) {
ObjectUtils.argumentNotNull(queryResultsIterable, "Iterable must be not null");
Stream<T> stream = StreamSupport.stream(queryResultsIterable.spliterator(), false);
if (closeHandler != null) {
stream.onClose(closeHandler);
}
return stream;
}
示例6: dice
import java.util.stream.Stream; //導入方法依賴的package包/類
/**
* Dices {@code stream} into smaller chunks each with up to {@code maxSize} elements.
*
* <p>For a sequential stream, the first N-1 chunk's will contain exactly {@code maxSize}
* elements and the last chunk may contain less (but never 0).
* However for parallel streams, it's possible that the stream is split in roughly equal-sized
* sub streams before being diced into smaller chunks, which then will result in more than one
* chunks with less than {@code maxSize} elements.
*
* <p>This is an <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html#StreamOps">
* intermediary operation</a>.
*
* @param stream the source stream to be diced
* @param maxSize the maximum size for each chunk
* @return Stream of diced chunks each being a list of size up to {@code maxSize}
* @throws IllegalStateException if {@code maxSize <= 0}
*/
public static <T> Stream<List<T>> dice(Stream<? extends T> stream, int maxSize) {
requireNonNull(stream);
if (maxSize <= 0) throw new IllegalArgumentException();
Stream<List<T>> diced = StreamSupport.stream(
() -> dice(stream.spliterator(), maxSize), Spliterator.NONNULL, stream.isParallel());
return diced.onClose(stream::close);
}
示例7: findAll
import java.util.stream.Stream; //導入方法依賴的package包/類
/**
* Returns a stream of match results from this scanner. The stream
* contains the same results in the same order that would be returned by
* calling {@code findWithinHorizon(pattern, 0)} and then {@link #match}
* successively as long as {@link #findWithinHorizon findWithinHorizon()}
* finds matches.
*
* <p>The resulting stream is sequential and ordered. All stream elements are
* non-null.
*
* <p>Scanning starts upon initiation of the terminal stream operation, using the
* current state of this scanner. Subsequent calls to any methods on this scanner
* other than {@link #close} and {@link #ioException} may return undefined results
* or may cause undefined effects on the returned stream. The returned stream's source
* {@code Spliterator} is <em>fail-fast</em> and will, on a best-effort basis, throw a
* {@link java.util.ConcurrentModificationException} if any such calls are detected
* during stream pipeline execution.
*
* <p>After stream pipeline execution completes, this scanner is left in an indeterminate
* state and cannot be reused.
*
* <p>If this scanner contains a resource that must be released, this scanner
* should be closed, either by calling its {@link #close} method, or by
* closing the returned stream. Closing the stream will close the underlying scanner.
* {@code IllegalStateException} is thrown if the scanner has been closed when this
* method is called, or if this scanner is closed during stream pipeline execution.
*
* <p>As with the {@link #findWithinHorizon findWithinHorizon()} methods, this method
* might block waiting for additional input, and it might buffer an unbounded amount of
* input searching for a match.
*
* @apiNote
* For example, the following code will read a file and return a list
* of all sequences of characters consisting of seven or more Latin capital
* letters:
*
* <pre>{@code
* try (Scanner sc = new Scanner(Paths.get("input.txt"))) {
* Pattern pat = Pattern.compile("[A-Z]{7,}");
* List<String> capWords = sc.findAll(pat)
* .map(MatchResult::group)
* .collect(Collectors.toList());
* }
* }</pre>
*
* @param pattern the pattern to be matched
* @return a sequential stream of match results
* @throws NullPointerException if pattern is null
* @throws IllegalStateException if this scanner is closed
* @since 9
*/
public Stream<MatchResult> findAll(Pattern pattern) {
Objects.requireNonNull(pattern);
ensureOpen();
Stream<MatchResult> stream = StreamSupport.stream(new FindSpliterator(pattern), false);
return stream.onClose(this::close);
}
示例8: tokens
import java.util.stream.Stream; //導入方法依賴的package包/類
/**
* Returns a stream of delimiter-separated tokens from this scanner. The
* stream contains the same tokens that would be returned, starting from
* this scanner's current state, by calling the {@link #next} method
* repeatedly until the {@link #hasNext} method returns false.
*
* <p>The resulting stream is sequential and ordered. All stream elements are
* non-null.
*
* <p>Scanning starts upon initiation of the terminal stream operation, using the
* current state of this scanner. Subsequent calls to any methods on this scanner
* other than {@link #close} and {@link #ioException} may return undefined results
* or may cause undefined effects on the returned stream. The returned stream's source
* {@code Spliterator} is <em>fail-fast</em> and will, on a best-effort basis, throw a
* {@link java.util.ConcurrentModificationException} if any such calls are detected
* during stream pipeline execution.
*
* <p>After stream pipeline execution completes, this scanner is left in an indeterminate
* state and cannot be reused.
*
* <p>If this scanner contains a resource that must be released, this scanner
* should be closed, either by calling its {@link #close} method, or by
* closing the returned stream. Closing the stream will close the underlying scanner.
* {@code IllegalStateException} is thrown if the scanner has been closed when this
* method is called, or if this scanner is closed during stream pipeline execution.
*
* <p>This method might block waiting for more input.
*
* @apiNote
* For example, the following code will create a list of
* comma-delimited tokens from a string:
*
* <pre>{@code
* List<String> result = new Scanner("abc,def,,ghi")
* .useDelimiter(",")
* .tokens()
* .collect(Collectors.toList());
* }</pre>
*
* <p>The resulting list would contain {@code "abc"}, {@code "def"},
* the empty string, and {@code "ghi"}.
*
* @return a sequential stream of token strings
* @throws IllegalStateException if this scanner is closed
* @since 9
*/
public Stream<String> tokens() {
ensureOpen();
Stream<String> stream = StreamSupport.stream(new TokenSpliterator(), false);
return stream.onClose(this::close);
}