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

/**
 * This class represents an aggregated value in a {@link GroupingExpression}. Because it operates on a list of data, it
 * can not be used as a document-level expression (i.e. level 0, see {@link GroupingExpression#resolveLevel(int)}). The
 * contained expression is evaluated at the level of the aggregator minus 1.
 *
 * @author Simon Thoresen Hult
 */
public abstract class AggregatorNode extends GroupingExpression {

    private final GroupingExpression exp;

    protected AggregatorNode(String image, String label, Integer level) {
        super(image + "()", label, level);
        this.exp = null;
    }

    protected AggregatorNode(String image, String label, Integer level, GroupingExpression exp) {
        super(image + "(" + exp.toString() + ")", label, level);
        this.exp = exp;
    }

    /**
     * Returns the expression that this node aggregates on.
     *
     * @return The expression.
     */
    public GroupingExpression getExpression() {
        return exp;
    }

    @Override
    public void resolveLevel(int level) {
        super.resolveLevel(level);
        if (level < 1) {
            throw new IllegalArgumentException("Expression '" + this + "' not applicable for " +
                                               GroupingOperation.getLevelDesc(level) + ".");
        }
        if (exp != null) {
            exp.resolveLevel(level - 1);
        }
    }

    @Override
    public void visit(ExpressionVisitor visitor) {
        super.visit(visitor);
        if (exp != null) {
            exp.visit(visitor);
        }
    }
}