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

io.sundr.builder.BaseFluent Maven / Gradle / Ivy

/*
 * Copyright 2015 The original authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package io.sundr.builder;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class BaseFluent> implements Fluent, Visitable {

    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  LinkedHashSet build(Set> list) {
        LinkedHashSet result = new LinkedHashSet();
        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) {
            return ((TypedVisitor) visitor).getType().isAssignableFrom(fluent.getClass());
        }

        for (Method method : visitor.getClass().getDeclaredMethods()) {
            if (method.getParameterTypes().length != 1) {
                continue;
            }
            Class visitorType = method.getParameterTypes()[0];
            if (visitorType.isAssignableFrom(fluent.getClass())) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    @Override
    public F accept(Visitor visitor) {
        for (Visitable visitable : _visitables) {
            visitable.accept(visitor);
        }

        if (canVisit(visitor, this)) {
            visitor.visit(this);
        }
        return (F) this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy