peer-at-code-parser-java/src/dev/peerat/parser/java/builder/JavaAnnotationBuilder.java

56 lines
1.5 KiB
Java

package dev.peerat.parser.java.builder;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import dev.peerat.parser.Token;
import dev.peerat.parser.java.Annotation;
import dev.peerat.parser.java.JavaElement;
import dev.peerat.parser.java.value.Value;
public class JavaAnnotationBuilder extends JavaBuilder<Annotation>{
private String name;
private Map<String, Value> values;
public JavaAnnotationBuilder setName(String name){
this.name = name;
return this;
}
public JavaAnnotationBuilder parameter(String key, Value value){
if(this.values == null) this.values = new HashMap<>();
this.values.put(key, value);
return this;
}
public JavaAnnotationBuilder uniqueParameter(Value value){
return parameter(null, value);
}
public JavaAnnotationBuilder normalize(Annotation annotation){
setName(annotation.getName().getValue());
Map<Token, Value> parameters = annotation.getParameters();
if(parameters != null){
if(this.values == null) this.values = new HashMap<>();
for(Entry<Token, Value> entry : parameters.entrySet()){
this.values.put(entry.getKey() == null ? null : entry.getKey().getValue(), JavaValueBuilder.normalize(entry.getValue()));
}
}
return this;
}
@Override
public Annotation build(){
Map<Token, Value> map = null;
if(values != null){
map = new HashMap<>();
for(Entry<String, Value> entry : values.entrySet()){
map.put(entry.getKey() == null ? null : new Token(0,0,entry.getKey(), null), entry.getValue());
}
}
return new Annotation(new Token(0,0,name, null), map);
}
}