aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/expression/AttributeNode.java
blob: e5dac72938f4791124dfa1de2e0cef33721c99bc (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.expression;

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

/**
 * This function is an instruction to retrieve the value of a named attribute.
 *
 * @author baldersheim
 * @author Simon Thoresen Hult
 */
public class AttributeNode extends FunctionNode {

    public static final int classId = registerClass(0x4000 + 55, AttributeNode.class, AttributeNode::new);
    private String attribute;

    /**
     * Constructs an empty result node. <b>NOTE:</b> This instance is broken until non-optional member data is set.
     */
    public AttributeNode() { }

    /**
     * Constructs an instance of this class with given attribute name.
     *
     * @param attribute The attribute to retrieve.
     */
    public AttributeNode(String attribute) {
        setAttributeName(attribute);
    }

    /**
     * Returns the name of the attribute whose value this function is to retrieve.
     *
     * @return The attribute name.
     */
    public String getAttributeName() {
        return attribute;
    }

    /**
     * Sets the name of the attribute whose value this function is to retrieve.
     *
     * @param attribute The attribute to retrieve.
     * @return This, to allow chaining.
     */
    public AttributeNode setAttributeName(String attribute) {
        if (attribute == null) {
            throw new IllegalArgumentException("Attribute name can not be null.");
        }
        this.attribute = attribute;
        return this;
    }

    @Override
    protected int onGetClassId() {
        return classId;
    }

    @Override
    protected void onSerialize(Serializer buf) {
        super.onSerialize(buf);
        putUtf8(buf, attribute);
    }

    @Override
    protected void onDeserialize(Deserializer buf) {
        super.onDeserialize(buf);
        attribute = getUtf8(buf);
    }

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

    @Override
    protected boolean equalsFunction(FunctionNode obj) {
        return attribute.equals(((AttributeNode)obj).attribute);
    }

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