All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.alauda.kubernetes.api.builder.BaseFluent Maven / Gradle / Ivy

There is a newer version: 0.2.12
Show newest version
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> list){
            ArrayList result = new ArrayList();
            for (Builder builder : list) {
    result.add(builder.build());
}
            return result;
    }

    public static List build(Set> list){
            List result = new ArrayList();
            for (Builder builder : list) {
    result.add(builder.build());
}
            return result;
    }

    public static ArrayList aggregate(List... lists){
            ArrayList result = new ArrayList();
            for (List list : lists) {
    result.addAll(list);
}
            return result;
    }

    public static LinkedHashSet aggregate(Set... sets){
            LinkedHashSet result = new LinkedHashSet();
            for (Set 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));
    }




}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy