it.uniroma2.art.coda.pearl.model.annotation.Annotation Maven / Gradle / Ivy
package it.uniroma2.art.coda.pearl.model.annotation;
import it.uniroma2.art.coda.exception.parserexception.MissingMandatoryParamInAnnotationException;
import it.uniroma2.art.coda.pearl.model.annotation.param.ParamValueInterface;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Annotation {
private AnnotationDefinition annotationDefinition;
private String name;
private List targetList;
private Map> paramMap;
public Annotation(String name, AnnotationDefinition annotationDefinition) {
this.name = name;
this.annotationDefinition = annotationDefinition;
paramMap = new HashMap<>();
targetList = new ArrayList<>();
}
public AnnotationDefinition getAnnotationDefinition(){
return annotationDefinition;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addTarget(String target){
if(!targetList.contains(target)){
targetList.add(target);
}
}
public List getTargetList(){
return targetList;
}
public void addParams(String name, List valueList){
if(!paramMap.containsKey(name)){
paramMap.put(name, valueList);
List valueListRedux = new ArrayList<>();
for(ParamValueInterface input : valueList){
boolean add = true;
for(ParamValueInterface existing : valueListRedux){
if(existing.asString().equals(input.asString())){
add = false;
}
}
if(add){
valueListRedux.add(input);
}
}
paramMap.put(name, valueListRedux);
} else{
//the map already contains this name, so add the values (avoid duplicates)
List existingValueList = paramMap.get(name);
for(ParamValueInterface input : valueList){
boolean add = true;
for(ParamValueInterface existing : existingValueList){
if(existing.asString().equals(input.asString())){
add = false;
}
}
if(add){
existingValueList.add(input);
}
}
}
}
public List getParamValueList(String name){
if(paramMap.get(name)==null){
//if the annotation do not have such param, check if the annotationDefinition has a default value for such param
if(annotationDefinition.getParamDefinition(name) != null && annotationDefinition.getParamDefinition(name).hasDefault){
List pviList = new ArrayList<>();
pviList.add(annotationDefinition.getParamDefinition(name).getDefaultValue());
return pviList;
}
}
return paramMap.get(name);
}
public Map> getParamMap(){
Map> completeParamMap = new HashMap<>();
//return not just the param with an explicit value, but also the default one, of no explicit value has been set
for(ParamDefinition paramDefinition : annotationDefinition.getParamDefinitionList()){
if(!paramMap.containsKey(paramDefinition.getName())){
//the explicit paramMap does not have a value for the parameter, so add the default one
if(paramDefinition.hasDefault){
List pviList = new ArrayList<>();
pviList.add(paramDefinition.getDefaultValue());
completeParamMap.put(paramDefinition.getName(), pviList);
} else {
//this should never happen
}
} else{
completeParamMap.put(paramDefinition.getName(), paramMap.get(paramDefinition.getName()));
}
}
return completeParamMap;
}
}