io.alauda.kubernetes.api.builder.BaseFluent Maven / Gradle / Ivy
package io.alauda.kubernetes.api.builder;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
import java.lang.String;
import java.lang.reflect.Method;
import java.lang.Boolean;
public class BaseFluent> implements Fluent,Visitable{
private static final String VISIT = "visit";
public final List _visitables = new ArrayList();
public static ArrayList build(List extends Builder extends T>> list){
ArrayList result = new ArrayList();
for (Builder extends T> builder : list) {
result.add(builder.build());
}
return result;
}
public static List build(Set extends Builder extends T>> list){
List result = new ArrayList();
for (Builder extends T> builder : list) {
result.add(builder.build());
}
return result;
}
public static ArrayList aggregate(List extends T>... lists){
ArrayList result = new ArrayList();
for (List extends T> list : lists) {
result.addAll(list);
}
return result;
}
public static LinkedHashSet aggregate(Set... sets){
LinkedHashSet result = new LinkedHashSet();
for (Set extends T> set : sets) {
result.addAll(set);
}
return result;
}
private static Boolean canVisit(V visitor,F fluent){
if (visitor instanceof TypedVisitor) {
if (!((TypedVisitor) visitor).getType().isAssignableFrom(fluent.getClass())) {
return false;
}
}
if (visitor instanceof PathAwareTypedVisitor) {
PathAwareTypedVisitor pathAwareTypedVisitor = (PathAwareTypedVisitor) visitor;
Class parentType = pathAwareTypedVisitor.getParentType();
Class actaulParentType = pathAwareTypedVisitor.getActualParentType();
if (!parentType.isAssignableFrom(actaulParentType)) {
return false;
}
}
return hasCompatibleVisitMethod(visitor, fluent);
}
private static Boolean hasCompatibleVisitMethod(V visitor,F fluent){
for (Method method : visitor.getClass().getMethods()) {
if (!method.getName().equals(VISIT) || method.getParameterTypes().length != 1) {
continue;
}
Class visitorType = method.getParameterTypes()[0];
if (visitorType.isAssignableFrom(fluent.getClass())) {
return true;
} else {
return false;
}
}
return false;
}
public F accept(io.alauda.kubernetes.api.builder.Visitor visitor){
if (visitor instanceof PathAwareTypedVisitor) {
return acceptPathAware((PathAwareTypedVisitor) visitor);
} else {
return acceptInternal(visitor);
}
}
private F acceptInternal(io.alauda.kubernetes.api.builder.Visitor visitor){
for (Visitable visitable : _visitables) {
visitable.accept(visitor);
}
if (canVisit(visitor, this)) {
visitor.visit(this);
}
return (F) this;
}
private F acceptPathAware(PathAwareTypedVisitor pathAwareTypedVisitor){
return acceptInternal(pathAwareTypedVisitor.next(this));
}
}