aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/Result.java
blob: 02a3d0465a6d1f9f83cceeb0f1d8fc5445b3a326 (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
67
68
69
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus;

/**
 * <p>Information on the outcome of <i>initiating</i> a send or forward on a session.
 * The result will tell if the send/forward was accepted or not. If it was accepted,
 * an (asynchroneous) reply is guaranteed to be delivered at some later time.
 * If it was not accepted, a <i>transient error</i> has occured. In that case,
 * {@link #getError} can be used to access the exact error.</p>
 *
 * <p>This class is <b>immutable</b>.
 *
 * @author bratseth
 */
public class Result {

    public static final Result ACCEPTED = new Result();
    private final Error error;

    /**
     * The default constructor is private so that the only error-results can be new'ed by
     * the user. All accepted results should use the public "accepted" constant.
     */
    private Result() {
        error = null;
    }

    /**
     * This constructor assigns a given error to the member variable such that this result
     * becomes unaccepted with a descriptive error.
     *
     * @param error The error to assign to this result.
     */
    public Result(Error error) {
        this.error = error;
    }

    /**
     * This constructor is a convencience function to allow simpler instantiation of a result that contains an error.
     * It does nothing but proxy the {@link #Result(Error)} function with a new instance of {@link Error}.
     *
     * @param code The numerical code of the error.
     * @param message The description of the error.
     */
    public Result(int code, String message) {
        this(new Error(code, message));
    }

    /**
     * Returns whether this message was accepted.
     * If it was accepted, a Reply is guaranteed to be produced for this message
     * at some later time. If it was not accepted, getError can be called to
     * investigate why.
     *
     * @return true if this message was accepted, false otherwise
     */
    public boolean isAccepted() {
        return error == null;
    }

    /**
     * The error resulting from this send/forward if the message was not accepted.
     *
     * @return The error is not accepcted, null if accepted.
     */
    public Error getError() {
        return error;
    }
}