我希望实施Stream<E>
接口(我承认,这是不必要的大接口)并添加一个构建器方法foo()
.
public MyStream<E> implements Stream<E>, ExtendedStream<E> {
private final Stream<E> delegate;
public MyStream(final Stream<E> stream) {
this.delegate = stream;
}
// a sample Stream<E> method implementation
@Override
public <R> MyStream<R> map(Function<? super E, ? extends R> mapper) {
return new MyStream<>(this.delegate.map(mapper));
}
// the rest in the same way (skipped)
// a method from ExtendedStream<E>
@Override
public MyStream<E> foo() {
return new MyStream(this.delegate.......);
}
}
到目前为止,一切都很好。
long count = new MyStream(list.stream())
.map(i -> i * 10)
.foo()
.filter(i -> i > 100)
.count();
我遇到了麻烦Closeable
的行为Stream
。的文档Stream https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html关于关闭(格式化我的)的说明:
流有一个BaseStream.close()
方法与实施AutoCloseable
,但几乎所有流实例实际上并不需要在使用后关闭。一般来说,只有源是 IO 通道的流(例如由Files.lines(Path, Charset))
将需要关闭。
关闭 Stream 的唯一方法是flatMap
or close
.
对象的实例化在日食氧气带有下划线并带有警告:
资源泄漏:'<unassigned Closeable value>
' 永远不会关闭
这不能重现IntelliJ Idea 2018.1.5。我浏览过的相关问题是here https://stackoverflow.com/questions/11463327/is-this-a-memory-leak-or-a-false-positive and here https://stackoverflow.com/questions/23779163/potential-resource-leak-unassigned-closeable-with-a-hashmap。我明白了Closeable
问题与File
or Dictionary
,但是,我被 Streams 困住了。
I dislike the static method MyStream.of(...)
calling a private constructor workaround.