All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hisrc.jscm.codemodel.statement.impl.SwitchStatementImpl Maven / Gradle / Ivy

The newest version!
package org.hisrc.jscm.codemodel.statement.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.hisrc.jscm.codemodel.JSCodeModel;
import org.hisrc.jscm.codemodel.expression.JSExpression;
import org.hisrc.jscm.codemodel.lang.Validate;
import org.hisrc.jscm.codemodel.statement.JSStatement;
import org.hisrc.jscm.codemodel.statement.JSStatementVisitor;
import org.hisrc.jscm.codemodel.statement.JSSwitchStatement;

public class SwitchStatementImpl extends StatementImpl implements
		JSSwitchStatement {

	private final JSExpression expression;

	private final List firstCaseClauses = new ArrayList();

	private final List unmodifiableFirstCaseClauses = Collections
			.unmodifiableList(firstCaseClauses);

	private JSDefaultClause defaultClause;

	private final List secondCaseClauses = new ArrayList();

	private final List unmodifiableSecondCaseClauses = Collections
			.unmodifiableList(secondCaseClauses);

	public SwitchStatementImpl(JSCodeModel codeModel, JSExpression expression) {
		super(codeModel);
		Validate.notNull(expression);
		this.expression = expression;
	}

	@Override
	public JSCaseClause _case(JSExpression expression) {
		final JSCaseClause caseClause = new CaseClauseImpl(getCodeModel(),
				expression);
		if (this.defaultClause == null) {
			this.firstCaseClauses.add(caseClause);
		} else {
			this.secondCaseClauses.add(caseClause);
		}
		return caseClause;
	}

	@Override
	public JSDefaultClause _default() {
		if (this.defaultClause == null) {
			this.defaultClause = new DefaultClauseImpl(getCodeModel());
		}
		return defaultClause;
	}

	@Override
	public JSExpression getExpression() {
		return expression;
	}

	@Override
	public List getFirstCaseClauses() {
		return unmodifiableFirstCaseClauses;
	}

	@Override
	public JSDefaultClause getDefaultClause() {
		return defaultClause;
	}

	@Override
	public List getSecondCaseClauses() {
		return unmodifiableSecondCaseClauses;
	}

	@Override
	public  V acceptStatementVisitor(
			JSStatementVisitor visitor) throws E {
		return visitor.visitSwitch(this);
	}

	public static class DefaultClauseImpl extends StatementGeneratorImpl
			implements JSDefaultClause {

		private final List statements = new ArrayList();

		private final List unmodifiableStatements = Collections
				.unmodifiableList(statements);

		public DefaultClauseImpl(JSCodeModel codeModel) {
			super(codeModel);
		}

		@Override
		public List getStatements() {
			return unmodifiableStatements;
		}

		@Override
		protected  S add(S statement) {
			this.statements.add(statement);
			return statement;
		}

	}

	public static class CaseClauseImpl extends StatementGeneratorImpl implements
			JSCaseClause {

		private final JSExpression expression;

		private final List statements = new ArrayList();

		private final List unmodifiableStatements = Collections
				.unmodifiableList(statements);

		public CaseClauseImpl(JSCodeModel codeModel, JSExpression expression) {
			super(codeModel);
			Validate.notNull(expression);
			this.expression = expression;
		}

		@Override
		public JSExpression getExpression() {
			return expression;
		}

		@Override
		public List getStatements() {
			return unmodifiableStatements;
		}

		@Override
		protected  S add(S statement) {
			this.statements.add(statement);
			return statement;
		}

	}

}