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

120 lines
3.5 KiB
Java

package dev.peerat.parser.java.builder;
import java.util.ArrayList;
import java.util.List;
import dev.peerat.parser.Token;
import dev.peerat.parser.java.Annotation;
import dev.peerat.parser.java.Function;
import dev.peerat.parser.java.JavaElement;
import dev.peerat.parser.java.Variable;
public class JavaFunctionBuilder extends JavaModifiableBuilder<JavaFunctionBuilder, Function>{
private List<Annotation> annotations;
private String generic;
private String type;
private String name;
private List<Variable> parameters;
private List<Token> throwables;
private List<JavaElement> elements;
public JavaFunctionBuilder annotate(JavaAnnotationBuilder annotation){
if(this.annotations == null) this.annotations = new ArrayList<>();
this.annotations.add(annotation.build());
return this;
}
public JavaFunctionBuilder setGeneric(String generic){
this.generic = generic;
return this;
}
public JavaFunctionBuilder setType(String type){
this.type = type;
return this;
}
public JavaFunctionBuilder setName(String name){
this.name = name;
return this;
}
public JavaFunctionBuilder parameter(JavaVariableBuilder builder){
if(this.parameters == null) this.parameters = new ArrayList<>();
this.parameters.add(builder.build());
return this;
}
public JavaFunctionBuilder throwable(String throwable){
if(this.throwables == null) this.throwables = new ArrayList<>();
this.throwables.add(new Token(0,0,throwable, null));
return this;
}
public JavaFunctionBuilder element(JavaElement element){
if(this.elements == null) this.elements = new ArrayList<>();
this.elements.add(element);
return this;
}
public JavaFunctionBuilder normalize(Function func){
normalizeAnnotations(func);
normalizeSignature(func);
//elements
return this;
}
public JavaFunctionBuilder normalizeAnnotations(Function func){
List<Annotation> annotations = func.getAnnotations();
if(annotations != null && annotations.size() > 0){
if(this.annotations == null) this.annotations = new ArrayList<>();
for(Annotation annotation : annotations){
this.annotations.add(JavaBuilder.ofAnnotation().normalize(annotation).build());
}
}
return this;
}
public JavaFunctionBuilder normalizeSignature(Function func){
this.mod = func.getModifier();
if(func.getGeneric() != null) this.generic = func.getGeneric().getValue();
if(func.getReturnType() != null) this.type = func.getReturnType().getValue();
if(func.getName() != null) this.name = func.getName().getValue();
List<Variable> parameters = func.getParameters();
if(parameters != null && parameters.size() > 0){
if(this.parameters == null) this.parameters = new ArrayList<>();
for(Variable param : parameters) this.parameters.add(JavaBuilder.ofVariable().normalize(param).build());
}
List<Token> throwables = func.getThrowables();
if(throwables != null && throwables.size() > 0){
if(this.throwables == null) this.throwables = new ArrayList<>();
for(Token token : throwables) this.throwables.add(new Token(0,0, token.getValue(), null));
}
return this;
}
public JavaFunctionBuilder clearAnnotations(){
this.annotations = null;
return this;
}
@Override
public Function build(){
Function result = new Function(
this.annotations,
this.mod,
this.generic == null ? null : new Token(0,0, this.generic, null),
this.type == null ? null : new Token(0,0, this.type, null),
new Token(0,0, this.name, null),
this.parameters,
this.throwables
);
result.getElements().addAll(elements);
return result;
}
}