blob: d15608fcbeb7e1732c7f9c99f46e492a6fa68a80 (
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.grouping.request;
import com.yahoo.javacc.UnicodeUtilities;
import java.util.List;
/**
* This class represents an expression in a {@link GroupingOperation}. You may manually construct this expression, or
* you may use the {@link com.yahoo.search.grouping.request.parser.GroupingParser} to generate one from a query-string.
*
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
*/
public abstract class GroupingExpression extends GroupingNode {
private Integer level = null;
protected GroupingExpression(String image) {
super(image);
}
/**
* Resolves the conceptual level of this expression. This level represents the type of data that is consumed by this
* expression, where level 0 is a single hit, level 1 is a group, level 2 is a list of groups, and so forth. This
* method verifies the input level against the expression type, and recursively resolves the level of all argument
* expressions.
*
* @param level The level of the input data.
* @throws IllegalArgumentException Thrown if the level of this expression could not be resolved.
* @throws IllegalStateException Thrown if type failed to accept the number of arguments provided.
*/
public void resolveLevel(int level) {
if (level < 0) {
throw new IllegalArgumentException("Expression '" + this + "' recurses through a single hit.");
}
this.level = level;
}
/**
* Returns the conceptual level of this expression.
*
* @return The level.
* @throws IllegalArgumentException Thrown if the level of this expression has not been resolved.
* @see #resolveLevel(int)
*/
public int getLevel() {
if (level == null) {
throw new IllegalStateException("Level for expression '" + this + "' has not been resolved.");
}
return level;
}
/**
* Recursively calls {@link ExpressionVisitor#visitExpression(GroupingExpression)} for this expression and all of
* its argument expressions.
*
* @param visitor The visitor to call.
*/
public void visit(ExpressionVisitor visitor) {
visitor.visitExpression(this);
}
/**
* Returns a string description of the given list of expressions. This is a comma-separated list of the expressions
* own {@link GroupingExpression#toString()} output.
*
* @param lst The list of expressions to output.
* @return The string description.
*/
public static String asString(List<GroupingExpression> lst) {
StringBuilder ret = new StringBuilder();
for (int i = 0, len = lst.size(); i < len; ++i) {
ret.append(lst.get(i));
if (i < len - 1) {
ret.append(", ");
}
}
return ret.toString();
}
/**
* Returns a string representation of an object that can be used in the 'image' constructor argument of {@link
* GroupingNode}. This method ensures that strings are quoted, and that all complex characters are escaped.
*
* @param obj The object to output.
* @return The string representation.
*/
public static String asImage(Object obj) {
if (!(obj instanceof String)) {
return obj.toString();
}
return UnicodeUtilities.quote((String)obj, '"');
}
@Override
public GroupingExpression setLabel(String label) {
super.setLabel(label);
return this;
}
}
|