当前位置: 首页>>代码示例>>Java>>正文


Java Stream.onClose方法代码示例

本文整理汇总了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
                    );
                }
            }
        }
    });
}
 
开发者ID:mware-solutions,项目名称:memory-graph,代码行数:18,代码来源:StreamUtils.java

示例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();
}
 
开发者ID:google,项目名称:mug,代码行数:12,代码来源:BiStreamTest.java

示例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();
}
 
开发者ID:google,项目名称:mug,代码行数:8,代码来源:BiStreamTest.java

示例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();
}
 
开发者ID:google,项目名称:mug,代码行数:8,代码来源:MoreStreamsParameterizedTest.java

示例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;
}
 
开发者ID:holon-platform,项目名称:holon-core,代码行数:16,代码来源:QueryUtils.java

示例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);
}
 
开发者ID:google,项目名称:mug,代码行数:25,代码来源:MoreStreams.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:58,代码来源:Scanner.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:52,代码来源:Scanner.java


注:本文中的java.util.stream.Stream.onClose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。