MCPcopy Create free account
hub / github.com/BaseXdb/basex / toIf

Method toIf

basex-core/src/main/java/org/basex/query/expr/Switch.java:170–189  ·  view source on GitHub ↗

Rewrites the switch to an if expression. @param cc compilation context @return new or original expression @throws QueryException query exception

(final CompileContext cc)

Source from the content-addressed store, hash-verified

168 * @throws QueryException query exception
169 */
170 private Expr toIf(final CompileContext cc) throws QueryException {
171 if(groups.length != 2) return this;
172
173 final SeqType st = cond.seqType();
174 final boolean string = st.type.isStringOrUntyped(), dec = st.type.instanceOf(BasicType.DECIMAL);
175 if(!st.one() || !(string || dec)) return this;
176
177 final Expr[] exprs = groups[0].exprs;
178 for(int e = exprs.length - 1; e >= 1; e--) {
179 final SeqType est = exprs[e].seqType();
180 if(!est.oneOrMore() || !(
181 string && est.type.isStringOrUntyped() ||
182 dec && est.type.instanceOf(BasicType.DECIMAL)
183 )) return this;
184 }
185
186 final Expr list = List.get(cc, groups[0].info, Arrays.copyOfRange(exprs, 1, exprs.length));
187 final CmpG cmp = new CmpG(groups[0].info, cond, list, CmpOp.EQ);
188 return new If(info, cmp.optimize(cc), groups[0].rtrn(), groups[1].rtrn()).optimize(cc);
189 }
190
191 @Override
192 public Iter iter(final QueryContext qc) throws QueryException {

Callers 1

optMethod · 0.95

Calls 9

oneMethod · 0.95
oneOrMoreMethod · 0.95
getMethod · 0.95
optimizeMethod · 0.95
rtrnMethod · 0.80
seqTypeMethod · 0.65
isStringOrUntypedMethod · 0.65
instanceOfMethod · 0.65
optimizeMethod · 0.45

Tested by

no test coverage detected