Switch expression. @author BaseX Team, BSD License @author Christian Gruen
| 24 | * @author Christian Gruen |
| 25 | */ |
| 26 | public final class Switch extends ParseExpr { |
| 27 | /** Condition. */ |
| 28 | private Expr cond; |
| 29 | /** Case groups. */ |
| 30 | private SwitchGroup[] groups; |
| 31 | |
| 32 | /** |
| 33 | * Constructor. |
| 34 | * @param info input info (can be {@code null}) |
| 35 | * @param cond condition |
| 36 | * @param groups case groups (last one is default case) |
| 37 | */ |
| 38 | public Switch(final InputInfo info, final Expr cond, final SwitchGroup[] groups) { |
| 39 | super(info, Types.ITEM_ZM); |
| 40 | this.cond = cond; |
| 41 | this.groups = groups; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void checkUp() throws QueryException { |
| 46 | checkNoUp(cond); |
| 47 | for(final SwitchGroup group : groups) group.checkUp(); |
| 48 | // check if none or all return expressions are updating |
| 49 | final ExprList rtrns = new ExprList(groups.length); |
| 50 | for(final SwitchGroup group : groups) rtrns.add(group.rtrn()); |
| 51 | checkAllUp(rtrns.finish()); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public Expr compile(final CompileContext cc) throws QueryException { |
| 56 | cond = cond.compile(cc); |
| 57 | for(final SwitchGroup group : groups) group.compile(cc); |
| 58 | return optimize(cc); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public Expr optimize(final CompileContext cc) throws QueryException { |
| 63 | cond = cond.simplifyFor(Simplify.STRING, cc); |
| 64 | |
| 65 | // check if expression can be pre-evaluated |
| 66 | final Expr expr = opt(cc); |
| 67 | if(expr != this) return cc.replaceWith(this, expr); |
| 68 | |
| 69 | // combine types of return expressions |
| 70 | exprType.assign(SeqType.union(groups, true)).data(groups); |
| 71 | |
| 72 | return this; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Expr simplifyFor(final Simplify mode, final CompileContext cc) throws QueryException { |
| 77 | boolean changed = false; |
| 78 | for(final SwitchGroup group : groups) { |
| 79 | changed |= group.simplifyFor(mode, cc) == null; |
| 80 | } |
| 81 | return changed ? optimize(cc) : super.simplifyFor(mode, cc); |
| 82 | } |
| 83 |
nothing calls this directly
no outgoing calls
no test coverage detected