package be.jeffcheasey88.peeratcode.parser.java; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; import be.jeffcheasey88.peeratcode.parser.Token; import be.jeffcheasey88.peeratcode.parser.TokenType; import be.jeffcheasey88.peeratcode.parser.java.Operation.OperationContainer; import be.jeffcheasey88.peeratcode.parser.java.Variable.VariableContainer; public class Function extends JavaElement implements VariableContainer, OperationContainer{ public static interface FunctionContainer{ void addFunction(Function function); } private List annotations; private int mod; private Token generic; private Token type; private Token name; private List parameters; private List throwables; private List elements; public Function(List annotations, int mod, Token type, Token name){ this.annotations = annotations; this.mod = mod; this.type = type; this.name = name; this.elements = new ArrayList<>(); } public Function(List annotations, int mod, Token generic, Token type, Token name){ this(annotations, mod, type, name); this.generic = generic; } public Function(List annotations, int mod, Token generic, Token type, Token name, List parameters){ this(annotations, mod, generic, type, name); this.parameters = parameters; } public Function(List annotations, int mod, Token generic, Token type, Token name, List parameters, List throwables){ this(annotations, mod, generic, type, name, parameters); this.throwables = throwables; } @Override public void addVariable(Variable variable) { this.elements.add(variable); } @Override public void addOperation(Operation operation) { this.elements.add(operation); } public List getAnnotations(){ return this.annotations; } public int getModifier(){ return this.mod; } public Token getGeneric(){ return this.generic; } public Token getReturnType(){ return this.type; } public Token getName(){ return this.name; } public List getParameters(){ return this.parameters; } public List getThrowables(){ return this.throwables; } public List getElements(){ return this.elements; } @Override public void build(Builder builder) throws Exception{ if(annotations != null){ for(Annotation annotation : this.annotations){ annotation.build(builder); } } String mod = Modifier.toString(this.mod); builder.append(new Token(type.getLineNumber(), type.getCharacterNumber()-(mod.length()+1), mod, TokenType.GROUP)); if(generic != null) builder.append(generic); builder.append(type); builder.append(name); builder.append("("); if(parameters != null){ for(Variable param : this.parameters){ param.build(builder); } } builder.append(")"); if(throwables != null){ builder.append("throws"); for(Token token : throwables){ builder.append(token); } } builder.append("{"); if(elements != null){ for(JavaElement content : this.elements){ content.build(builder); } } builder.append("}"); } @Override public E find(java.util.function.Function finder){ if(annotations != null){ for(Annotation annotation : this.annotations){ if(finder.apply(annotation)) return (E) annotation; } } if(parameters != null){ for(Variable param : this.parameters){ if(finder.apply(param)) return (E) param; } } if(elements != null){ for(JavaElement content : this.elements){ if(finder.apply(content)) return (E) content; } } return null; } }