peer-at-code-parser-java/src/dev/peerat/parser/java/Variable.java

111 lines
2.7 KiB
Java

package dev.peerat.parser.java;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import dev.peerat.parser.Token;
import dev.peerat.parser.java.Annotation.Annotable;
import dev.peerat.parser.java.value.Value;
import dev.peerat.parser.visitor.TokenVisitor;
import dev.peerat.parser.visitor.Visitor;
import dev.peerat.parser.visitor.VisitorBag;
public class Variable extends Annotable{
public static interface VariableContainer{
void addVariable(Variable variable);
}
private int mod;
private Token type; //TODO change to an independant objet ? to split package, name and generic
private Token name;
private boolean elips;
private Value value;
public Variable(List<Annotation> annotations, int mod, Token type, Token name){
super(annotations);
this.mod = mod;
this.type = type;
this.name = name;
}
public Variable(List<Annotation> annotations, int mod, Token type, Token name, boolean elips){
this(annotations, mod, type, name);
this.elips = elips;
}
public Variable(List<Annotation> 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 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 <E extends JavaElement> E find(Predicate<JavaElement> finder) {
E annotation = super.find(finder);
if(annotation != null) return annotation;
return value != null ? finder.test(value) ? (E)value : null : null;
}
@Override
public <E extends JavaElement> void findAll(Predicate<JavaElement> finder, Collection<E> collector) {
super.findAll(finder, collector);
if(value != null){
if(finder.test(value)) collector.add((E)value);
value.findAll(finder, collector);
}
}
@Override
public VisitorBag visit(Visitor<JavaElement> visitor){
if(visitor.canVisit(getClass())) return visitor.visit(this);
VisitorBag bag = new VisitorBag();
if(!visitor.canPropagate()) return bag;
List<Annotation> annotations = getAnnotations();
if(annotations != null)
for(Annotation annotation : annotations){
bag.merge(annotation.visit(visitor));
}
if(this.value != null) bag.merge(this.value.visit(visitor));
return bag;
}
@Override
public VisitorBag visit(TokenVisitor visitor){
VisitorBag bag = new VisitorBag();
return bag;
}
}