aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/main/java/com/yahoo/documentapi/DocumentResponse.java
blob: 9b79207bac9a2fd602257d0cf90b895674729968 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentapi;

import com.yahoo.document.Document;
import com.yahoo.messagebus.Trace;

/**
 * The asynchronous response to a document put or get operation.
 * This is a <i>value object</i>.
 *
 * @author Einar M R Rosenvinge
 */
public class DocumentResponse extends Response {

    /** The document of this response, if any */
    private final Document document;

    /** Creates a successful response */
    public DocumentResponse(long requestId) {
        this(requestId, null);
    }

    /**
     * Creates a successful response containing a document
     *
     * @param document the Document to encapsulate in the Response
     */
    public DocumentResponse(long requestId, Document document) {
        this(requestId, document, null);
    }

    /**
     * Creates a successful response containing a document
     *
     * @param document the Document to encapsulate in the Response
     */
    public DocumentResponse(long requestId, Document document, Trace trace) {
        this(requestId, document, null, document != null ? Outcome.SUCCESS : Outcome.NOT_FOUND, trace);
    }

    /**
     * Creates a response containing a textual message and/or a document
     *
     * @param document    the Document to encapsulate in the Response
     * @param textMessage the message to encapsulate in the Response
     * @param outcome     the outcome of this operation
     */
    public DocumentResponse(long requestId, Document document, String textMessage, Outcome outcome) {
        this(requestId, document, textMessage, outcome, null);
    }


    /**
     * Creates a response containing a textual message and/or a document
     *
     * @param document    the Document to encapsulate in the Response
     * @param textMessage the message to encapsulate in the Response
     * @param outcome     the outcome of this operation
     */
    public DocumentResponse(long requestId, Document document, String textMessage, Outcome outcome, Trace trace) {
        super(requestId, textMessage, outcome, trace);
        this.document = document;
    }


    /**
     * Returns the document of this response, or null if there is none
     *
     * @return the Document, or null
     */
    public Document getDocument() { return document; }

    @Override
    public boolean isSuccess() {
        // TODO: is it right that Get operations are successful without a result, in this API?
        return super.isSuccess() || outcome() == Outcome.NOT_FOUND;
    }

    public int hashCode() {
        return super.hashCode() + (document == null ? 0 : document.hashCode());
    }

    public boolean equals(Object o) {
        if (!(o instanceof DocumentResponse)) {
            return false;
        }

        DocumentResponse docResp = (DocumentResponse) o;

        return super.equals(docResp) && ((document == null && docResp.document == null) ||
                (document != null && docResp.document != null && document.equals(docResp.document)));
    }

    public String toString() {
        return "Document" + super.toString() + (document == null ? "" : " " + document);
    }

}