Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnmodifiableFuture? #65

Open
trustin opened this issue Jan 18, 2020 · 7 comments
Open

UnmodifiableFuture? #65

trustin opened this issue Jan 18, 2020 · 7 comments

Comments

@trustin
Copy link

trustin commented Jan 18, 2020

When returning a CompletableFuture to a caller, I sometimes need to make sure it is never completed by the caller. I could return a CompletionStage but it is still possible for a user to misuse it by converting it with toCompletableFuture().

So, I wrote a new class called UnmodifiableFuture:

Are you interested in making this a part of completable-futures? Then, I'd be happy to polish it a little bit and send a pull request.

@spkrka
Copy link
Member

spkrka commented Jan 20, 2020

Couldn't you just return x.thenApply(Function.identity()) instead of x to prevent x from being completed by the caller?

@trustin
Copy link
Author

trustin commented Jan 20, 2020

It doesn't prevent a user from completing the returned future anyway. If it's passed to some other component, then we have the same problem, and I'd like to avoid using thenApply(Function.identity()) whenever I pass it around. It also creates an additional future object.

@trustin
Copy link
Author

trustin commented Jan 20, 2020

Also, we can create a singleton UnmodifiableFuture that's been completed with null and reuse it in many places, which is a GC-wise win.

@spkrka
Copy link
Member

spkrka commented Jan 20, 2020

@trustin
Copy link
Author

trustin commented Jan 20, 2020

Not in Java 8, because it's added in 9. 😭 (It's also less verbose to call .join() or .get() because minimal stage must be converted to future.)

@mattnworb
Copy link
Member

I like the idea 👍

@trustin
Copy link
Author

trustin commented Jul 20, 2020

Gentle ping 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants