summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/profile/SingleValueQueryProfileVisitor.java
blob: c1bb6c8829ef25ef557c2ec2254b37d2b6e388d3 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile;

import java.util.List;

/**
 * Visitor which stores the first non-query-profile value encountered,
 * or the first query profile encountered at a stop where we do not have any name components left which can be used to
 * visit further subprofiles. Hence this may be used both to get the highest prioritized primitive
 * value, or query profile, whichever is encountered first which matches the name.
 * <p>
 *
 * @author bratseth
 */
final class SingleValueQueryProfileVisitor extends QueryProfileVisitor {

    /** The value found, or null if none */
    private Object value=null;

    private final List<String> name;

    private int nameIndex=-1;

    private final boolean allowQueryProfileResult;

    private boolean enteringContent=true;

    public SingleValueQueryProfileVisitor(List<String> name,boolean allowQueryProfileResult) {
        this.name=name;
        this.allowQueryProfileResult=allowQueryProfileResult;
    }

    @Override
    public String getLocalKey() {
        return name.get(nameIndex);
    }

    @Override
    public boolean enter(String name) {
        if (nameIndex+1<this.name.size()) {
            nameIndex++;
            enteringContent=true;
        }
        else {
            enteringContent=false;
        }
        return enteringContent;
    }

    @Override
    public void leave(String name) {
        nameIndex--;
    }

    @Override
    public void onValue(String key,Object value, DimensionBinding binding, QueryProfile owner) {
        if (nameIndex==name.size()-1)
            this.value=value;
    }

    @Override
    public void onQueryProfile(QueryProfile profile,DimensionBinding binding, QueryProfile owner) {
        if (enteringContent) return; // still waiting for content
        if (allowQueryProfileResult)
            this.value = profile;
        else
            this.value = profile.getValue();
    }

    @Override
    public boolean isDone() {
        return value!=null;
    }

    /** Returns the value found during visiting, or null if none */
    public Object getResult() { return value; }

    @Override
    public String toString() {
        return "a single value visitor (hash " + hashCode() + ") with current value " + value;
    }

}