Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:26:10+00:00 2026-06-02T06:26:10+00:00

https://gist.github.com/2414323 Sometimes, you already know the result of a computation, but your interface requires

  • 0

https://gist.github.com/2414323

Sometimes, you already know the result of a computation, but your interface requires you to return a Future. Rather than implement a long anonymous class, or extend AbstractFuture or FutureTask which are themselves complicated, I find it simpler to just create a small holder class that implements Future.

My question is – does a class similar to what I wrote below already exist in one of the standard libraries?

Usage:

Future<Boolean> iHaveToReturnAFutureButIAlreadyKnowTheAnswer() {
    return new ResolvedFuture(true);
}

Code:

/**
 * Used when you need to return a Future, but you already have the answer.
 */
public class ResolvedFuture<T> implements Future<T>{
    private final T item;

    public ResolvedFuture(T item) {
        this.item = item;
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return false;
    }

    @Override
    public boolean isCancelled() {
        return false;
    }

    @Override
    public boolean isDone() {
        return true;
    }

    @Override
    public T get() throws InterruptedException, ExecutionException {
        return item;
    }

    @Override
    public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return item;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:26:11+00:00Added an answer on June 2, 2026 at 6:26 am

    Standard library doesn’t support it, but some third-party libraries may contains such an implementation. For example, Futures.immediateFuture() from Google Guava.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Folks, I have this code https://gist.github.com/2473387 But, how do I get the coordinates of
This is PHP code I have so far: https://gist.github.com/2eeba2ff31ebecb526e2 This is the result: https://gist.github.com/cf07fe90922ac3dfcd22
I just posted this to a gist: https://gist.github.com/2228570 var out = ''; function doWhat(){
I have 2 python scripts https://gist.github.com/2233477 . rsgen.py generates random inputs for use in
Here is my (censored) config/deploy.rb: https://gist.github.com/eedf6c976f632c1242cf For some reason, any changes to my assets,
I am using the middleware provided in https://gist.github.com/426829 to do cross site scripting. However,
UPDATE 9th june 2012: Setup with mongoid 3.0.0.rc at heroku, see this gist: https://gist.github.com/2900804
I have a ivy.xml - https://gist.github.com/1898060 I also have the jar file related to
In the code available here : https://gist.github.com/1338861 if I would include the code of
In the code presented on the gist : https://gist.github.com/1341600 I am trying to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.