I wish to implement the Stream<E>
interface (I admit, it's the unnecessarily large one) and add a builder method 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.......);
}
}
So far so good.
long count = new MyStream(list.stream())
.map(i -> i * 10)
.foo()
.filter(i -> i > 100)
.count();
I have trouble with the Closeable
behavior of Stream
. The documentation of Stream
says about closing (formatting mine):
Streams have a
BaseStream.close()
method and implementAutoCloseable
, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned byFiles.lines(Path, Charset))
will require closing.
The only methods that close Stream are flatMap
or close
.
The instantiation of an object in Eclipse Oxygen is underlined with a warning:
Resource leak: '
<unassigned Closeable value>
' is never closed
This is not reproducible with IntelliJIdea 2018.1.5. Related questions I skimmed through are here and here. I understand the Closeable
issues with File
or Dictionary
, however, I am stuck with Streams.
I dislike the static method MyStream.of(...)
calling a private constructor workaround.