java.util.stream.Stream.onClose()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(422)

本文整理了Java中java.util.stream.Stream.onClose()方法的一些代码示例,展示了Stream.onClose()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Stream.onClose()方法的具体详情如下:
包路径:java.util.stream.Stream
类名称:Stream
方法名:onClose

Stream.onClose介绍

暂无

代码示例

代码示例来源:origin: neo4j/neo4j

/**
 * @return this iterator as a {@link Stream}
 */
default Stream<T> stream()
{
  return StreamSupport
      .stream( spliteratorUnknownSize( this, 0 ), false )
      .onClose( this::close );
}

代码示例来源:origin: ben-manes/caffeine

/** Returns a stream of each line in the trace file. */
 protected Stream<String> lines() throws IOException {
  InputStream input = readFiles();
  Reader reader = new InputStreamReader(input, UTF_8);
  return new BufferedReader(reader).lines().map(String::trim)
    .onClose(() -> Closeables.closeQuietly(input));
 }
}

代码示例来源:origin: iluwatar/java-design-patterns

}, false).onClose(() -> mutedClose(connection, statement, resultSet));
} catch (SQLException e) {
 throw new CustomException(e.getMessage(), e);

代码示例来源:origin: google/guava

return reader
  .lines()
  .onClose(
    () -> {
     try {

代码示例来源:origin: requery/requery

@Override
public Stream<E> stream() {
  final CloseableIterator<E> iterator = createIterator();
  Spliterator<E> spliterator = maxSize == null ?
    Spliterators.spliteratorUnknownSize(iterator, 0) :
    Spliterators.spliterator(iterator, maxSize, 0);
  return StreamSupport.stream(spliterator, false).onClose(new Runnable() {
    @Override
    public void run() {
      iterator.close();
    }
  });
}

代码示例来源:origin: Graylog2/graylog2-server

/**
 * Returns an unordered stream of database entries for the given {@link DBQuery.Query}.
 * <p>
 * The returned stream needs to be closed to free the underlying database resources.
 *
 * @param query the query to execute
 * @return stream of database entries that match the query
 */
protected Stream<DTO> streamQuery(DBQuery.Query query) {
  final DBCursor<DTO> cursor = db.find(query);
  return Streams.stream((Iterable<DTO>) cursor).onClose(cursor::close);
}

代码示例来源:origin: google/guava

public void testZip_closeIsPropagated() {
 AtomicInteger lettersCloseCount = new AtomicInteger();
 Stream<String> letters = Stream.of("a", "b", "c").onClose(lettersCloseCount::incrementAndGet);
 AtomicInteger numbersCloseCount = new AtomicInteger();
 Stream<Integer> numbers = Stream.of(1, 2, 3).onClose(numbersCloseCount::incrementAndGet);
 Stream<String> zipped = Streams.zip(letters, numbers, (a, b) -> a + ":" + b);
 zipped.close();
 Truth.assertThat(lettersCloseCount.get()).isEqualTo(1);
 Truth.assertThat(numbersCloseCount.get()).isEqualTo(1);
}

代码示例来源:origin: hibernate/hibernate-orm

@Override
@SuppressWarnings("unchecked")
public Stream<R> stream() {
  if (getMaxResults() == 0){
    final Spliterator<R> spliterator = Spliterators.emptySpliterator();
    return StreamSupport.stream( spliterator, false );
  }
  final ScrollableResultsImplementor scrollableResults = scroll( ScrollMode.FORWARD_ONLY );
  final ScrollableResultsIterator<R> iterator = new ScrollableResultsIterator<>( scrollableResults );
  final Spliterator<R> spliterator = Spliterators.spliteratorUnknownSize( iterator, Spliterator.NONNULL );
  final Stream<R> stream = StreamSupport.stream( spliterator, false );
  stream.onClose( scrollableResults::close );
  return stream;
}

代码示例来源:origin: google/guava

public void testConcat_refStream_closeIsPropagated_Stream_concat() {
 // Just to demonstrate behavior of Stream::concat in the standard library
 AtomicInteger closeCountB = new AtomicInteger(0);
 Stream<String> streamB = Stream.of("b").onClose(closeCountB::incrementAndGet);
 Stream<String> concatenated =
   Stream.<Stream<String>>of(Stream.of("a"), streamB, Stream.empty(), Stream.of("c", "d"))
     .reduce(Stream.empty(), Stream::concat);
 assertThat(concatenated).containsExactly("a", "b", "c", "d").inOrder();
 concatenated.close();
 Truth.assertThat(closeCountB.get()).isEqualTo(1);
}

代码示例来源:origin: google/guava

public void testConcat_refStream_closeIsPropagated_Stream_flatMap() {
 // Just to demonstrate behavior of Stream::flatMap in the standard library
 AtomicInteger closeCountB = new AtomicInteger(0);
 Stream<String> streamB = Stream.of("b").onClose(closeCountB::incrementAndGet);
 Stream<String> concatenated =
   Stream.<Stream<String>>of(Stream.of("a"), streamB, Stream.empty(), Stream.of("c", "d"))
     .flatMap(x -> x);
 assertThat(concatenated).containsExactly("a", "b", "c", "d").inOrder();
 concatenated.close();
 // even without close, see doc for flatMap
 Truth.assertThat(closeCountB.get()).isEqualTo(1);
}

代码示例来源:origin: google/guava

private void testMapWithIndex_closeIsPropagated(Stream<String> source) {
 AtomicInteger stringsCloseCount = new AtomicInteger();
 Stream<String> strings = source.onClose(stringsCloseCount::incrementAndGet);
 Stream<String> withIndex = Streams.mapWithIndex(strings, (str, i) -> str + ":" + i);
 withIndex.close();
 Truth.assertThat(stringsCloseCount.get()).isEqualTo(1);
}

代码示例来源:origin: Graylog2/graylog2-server

/**
 * Returns a stream of database entries for the given {@link DBQuery.Query} sorted by the give {@link DBSort.SortBuilder}.
 * <p>
 * The returned stream needs to be closed to free the underlying database resources.
 *
 * @param query the query to execute
 * @param sort the sort order for the query
 * @return stream of database entries that match the query
 */
protected Stream<DTO> streamQueryWithSort(DBQuery.Query query, DBSort.SortBuilder sort) {
  final DBCursor<DTO> cursor = db.find(query).sort(sort);
  return Streams.stream((Iterable<DTO>) cursor).onClose(cursor::close);
}

代码示例来源:origin: google/guava

public void testConcat_refStream_closeIsPropagated() {
 AtomicInteger closeCountB = new AtomicInteger(0);
 Stream<String> streamB = Stream.of("b").onClose(closeCountB::incrementAndGet);
 Stream<String> concatenated =
   Streams.concat(Stream.of("a"), streamB, Stream.empty(), Stream.of("c", "d"));
 assertThat(concatenated).containsExactly("a", "b", "c", "d").inOrder();
 concatenated.close();
 Truth.assertThat(closeCountB.get()).isEqualTo(1);
}

代码示例来源:origin: google/guava

estimatedSize),
  isParallel)
.onClose(
  () -> {
   for (Stream<? extends T> stream : streams) {

代码示例来源:origin: neo4j/neo4j

@Procedure
  public Stream<MyOutputRecord> throwsInStream( )
  {
    return Stream.<MyOutputRecord>generate( () ->
    {
      throw new IndexOutOfBoundsException();
    }).onClose( () ->
    {
      throw new ExceptionDuringClose();
    } );
  }
}

代码示例来源:origin: reactor/reactor-core

@Test
public void streamClosedOnCompletionSlowPathNormal() {
  AtomicInteger closed = new AtomicInteger();
  Stream<String> source = Stream.of("foo", "bar", "baz")
                 .onClose(closed::incrementAndGet);
  StepVerifier.create(Flux.fromStream(source), 3)
        .expectNext("foo", "bar", "baz")
        .verifyComplete();
  assertThat(closed.get()).isEqualTo(1);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void streamClosedOnNullContentSlowPathNormal() {
  AtomicInteger closed = new AtomicInteger();
  Stream<String> source = Stream.of("foo", "bar", null, "baz")
                 .onClose(closed::incrementAndGet);
  StepVerifier.create(Flux.fromStream(source), 4)
        .expectNext("foo", "bar")
        .verifyErrorMessage("The iterator returned a null value");
  assertThat(closed.get()).isEqualTo(1);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void streamClosedOnCompletionConditional() {
  AtomicInteger closed = new AtomicInteger();
  Stream<String> source = Stream.of("foo", "bar", "baz")
                 .onClose(closed::incrementAndGet);
  StepVerifier.create(Flux.fromStream(source).filter(i -> true))
        .expectNext("foo", "bar", "baz")
        .verifyComplete();
  assertThat(closed.get()).isEqualTo(1);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void streamClosedOnCancelConditional() {
  AtomicInteger closed = new AtomicInteger();
  Stream<String> source = Stream.of("foo", "bar", "baz")
                 .onClose(closed::incrementAndGet);
  StepVerifier.create(Flux.fromStream(source)
              .filter(i -> true))
        .expectNext("foo")
        .thenCancel()
        .verify();
  assertThat(closed.get()).isEqualTo(1);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void streamClosedOnErrorConditional() {
  AtomicInteger closed = new AtomicInteger();
  Stream<String> source = Stream.of("foo", "bar", "baz")
                 .onClose(closed::incrementAndGet);
  StepVerifier.create(Flux.fromStream(source)
              .concatWith(Mono.error(new IllegalStateException("boom")))
              .filter(i -> true))
        .expectNext("foo", "bar", "baz")
        .verifyErrorMessage("boom");
  assertThat(closed.get()).isEqualTo(1);
}

相关文章