RFR: 8272137: Make Collection and Optional classes streamable
CC007
github.com+5381337+cc007 at openjdk.java.net
Tue Aug 17 17:54:32 UTC 2021
On Mon, 9 Aug 2021 12:28:23 GMT, CC007 <github.com+5381337+CC007 at openjdk.org> wrote:
> create Streamable and ParallelStreamable interface and use them in Collection and Optional
Ah ok, I see your point. In the case that you want to have something be only `Streamable`, you can create an interface like this (fixed missing method type param and added `ofCollection`:
public interface Streamable<T> {
Stream<T> stream();
static <T> Streamable<T> ofIterable(Iterable<T> iterable) {
return () -> StreamSupport.stream(iterable.spliterator(), false);
}
static <T> Streamable<T> ofCollection(Collection<T> collection) {
return collection::stream;
}
}
This will indeed allow you to only expose the `stream()` method, even to the degree that you can't even expose the other methods with type casting, which is a nice side effect. You could also add a static method for `ofOptional`, if required, but you made a good point about `Optional.stream`'s general use case (though it could still be used as a stream when needed).
At first I didn't fully understand how that would resolve the issue for whenever you need something that is both `Iterable` and `Streamable`, but after rereading your comment I came up with the following interface:
/**
* This is an interface that specifies that its content can be traversed and acted upon,
* either through iteration or using streams
*/
public interface Traversable<T> extends Streamable<T>, Iterable<T> {
static <T> Traversable<T> ofIterable(Iterable<T> iterable) {
return new Traversable<T>() {
@Override
public Stream<T> stream() {
return StreamSupport.stream(iterable.spliterator(), false);
}
@Override
public Iterator<T> iterator() {
return iterable.iterator();
}
};
}
static <T> Traversable<T> ofStreamable(Streamable<T> streamable) {
return new Traversable<T>() {
@Override
public Stream<T> stream() {
return streamable.stream();
}
@Override
public Iterator<T> iterator() {
return streamable.stream().iterator();
}
};
}
static <T> Traversable<T> ofCollection(Collection<T> collection) {
return new Traversable<T>() {
@Override
public Stream<T> stream() {
return collection.stream();
}
@Override
public Iterator<T> iterator() {
return collection.iterator();
}
};
}
}
For anyone who's doubtful about the arguments against this change, you can find this code in use in demo2 in my demo repo: https://github.com/CC007/InterfaceSegregationDemo/tree/master/src/main/java/com/github/cc007/interfacesegregationdemo/demo2
-------------
PR: https://git.openjdk.java.net/jdk/pull/5050
More information about the core-libs-dev
mailing list