blob: 3003ce69abea8641428cfcf2c8179c4f30cc3fe8 (
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.grouping.request;
import java.util.*;
/**
* This class represents a function in a {@link GroupingExpression}. Because it operate on other expressions (as opposed
* to {@link AggregatorNode} and {@link DocumentValue} that operate on inputs), this expression type can be used at any
* input level (see {@link GroupingExpression#resolveLevel(int)}).
*
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
*/
public abstract class FunctionNode extends GroupingExpression implements Iterable<GroupingExpression> {
private final List<GroupingExpression> args = new ArrayList<>();
protected FunctionNode(String image, List<GroupingExpression> args) {
super(image + "(" + asString(args) + ")");
this.args.addAll(args);
}
/**
* Returns the number of arguments that were given to this function at construction.
*
* @return The argument count.
*/
public int getNumArgs() {
return args.size();
}
/**
* Returns the argument at the given index.
*
* @param i The index of the argument to return.
* @return The argument at the given index.
* @throws IndexOutOfBoundsException If the index is out of range.
*/
public GroupingExpression getArg(int i) {
return args.get(i);
}
@Override
public Iterator<GroupingExpression> iterator() {
return Collections.unmodifiableList(args).iterator();
}
@Override
public void resolveLevel(int level) {
super.resolveLevel(level);
for (GroupingExpression arg : args) {
arg.resolveLevel(level);
}
}
@Override
public void visit(ExpressionVisitor visitor) {
super.visit(visitor);
for (GroupingExpression arg : args) {
arg.visit(visitor);
}
}
@SuppressWarnings("unchecked")
protected static <T> List<T> asList(T arg1, T... argN) {
return asList(Arrays.asList(arg1), Arrays.asList(argN));
}
@SuppressWarnings("unchecked")
protected static <T> List<T> asList(T arg1, T arg2, T... argN) {
return asList(Arrays.asList(arg1, arg2), Arrays.asList(argN));
}
protected static <T> List<T> asList(List<T> foo, List<T> bar) {
List<T> ret = new LinkedList<>(foo);
ret.addAll(bar);
return ret;
}
}
|