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

io.kubernetes.client.fluent.BaseFluent Maven / Gradle / Ivy

package io.kubernetes.client.fluent;

import java.util.LinkedHashSet;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.Set;
import java.util.ArrayList;
import java.lang.String;
import java.util.AbstractMap;
import java.util.Objects;
import java.lang.Class;
import java.lang.Object;
import java.util.List;
import java.util.Arrays;
import java.util.Collections;
public class BaseFluent> implements Fluent,Visitable{
  public static final String VISIT = "visit";
  public final VisitableMap _visitables = new VisitableMap();
  public static VisitableBuilder builderOf(T item) {
    if (item instanceof Editable) {
      Object editor = ((Editable) item).edit();
      if (editor instanceof VisitableBuilder) {
        return (VisitableBuilder) editor;
      }
    }

    try {
      return (VisitableBuilder) Class.forName(item.getClass().getName() + "Builder", true, item.getClass().getClassLoader()).getConstructor(item.getClass())
          .newInstance(item);
    } catch (Exception e) {
      try {
        return (VisitableBuilder) Class.forName(item.getClass().getName() + "Builder").getConstructor(item.getClass())
            .newInstance(item);
      } catch (Exception e1) {
        throw new IllegalStateException("Failed to create builder for: " + item.getClass(), e1);
      }
    }
  }
  public static List build(List> list) {
    return list == null ? null : list.stream().map(Builder::build).collect(Collectors.toList());
  }
  public static Set build(Set> set) {
    return set == null ? null : new LinkedHashSet(set.stream().map(Builder::build).collect(Collectors.toSet()));
  }
  public static List aggregate(List... lists) {
    return new ArrayList(Arrays.stream(lists).filter(Objects::nonNull).collect(Collectors.toList()));
  }
  public static Set aggregate(Set... sets) {
    return new LinkedHashSet(Arrays.stream(sets).filter(Objects::nonNull).collect(Collectors.toSet()));
  }
  public F accept(io.kubernetes.client.fluent.Visitor... visitors) {
    return accept(Collections.emptyList(), visitors);
  }
  public F accept(Class type,Visitor visitor) {
    return accept(Collections.emptyList(), new Visitor() {
      @Override
      public Class getType() {
        return type;
      }

      @Override
      public void visit(List> path, V element) {
        visitor.visit(path, element);
      }

      @Override
      public void visit(V element) {
        visitor.visit(element);
      }
    });
  }
  public F accept(List> path,io.kubernetes.client.fluent.Visitor... visitors) {
    return accept(path, "", visitors);
  }
  public F accept(List> path,String currentKey,io.kubernetes.client.fluent.Visitor... visitors) {
    List sortedVisitor = new ArrayList<>();
    for (Visitor visitor : visitors) {
      visitor = VisitorListener.wrap(visitor);
      if (!visitor.canVisit(path, this)) {
        continue;
      }
      sortedVisitor.add(visitor);
    }
    sortedVisitor.sort((l, r) -> ((Visitor) r).order() - ((Visitor) l).order());
    for (Visitor visitor : sortedVisitor) {
      visitor.visit(path, this);
    }

    List> copyOfPath = path != null ? new ArrayList(path) : new ArrayList<>();
    copyOfPath.add(new AbstractMap.SimpleEntry<>(currentKey, this));

    for (Entry entry : _visitables.entrySet()) {
      List> newPath = Collections.unmodifiableList(copyOfPath);

      // Copy visitables to avoid ConcurrentModificationException when Visitors add/remove Visitables
      for (Visitable visitable : new ArrayList<>((List>) entry.getValue())) {
        for (Visitor visitor : visitors) {
          if (visitor.getType() != null && visitor.getType().isAssignableFrom(visitable.getClass())) {
            visitable.accept(newPath, entry.getKey(), visitor);
          }
        }

        for (Visitor visitor : visitors) {
          if (visitor.getType() == null || !visitor.getType().isAssignableFrom(visitable.getClass())) {
            visitable.accept(newPath, entry.getKey(), visitor);
          }
        }
      }
    }
    return (F) this;
  }
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + 0;
    return result;
  }
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    return true;
  }
  
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy