001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: package de.uka.ilkd.key.java.statement;
012:
013: import de.uka.ilkd.key.java.*;
014: import de.uka.ilkd.key.java.visitor.Visitor;
015: import de.uka.ilkd.key.util.ExtList;
016:
017: /**
018: * Switch.
019: */
020:
021: public class Switch extends BranchStatement implements
022: ExpressionContainer, VariableScope, TypeScope {
023:
024: /**
025: * Branches.
026: */
027:
028: protected final ArrayOfBranch branches;
029:
030: /**
031: * Expression.
032: */
033:
034: protected final Expression expression;
035:
036: /**
037: * Switch.
038: */
039:
040: public Switch() {
041: this .branches = null;
042: this .expression = null;
043: }
044:
045: /**
046: * Switch.
047: * @param e an expression.
048: */
049:
050: public Switch(Expression e) {
051: this .branches = null;
052: this .expression = e;
053: }
054:
055: /**
056: * Switch.
057: * @param e an expression.
058: * @param branches a branch array
059: */
060:
061: public Switch(Expression e, Branch[] branches) {
062: this .branches = new ArrayOfBranch(branches);
063: this .expression = e;
064: }
065:
066: /**
067: * Switch.
068: * @param children a list with all children
069: */
070:
071: public Switch(ExtList children) {
072: super (children);
073: this .expression = (Expression) children.get(Expression.class);
074: this .branches = new ArrayOfBranch((Branch[]) children
075: .collect(Branch.class));
076: }
077:
078: /**
079: * Returns the number of children of this node.
080: * @return an int giving the number of children of this node
081: */
082:
083: public int getChildCount() {
084: int result = 0;
085: if (expression != null)
086: result++;
087: if (branches != null)
088: result += branches.size();
089: return result;
090: }
091:
092: /**
093: * Returns the child at the specified index in this node's "virtual"
094: * child array
095: * @param index an index into this node's "virtual" child array
096: * @return the program element at the given position
097: * @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
098: * of bounds
099: */
100:
101: public ProgramElement getChildAt(int index) {
102: if (expression != null) {
103: if (index == 0)
104: return expression;
105: index--;
106: }
107: if (branches != null) {
108: return branches.getBranch(index);
109: }
110: throw new ArrayIndexOutOfBoundsException();
111: }
112:
113: /**
114: * Get the number of expressions in this container.
115: * @return the number of expressions.
116: */
117:
118: public int getExpressionCount() {
119: return (expression != null) ? 1 : 0;
120: }
121:
122: /*
123: Return the expression at the specified index in this node's
124: "virtual" expression array.
125: @param index an index for an expression.
126: @return the expression with the given index.
127: @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
128: of bounds.
129: */
130:
131: public Expression getExpressionAt(int index) {
132: if (expression != null && index == 0) {
133: return expression;
134: }
135: throw new ArrayIndexOutOfBoundsException();
136: }
137:
138: /**
139: * Get expression.
140: * @return the expression.
141: */
142:
143: public Expression getExpression() {
144: return expression;
145: }
146:
147: /**
148: * Get the number of branches in this container.
149: * @return the number of branches.
150: */
151:
152: public int getBranchCount() {
153: return (branches != null) ? branches.size() : 0;
154: }
155:
156: /*
157: Return the branch at the specified index in this node's
158: "virtual" branch array.
159: @param index an index for a branch.
160: @return the branch with the given index.
161: @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
162: of bounds.
163: */
164:
165: public Branch getBranchAt(int index) {
166: if (branches != null) {
167: return branches.getBranch(index);
168: }
169: throw new ArrayIndexOutOfBoundsException();
170: }
171:
172: /* Return the branch array wrapper
173: * @return the array wrapper of the branches
174: */
175: public ArrayOfBranch getBranchList() {
176: return branches;
177: }
178:
179: /** calls the corresponding method of a visitor in order to
180: * perform some action/transformation on this element
181: * @param v the Visitor
182: */
183: public void visit(Visitor v) {
184: v.performActionOnSwitch(this );
185: }
186:
187: public void prettyPrint(PrettyPrinter p) throws java.io.IOException {
188: p.printSwitch(this);
189: }
190: }
|