97 lines
No EOL
2.7 KiB
Java
97 lines
No EOL
2.7 KiB
Java
package dev.peerat.parser.java.visitor;
|
|
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
import java.util.function.BiPredicate;
|
|
import java.util.function.Function;
|
|
import java.util.function.Predicate;
|
|
|
|
import dev.peerat.parser.Token;
|
|
import dev.peerat.parser.java.Annotation;
|
|
import dev.peerat.parser.java.value.Value;
|
|
import dev.peerat.parser.visitor.VisitorBag;
|
|
|
|
public class JavaAnnotationVisitor extends JavaVisitor<Annotation>{
|
|
|
|
private Predicate<String> nameFilter;
|
|
private Predicate<String> keyFilter;
|
|
private BiPredicate<String, Value> valueFilter;
|
|
private Function<String, JavaVisitor<?>> valueFilterVisitor;
|
|
|
|
public JavaAnnotationVisitor(){
|
|
super(Annotation.class);
|
|
}
|
|
|
|
public JavaAnnotationVisitor nameFilter(Predicate<String> validator){
|
|
this.nameFilter = (this.nameFilter == null) ? validator : this.nameFilter.and(validator);
|
|
return this;
|
|
}
|
|
|
|
public JavaAnnotationVisitor noValues(){
|
|
//TODO
|
|
return this;
|
|
}
|
|
|
|
public JavaAnnotationVisitor hasValues(){
|
|
//TODO
|
|
return this;
|
|
}
|
|
|
|
public JavaAnnotationVisitor hasKey(Predicate<String> validator){
|
|
this.keyFilter = (this.keyFilter == null) ? validator : this.keyFilter.and(validator);
|
|
return this;
|
|
}
|
|
|
|
public JavaAnnotationVisitor valueFilter(BiPredicate<String, Value> validator){
|
|
this.valueFilter = (this.valueFilter == null) ? validator : this.valueFilter.and(validator);
|
|
return this;
|
|
}
|
|
|
|
//unique ?
|
|
public JavaAnnotationVisitor valueFilter(Function<String, JavaVisitor<?>> visitor){
|
|
System.out.println("filter value visitor");
|
|
this.valueFilterVisitor = visitor;
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public VisitorBag visitElement(Annotation element){
|
|
VisitorBag bag = new VisitorBag();
|
|
if(nameFilter != null){
|
|
if(!nameFilter.test(element.getName().getValue())) return bag;
|
|
}
|
|
|
|
if(keyFilter != null){
|
|
Map<Token, Value> paramters = element.getParameters();
|
|
if(paramters == null) return bag;
|
|
for(Token token : paramters.keySet()){
|
|
if(!keyFilter.test(token.getValue())) return bag;
|
|
}
|
|
}
|
|
|
|
if(valueFilter != null){
|
|
Map<Token, Value> paramters = element.getParameters();
|
|
if(paramters == null) return bag;
|
|
for(Entry<Token, Value> entry : paramters.entrySet()){
|
|
if(!valueFilter.test(entry.getKey().getValue(), entry.getValue())) return bag;
|
|
}
|
|
}
|
|
|
|
if(this.valueFilterVisitor != null){
|
|
Map<Token, Value> paramters = element.getParameters();
|
|
if(paramters == null) return bag;
|
|
//ALL LIKE THIS?
|
|
boolean findOne = false;
|
|
for(Entry<Token, Value> entry : paramters.entrySet()){
|
|
VisitorBag vbag = this.valueFilterVisitor.apply(entry.getKey().getValue()).visit(entry.getValue());
|
|
bag.merge(vbag);
|
|
if(vbag.isValidated()) findOne = true;
|
|
}
|
|
if(!findOne) return bag;
|
|
}
|
|
|
|
bag.compute(element);
|
|
return bag;
|
|
}
|
|
|
|
} |