-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathComparisonExpression.java
More file actions
184 lines (160 loc) · 5.48 KB
/
ComparisonExpression.java
File metadata and controls
184 lines (160 loc) · 5.48 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Authors: Stefan Irimescu, Can Berker Cikis
*
*/
package org.rumbledb.expressions.comparison;
import org.rumbledb.exceptions.ExceptionMetadata;
import org.rumbledb.exceptions.OurBadException;
import org.rumbledb.expressions.AbstractNodeVisitor;
import org.rumbledb.expressions.Expression;
import org.rumbledb.expressions.Node;
import java.util.Arrays;
import java.util.List;
public class ComparisonExpression extends Expression {
private static final long serialVersionUID = 1L;
public static enum ComparisonOperator {
VC_EQ("eq"),
VC_NE("ne"),
VC_LT("lt"),
VC_LE("le"),
VC_GT("gt"),
VC_GE("ge"),
GC_EQ("="),
GC_NE("!="),
GC_LT("<"),
GC_LE("<="),
GC_GT(">"),
GC_GE(">=");
private String name;
private boolean isValueComparison;
ComparisonOperator(String name) {
switch (name) {
case "eq":
case "ne":
case "le":
case "lt":
case "ge":
case "gt":
this.isValueComparison = true;
break;
default:
this.isValueComparison = false;
}
this.name = name;
}
@Override
public String toString() {
return this.name;
}
public boolean isValueComparison() {
return this.isValueComparison;
}
public static ComparisonOperator fromSymbol(String symbol) {
if (symbol.equals(VC_EQ.toString())) {
return VC_EQ;
}
if (symbol.equals(VC_NE.toString())) {
return VC_NE;
}
if (symbol.equals(VC_LT.toString())) {
return VC_LT;
}
if (symbol.equals(VC_LE.toString())) {
return VC_LE;
}
if (symbol.equals(VC_GT.toString())) {
return VC_GT;
}
if (symbol.equals(VC_GE.toString())) {
return VC_GE;
}
if (symbol.equals(GC_EQ.toString())) {
return GC_EQ;
}
if (symbol.equals(GC_NE.toString())) {
return GC_NE;
}
if (symbol.equals(GC_LT.toString())) {
return GC_LT;
}
if (symbol.equals(GC_LE.toString())) {
return GC_LE;
}
if (symbol.equals(GC_GT.toString())) {
return GC_GT;
}
if (symbol.equals(GC_GE.toString())) {
return GC_GE;
}
throw new OurBadException("Unrecognized comparison symbol: " + symbol);
}
};
private Expression leftExpression;
private Expression rightExpression;
private ComparisonOperator comparisonOperator;
public ComparisonExpression(
Expression leftExpression,
Expression rightExpression,
ComparisonOperator comparisonOperator,
ExceptionMetadata metadata
) {
super(metadata);
this.leftExpression = leftExpression;
this.rightExpression = rightExpression;
this.comparisonOperator = comparisonOperator;
}
@Override
public <T> T accept(AbstractNodeVisitor<T> visitor, T argument) {
return visitor.visitComparisonExpr(this, argument);
}
@Override
public List<Node> getChildren() {
return Arrays.asList(this.leftExpression, this.rightExpression);
}
public ComparisonOperator getComparisonOperator() {
return this.comparisonOperator;
}
public void print(StringBuffer buffer, int indent) {
for (int i = 0; i < indent; ++i) {
buffer.append(" ");
}
buffer.append(getClass().getSimpleName());
buffer.append(" (" + (this.comparisonOperator) + ") ");
buffer.append(" | " + this.highestExecutionMode);
buffer.append(" | " + (this.inferredSequenceType == null ? "not set" : this.inferredSequenceType));
buffer.append("\n");
for (Node iterator : getChildren()) {
iterator.print(buffer, indent + 1);
}
}
@Override
public void serializeToJSONiq(StringBuffer sb, int indent) {
indentIt(sb, indent);
sb.append("(\n");
this.leftExpression.serializeToJSONiq(sb, indent + 1);
indentIt(sb, indent);
sb.append(")\n");
indentIt(sb, indent);
sb.append(this.comparisonOperator.toString() + "\n");
indentIt(sb, indent);
sb.append("(\n");
this.rightExpression.serializeToJSONiq(sb, indent + 1);
indentIt(sb, indent);
sb.append(")\n");
}
}