/* Generated By:JJTree: Do not edit this line. CaseExpression.java Version 4.3 */
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=true,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
package org.teiid.query.sql.symbol;
import java.util.Collections;
import java.util.List;
import org.teiid.designer.query.sql.symbol.ICaseExpression;
import org.teiid.designer.runtime.version.spi.ITeiidServerVersion;
import org.teiid.query.parser.LanguageVisitor;
import org.teiid.query.sql.lang.SimpleNode;
import org.teiid.runtime.client.Messages;
/**
*
*/
public class CaseExpression extends SimpleNode implements Expression, ICaseExpression<LanguageVisitor> {
private Class type;
/**
* The expression whose evaluation is being tested in this case expression.
*/
private Expression expression = null;
/**
* Ordered List of Expressions in the WHEN parts of this expression.
*/
private List<Expression> when = null;
/** Ordered List containing Expression objects. */
private List<Expression> then = null;
/** The (optional) expression in the ELSE part of the expression */
private Expression elseExpression = null;
/**
* @param p
* @param id
*/
public CaseExpression(ITeiidServerVersion p, int id) {
super(p, id);
}
@Override
public Class getType() {
return type;
}
/**
* Gets the expression whose evaluation is being tested in this case expression.
* @return expression
*/
@Override
public Expression getExpression() {
return expression;
}
/**
* Sets the expression for this case expression
* @param expr a non-null Expression
*/
public void setExpression(Expression expr) {
if (expr == null) {
throw new IllegalArgumentException(Messages.getString(Messages.ERR.ERR_015_010_0035));
}
this.expression = expr;
}
@Override
public int getWhenCount() {
return (when == null) ? 0 : when.size();
}
/**
* Gets the List of Expressions in the WHEN parts of this expression. Never null.
*
* @return list of whens
*/
public List<Expression> getWhen() {
return when;
}
/**
* Gets the WHEN expression at the given 0-based index.
* @param index
*
* @return when expression
*/
@Override
public Expression getWhenExpression(int index) {
return when.get(index);
}
/**
* Sets the WHEN and THEN parts of this CASE expression.
* Both lists should have the same number of Expressions.
* @param when a non-null List of at least one Expression
* @param then a non-null List of at least one Expression
*/
public void setWhen(List<Expression> when, List<Expression> then) {
if (when == null || then == null) {
throw new IllegalArgumentException(Messages.getString(Messages.ERR.ERR_015_010_0036));
}
if (when.size() != then.size() ||
when.size() < 1) {
throw new IllegalArgumentException(Messages.getString(Messages.ERR.ERR_015_010_0036));
}
if (this.when != when) {
this.when = Collections.unmodifiableList(when);
}
setThen(then);
}
/**
* Gets the expression of the THEN part at the given index.
* @param index
*
* @return then expression
*/
@Override
public Expression getThenExpression(int index) {
return then.get(index);
}
/**
* Gets the List of THEN expressions in this CASE expression. Never null.
*
* @return then expressions
*/
public List<Expression> getThen() {
return then;
}
/**
* Sets the List of THEN expressions in this CASE expression
* @param then
*/
protected void setThen(List<Expression> then) {
if (this .then != then) {
this.then = Collections.unmodifiableList(then);
}
}
/**
* Gets the expression in the ELSE part of this expression. May be null as
* the ELSE is optional.
*
* @return else expression
*/
@Override
public Expression getElseExpression() {
return elseExpression;
}
/**
* Sets the expression in the ELSE part of this expression. Can be null.
* @param elseExpression
*/
public void setElseExpression(Expression elseExpression) {
this.elseExpression = elseExpression;
}
/**
* Sets the type to which this expression has resolved.
* @param type
*/
public void setType(Class type) {
this.type = type;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((this.elseExpression == null) ? 0 : this.elseExpression.hashCode());
result = prime * result + ((this.expression == null) ? 0 : this.expression.hashCode());
result = prime * result + ((this.then == null) ? 0 : this.then.hashCode());
result = prime * result + ((this.type == null) ? 0 : this.type.hashCode());
result = prime * result + ((this.when == null) ? 0 : this.when.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!super.equals(obj)) return false;
if (getClass() != obj.getClass()) return false;
CaseExpression other = (CaseExpression)obj;
if (this.elseExpression == null) {
if (other.elseExpression != null) return false;
} else if (!this.elseExpression.equals(other.elseExpression)) return false;
if (this.expression == null) {
if (other.expression != null) return false;
} else if (!this.expression.equals(other.expression)) return false;
if (this.then == null) {
if (other.then != null) return false;
} else if (!this.then.equals(other.then)) return false;
if (this.type == null) {
if (other.type != null) return false;
} else if (!this.type.equals(other.type)) return false;
if (this.when == null) {
if (other.when != null) return false;
} else if (!this.when.equals(other.when)) return false;
return true;
}
/** Accept the visitor. **/
@Override
public void acceptVisitor(LanguageVisitor visitor) {
visitor.visit(this);
}
@Override
public CaseExpression clone() {
CaseExpression clone = new CaseExpression(getTeiidVersion(), this.id);
if(getExpression() != null)
clone.setExpression(getExpression().clone());
if(getWhen() != null)
clone.setWhen(cloneList(getWhen()), cloneList(getThen()));
if(getElseExpression() != null)
clone.setElseExpression(getElseExpression().clone());
if(getType() != null)
clone.setType(getType());
return clone;
}
}
/* JavaCC - OriginalChecksum=cd01de33a53d47d00a2b5e980a68efc3 (do not edit this line) */