aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/aggregation/Hit.java
blob: 4da37ac691978fde72ba3b01eeb102c59bf38452 (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
99
100
101
102
103
104
105
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.aggregation;

import com.yahoo.vespa.objects.Deserializer;
import com.yahoo.vespa.objects.Identifiable;
import com.yahoo.vespa.objects.ObjectVisitor;
import com.yahoo.vespa.objects.Serializer;

/**
 * This class represents a generic hit with a rank value. Actual hits are represented using subclasses of this class.
 *
 * @author havardpe
 */
public abstract class Hit extends Identifiable {

    public static final int classId = registerClass(0x4000 + 94, Hit.class); // shared with c++
    private Object context = null;
    private double rank = 0.0;

    /**
     * Constructs an empty result node.
     */
    public Hit() {
        // empty
    }

    /**
     * Create a new hit with the given rank
     *
     * @param rank generic rank value
     */
    public Hit(double rank) {
        this.rank = rank;
    }

    /**
     * Obtain the rank of this hit. This is a comparable rank to allow multilevel sorting on arbitrary rank type.
     *
     * @return generic rank value
     */
    public double getRank() {
        return rank;
    }

    /**
     * Returns the context object of this hit.
     *
     * @return The context object.
     */
    public Object getContext() {
        return context;
    }

    /**
     * Sets the context object of this hit. This is not serialized, and is merely a tag used by the QRS.
     *
     * @param context The context to set.
     * @return This, to allow chaining.
     */
    public Hit setContext(Object context) {
        this.context = context;
        return this;
    }

    @Override
    protected void onSerialize(Serializer buf) {
        super.onSerialize(buf);
        buf.putDouble(null, rank);
    }

    @Override
    protected void onDeserialize(Deserializer buf) {
        super.onDeserialize(buf);
        rank = buf.getDouble(null);
    }

    @Override
    public int hashCode() {
        return super.hashCode() + (int)rank;
    }

    @SuppressWarnings({ "RedundantIfStatement" })
    @Override
    public boolean equals(Object obj) {
        if (!super.equals(obj)) {
            return false;
        }
        Hit rhs = (Hit)obj;
        if (Double.compare(rank, rhs.rank) != 0) {
            return false;
        }
        if (!equals(context, rhs.context)) {
            return false;
        }
        return true;
    }

    @Override
    public void visitMembers(ObjectVisitor visitor) {
        super.visitMembers(visitor);
        visitor.visit("rank", rank);
        visitor.visit("context", context);
    }

}