aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureResponse.java
blob: fea4f84416a941bf48a5b669be099dd8b0c05db6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.handler;

import com.google.common.util.concurrent.AbstractFuture;
import com.yahoo.jdisc.Response;

/**
 * <p>This class provides an implementation of {@link ResponseHandler} that allows you to wait for a {@link Response} to
 * be returned.</p>
 *
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
 */
public final class FutureResponse extends AbstractFuture<Response> implements ResponseHandler {

    private final ResponseHandler handler;

    /**
     * <p>Constructs a new FutureResponse that returns a {@link NullContent} when {@link #handleResponse(Response)} is
     * invoked.</p>
     */
    public FutureResponse() {
        this(NullContent.INSTANCE);
    }

    /**
     * <p>Constructs a new FutureResponse that returns the given {@link ContentChannel} when {@link
     * #handleResponse(Response)} is invoked.</p>
     *
     * @param content The content channel for the Response.
     */
    public FutureResponse(final ContentChannel content) {
        this(new ResponseHandler() {

            @Override
            public ContentChannel handleResponse(Response response) {
                return content;
            }
        });
    }

    /**
     * <p>Constructs a new FutureResponse that calls the given {@link ResponseHandler} when {@link
     * #handleResponse(Response)} is invoked.</p>
     *
     * @param handler The ResponseHandler to invoke.
     */
    public FutureResponse(ResponseHandler handler) {
        this.handler = handler;
    }

    @Override
    public ContentChannel handleResponse(Response response) {
        set(response);
        return handler.handleResponse(response);
    }

    @Override
    public final boolean cancel(boolean mayInterruptIfRunning) {
        throw new UnsupportedOperationException();
    }

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