aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/expression/StringBucketResultNode.java
blob: ccd1364a58179eb71a8ce9ffbee476b6e637fb35 (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
106
107
108
109
// 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 is an integer bucket value
 *
 * @author <a href="mailto:havardpe@yahoo-inc.com">Haavard Pettersen</a>
 * @author Simon Thoresen Hult
 */
public class StringBucketResultNode extends BucketResultNode {

    // The global class identifier shared with C++.
    public static final int classId = registerClass(0x4000 + 103, StringBucketResultNode.class, StringBucketResultNode::new);

    // bucket start, inclusive
    private ResultNode from = StringResultNode.getNegativeInfinity();

    // bucket end, exclusive
    private ResultNode to = StringResultNode.getNegativeInfinity();

    @Override
    public boolean empty() {
        return to.equals(from);
    }

    public StringBucketResultNode() {}

    /**
     * Create a bucket with the given limits
     *
     * @param from bucket start
     * @param to   bucket end
     */
    public StringBucketResultNode(ResultNode from, ResultNode to) {
        this.from = from;
        this.to = to;
    }

    /**
     * Create a bucket with the given limits
     *
     * @param from bucket start
     * @param to   bucket end
     */
    public StringBucketResultNode(String from, String to) {
        this(new StringResultNode(from), new StringResultNode(to));
    }

    /**
     * Obtain the bucket start
     *
     * @return bucket start
     */
    public String getFrom() {
        return from.getString();
    }

    /**
     * Obtain the bucket end
     *
     * @return bucket end
     */
    public String getTo() {
        return to.getString();
    }

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

    @Override
    protected void onSerialize(Serializer buf) {
        serializeOptional(buf, from);
        serializeOptional(buf, to);
    }

    @Override
    protected void onDeserialize(Deserializer buf) {
        from = (ResultNode)deserializeOptional(buf);
        to = (ResultNode)deserializeOptional(buf);
    }

    @Override
    protected int onCmp(ResultNode rhs) {
        if (classId != rhs.getClassId()) {
            return (classId - rhs.getClassId());
        }
        StringBucketResultNode b = (StringBucketResultNode)rhs;
        int diff = from.compareTo(b.from);
        return (diff == 0) ? to.compareTo(b.to) : diff;
    }

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

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