01: /*
02: * The contents of this file are subject to the terms
03: * of the Common Development and Distribution License
04: * (the "License"). You may not use this file except
05: * in compliance with the License.
06: *
07: * You can obtain a copy of the license at
08: * https://jwsdp.dev.java.net/CDDLv1.0.html
09: * See the License for the specific language governing
10: * permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL
13: * HEADER in each file and include the License file at
14: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
15: * add the following below this CDDL HEADER, with the
16: * fields enclosed by brackets "[]" replaced with your
17: * own identifying information: Portions Copyright [yyyy]
18: * [name of copyright owner]
19: */
20: package com.sun.codemodel;
21:
22: import java.util.ArrayList;
23: import java.util.Iterator;
24: import java.util.List;
25:
26: /**
27: * Switch statement
28: */
29: public final class JSwitch implements JStatement {
30:
31: /**
32: * Test part of switch statement.
33: */
34: private JExpression test;
35:
36: /**
37: * vector of JCases.
38: */
39: private List<JCase> cases = new ArrayList<JCase>();
40:
41: /**
42: * a single default case
43: */
44: private JCase defaultCase = null;
45:
46: /**
47: * Construct a While statment
48: */
49: JSwitch(JExpression test) {
50: this .test = test;
51: }
52:
53: public JExpression test() {
54: return test;
55: }
56:
57: public Iterator cases() {
58: return cases.iterator();
59: }
60:
61: public JCase _case(JExpression label) {
62: JCase c = new JCase(label);
63: cases.add(c);
64: return c;
65: }
66:
67: public JCase _default() {
68: // what if (default != null) ???
69:
70: // default cases statements don't have a label
71: defaultCase = new JCase(null, true);
72: return defaultCase;
73: }
74:
75: public void state(JFormatter f) {
76: if (JOp.hasTopOp(test)) {
77: f.p("switch ").g(test).p(" {").nl();
78: } else {
79: f.p("switch (").g(test).p(')').p(" {").nl();
80: }
81: for (JCase c : cases)
82: f.s(c);
83: if (defaultCase != null)
84: f.s(defaultCase);
85: f.p('}').nl();
86: }
87:
88: }
|