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 extends Builder extends T>> list) {
ArrayList result = new ArrayList();
for (Builder extends T> builder : list) {
result.add(builder.build());
}
return result;
}
public static LinkedHashSet build(Set extends Builder> list) {
LinkedHashSet result = new LinkedHashSet();
for (Builder 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 extends T> ...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) {
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