package code.ast;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import models.algebra.Type;
public class VariableDeclaration extends ASTNode implements IAnnotatable {
private Type type;
private String variableName;
private Expression optionalInitializer;
private Map<String, Annotation> annotations = new HashMap<>();
public VariableDeclaration(Type type, String variableName) {
this.type = type;
this.variableName = variableName;
}
public VariableDeclaration(Type type, String variableName, Expression initializer) {
this(type, variableName);
this.optionalInitializer = initializer;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public String getName() {
return variableName;
}
public void setName(String variableName) {
this.variableName = variableName;
}
public Expression getInitializer() { return optionalInitializer; }
public void setInitializer(Expression initializer) { this.optionalInitializer = initializer; }
@Override
public Annotation getAnnotation(String name) {
return annotations.get(name);
}
@Override
public Collection<Annotation> getAnnotations() {
return annotations.values();
}
@Override
public void addAnnotation(Annotation annotation) {
annotations.put(annotation.getElementName(), annotation);
}
public String toString() {
String code = "";
for (Annotation annotation: getAnnotations()) {
code += annotation.toString() + " ";
}
code += type.getInterfaceTypeName() + " " + variableName;
return code;
}
}