001    // Copyright (c) 2011, the Dart project authors.  Please see the AUTHORS file
002    // for details. All rights reserved. Use of this source code is governed by a
003    // BSD-style license that can be found in the LICENSE file.
004    
005    package com.google.dart.compiler.backend.js.ast;
006    
007    import com.google.dart.compiler.util.AstUtil;
008    import org.jetbrains.annotations.NotNull;
009    
010    import java.util.ArrayList;
011    import java.util.List;
012    
013    /**
014     * A JavaScript switch statement.
015     */
016    public class JsSwitch extends SourceInfoAwareJsNode implements JsStatement {
017    
018        private final List<JsSwitchMember> cases;
019        private JsExpression expression;
020    
021        public JsSwitch() {
022            super();
023            cases = new ArrayList<JsSwitchMember>();
024        }
025    
026        public JsSwitch(JsExpression expression, List<JsSwitchMember> cases) {
027            this.expression = expression;
028            this.cases = cases;
029        }
030    
031        public List<JsSwitchMember> getCases() {
032            return cases;
033        }
034    
035        public JsExpression getExpression() {
036            return expression;
037        }
038    
039        public void setExpression(JsExpression expression) {
040            this.expression = expression;
041        }
042    
043        @Override
044        public void accept(JsVisitor v) {
045            v.visit(this);
046        }
047    
048        @Override
049        public void acceptChildren(JsVisitor visitor) {
050            visitor.accept(expression);
051            visitor.acceptWithInsertRemove(cases);
052        }
053    
054        @Override
055        public void traverse(JsVisitorWithContext v, JsContext ctx) {
056            if (v.visit(this, ctx)) {
057                expression = v.accept(expression);
058                v.acceptList(cases);
059            }
060            v.endVisit(this, ctx);
061        }
062    
063        @NotNull
064        @Override
065        public JsSwitch deepCopy() {
066            JsExpression expressionCopy = AstUtil.deepCopy(expression);
067            List<JsSwitchMember> casesCopy = AstUtil.deepCopy(cases);
068    
069            return new JsSwitch(expressionCopy, casesCopy).withMetadataFrom(this);
070        }
071    }