aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/grouping/vespa/OffsetContinuation.java
blob: dc49d5859c85366cd95b3c9c16d6263dc4d81e35 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.grouping.vespa;

/**
 * @author Simon Thoresen Hult
 */
class OffsetContinuation extends EncodableContinuation {

    public static final int FLAG_UNSTABLE = 1;
    private final ResultId resultId;
    private final int tag;
    private final int offset;
    private final int flags;

    public OffsetContinuation(ResultId resultId, int tag, int offset, int flags) {
        resultId.getClass(); // throws NullPointerException
        this.resultId = resultId;
        this.tag = tag;
        this.offset = offset;
        this.flags = flags;
    }

    @Override
    public OffsetContinuation copy() {
        return this; // immutable
    }

    public ResultId getResultId() {
        return resultId;
    }

    public int getTag() {
        return tag;
    }

    public int getOffset() {
        return offset;
    }

    public int getFlags() {
        return flags;
    }

    public boolean testFlag(int flag) {
        return (flags & flag) != 0;
    }

    @Override
    public int hashCode() {
        return resultId.hashCode() + offset + flags;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof OffsetContinuation)) {
            return false;
        }
        OffsetContinuation rhs = (OffsetContinuation)obj;
        if (!resultId.equals(rhs.resultId)) {
            return false;
        }
        if (tag != rhs.tag) {
            return false;
        }
        if (offset != rhs.offset) {
            return false;
        }
        if (flags != rhs.flags) {
            return false;
        }
        return true;
    }

    @Override
    public void encode(IntegerEncoder out) {
        resultId.encode(out);
        out.append(tag);
        out.append(offset);
        out.append(flags);
    }

    public static OffsetContinuation decode(IntegerDecoder in) {
        ResultId resultId = ResultId.decode(in);
        int tag = in.next();
        int offset = in.next();
        int flags = in.next();
        return new OffsetContinuation(resultId, tag, offset, flags);
    }

}