package dev.peerat.parser.java; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; import java.util.function.Function; import dev.peerat.parser.Token; import dev.peerat.parser.TokenType; public class Variable extends JavaElement{ public static interface VariableContainer{ void addVariable(Variable variable); } private List annotations; private int mod; private Token type; private Token name; private boolean elips; private Value value; public Variable(List annotations, int mod, Token type, Token name){ this.mod = mod; this.type = type; this.name = name; this.annotations = new ArrayList<>(); } public Variable(List annotations, int mod, Token type, Token name, boolean elips){ this(annotations, mod, type, name); this.elips = elips; } public Variable(List annotations, int mod, Token type, Token name, boolean elips, Value value){ this(annotations, mod, type, name, elips); this.value = value; } @Override public String toString(){ return "Variable[mod="+mod+", type="+type+", name="+name+", elips="+elips+", value="+value+"]"; } public List getAnnotations(){ return this.annotations; } public int getModifier(){ return this.mod; } public Token getType(){ return this.type; } public Token getName(){ return this.name; } public boolean isElipsis(){ return this.elips; } public Value getValue(){ return this.value; } @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)); builder.append(type); if(elips) builder.append("..."); builder.append(name); if(value != null){ builder.append("="); value.build(builder); } } @Override public E find(Function finder){ if(annotations != null){ for(Annotation annotation : this.annotations){ if(finder.apply(annotation)) return (E) annotation; } } return value != null ? finder.apply(value) ? (E)value : null : null; } @Override public void findAll(Function finder, List list){ if(annotations != null){ for(Annotation annotation : this.annotations){ if(finder.apply(annotation)) list.add((E)annotation); annotation.findAll(finder, list); } } if(value != null){ if(finder.apply(value)) list.add((E)value); value.findAll(finder, list); } } }