-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
task: moves the crd annotations into generator annotations
closes #5429
- Loading branch information
Showing
22 changed files
with
678 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
79 changes: 79 additions & 0 deletions
79
kubernetes-client-api/src/test/java/io/fabric8/kubernetes/builder/BaseFluent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.fabric8.kubernetes.builder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class BaseFluent<F> { | ||
|
||
public static final String VISIT = "visit"; | ||
public final VisitableMap _visitables = new VisitableMap(); | ||
|
||
public static <T> VisitableBuilder<T, ?> builderOf(T item) { | ||
if (item instanceof Editable) { | ||
Object editor = ((Editable) item).edit(); | ||
if (editor instanceof VisitableBuilder) { | ||
return (VisitableBuilder<T, ?>) editor; | ||
} | ||
} | ||
|
||
try { | ||
return (VisitableBuilder<T, ?>) Class | ||
.forName(item.getClass().getName() + "Builder", true, item.getClass().getClassLoader()) | ||
.getConstructor(item.getClass()) | ||
.newInstance(item); | ||
} catch (Exception e) { | ||
try { | ||
return (VisitableBuilder<T, ?>) 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 <T> List<T> build(List<? extends Builder<? extends T>> list) { | ||
return list == null ? null : list.stream().map(Builder::build).collect(Collectors.toList()); | ||
} | ||
|
||
public static <T> Set<T> build(Set<? extends Builder<? extends T>> set) { | ||
return set == null ? null : new LinkedHashSet<T>(set.stream().map(Builder::build).collect(Collectors.toSet())); | ||
} | ||
|
||
public static <T> List<T> aggregate(List<? extends T>... lists) { | ||
return new ArrayList(Arrays.stream(lists).filter(Objects::nonNull).collect(Collectors.toList())); | ||
} | ||
|
||
public static <T> Set<T> aggregate(Set<? extends T>... sets) { | ||
return new LinkedHashSet(Arrays.stream(sets).filter(Objects::nonNull).collect(Collectors.toSet())); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + 0; | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
return true; | ||
} | ||
|
||
public Optional<VisitableMap> getVisitableMap() { | ||
return Optional.of(_visitables); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
kubernetes-client-api/src/test/java/io/fabric8/kubernetes/builder/Builder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.fabric8.kubernetes.builder; | ||
|
||
import java.lang.FunctionalInterface; | ||
@FunctionalInterface | ||
public interface Builder<T>{ | ||
|
||
|
||
T build(); | ||
|
||
|
||
} |
38 changes: 38 additions & 0 deletions
38
kubernetes-client-api/src/test/java/io/fabric8/kubernetes/builder/DelegatingVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.fabric8.kubernetes.builder; | ||
|
||
import java.util.Map.Entry; | ||
import java.lang.Class; | ||
import java.lang.Object; | ||
import java.util.List; | ||
import java.lang.String; | ||
import java.util.function.Predicate; | ||
public class DelegatingVisitor<T> implements Visitor<T>{ | ||
DelegatingVisitor(Class<T> type,Visitor<T> delegate) { | ||
this.type = type; | ||
this.delegate = delegate; | ||
} | ||
private final Class<T> type; | ||
private final Visitor<T> delegate; | ||
|
||
public Class<T> getType() { | ||
return type; | ||
} | ||
|
||
public void visit(T target) { | ||
delegate.visit(target); | ||
} | ||
|
||
public int order() { | ||
return delegate.order(); | ||
} | ||
|
||
public void visit(List<Entry<String,Object>> path,T target) { | ||
delegate.visit(path, target); | ||
} | ||
|
||
public <F>Predicate<List<Entry<String,Object>>> getRequirement() { | ||
return delegate.getRequirement(); | ||
} | ||
|
||
|
||
} |
9 changes: 9 additions & 0 deletions
9
kubernetes-client-api/src/test/java/io/fabric8/kubernetes/builder/Editable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.fabric8.kubernetes.builder; | ||
|
||
public interface Editable<T>{ | ||
|
||
|
||
T edit(); | ||
|
||
|
||
} |
9 changes: 9 additions & 0 deletions
9
kubernetes-client-api/src/test/java/io/fabric8/kubernetes/builder/Nested.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.fabric8.kubernetes.builder; | ||
|
||
public interface Nested<F>{ | ||
|
||
|
||
F and(); | ||
|
||
|
||
} |
38 changes: 38 additions & 0 deletions
38
kubernetes-client-api/src/test/java/io/fabric8/kubernetes/builder/PathAwareTypedVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.fabric8.kubernetes.builder; | ||
|
||
import java.util.Map.Entry; | ||
import java.lang.Class; | ||
import java.lang.Object; | ||
import java.util.List; | ||
import java.lang.String; | ||
import java.lang.reflect.Method; | ||
public class PathAwareTypedVisitor<V,P> extends TypedVisitor<V>{ | ||
PathAwareTypedVisitor() { | ||
List<Class> args = Visitors.getTypeArguments(PathAwareTypedVisitor.class, getClass()); | ||
if (args == null || args.isEmpty()) { | ||
throw new IllegalStateException("Could not determine type arguments for path aware typed visitor."); | ||
} | ||
this.type = (Class<V>) args.get(0); | ||
this.parentType = (Class<P>) args.get(1); | ||
} | ||
private final Class<V> type; | ||
private final Class<P> parentType; | ||
|
||
public void visit(V element) { | ||
|
||
} | ||
|
||
public void visit(List<Entry<String,Object>> path,V element) { | ||
visit(element); | ||
} | ||
|
||
public P getParent(List<Entry<String,Object>> path) { | ||
return path.size() - 1 >= 0 ? (P) path.get(path.size() - 1) : null; | ||
} | ||
|
||
public Class<P> getParentType() { | ||
return parentType; | ||
} | ||
|
||
|
||
} |
13 changes: 13 additions & 0 deletions
13
kubernetes-client-api/src/test/java/io/fabric8/kubernetes/builder/TypedVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.fabric8.kubernetes.builder; | ||
|
||
import java.lang.Class; | ||
public abstract class TypedVisitor<V> implements Visitor<V>{ | ||
|
||
|
||
|
||
public Class<V> getType() { | ||
return (Class<V>) Visitors.getTypeArguments(TypedVisitor.class, getClass()).get(0); | ||
} | ||
|
||
|
||
} |
88 changes: 88 additions & 0 deletions
88
kubernetes-client-api/src/test/java/io/fabric8/kubernetes/builder/Visitable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.fabric8.kubernetes.builder; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map.Entry; | ||
import java.util.Optional; | ||
|
||
public interface Visitable<T> { | ||
|
||
default T accept(Visitor<?>... visitors) { | ||
return accept(Collections.emptyList(), visitors); | ||
} | ||
|
||
default <V> T accept(Class<V> type, Visitor<V> visitor) { | ||
return accept(Collections.emptyList(), new Visitor<V>() { | ||
@Override | ||
public Class<V> getType() { | ||
return type; | ||
} | ||
|
||
@Override | ||
public void visit(List<Entry<String, Object>> path, V element) { | ||
visitor.visit(path, element); | ||
} | ||
|
||
@Override | ||
public void visit(V element) { | ||
visitor.visit(element); | ||
} | ||
}); | ||
} | ||
|
||
default T accept(List<Entry<String, Object>> path, Visitor<?>... visitors) { | ||
return accept(path, "", visitors); | ||
} | ||
|
||
default T accept(List<Entry<String, Object>> path, String currentKey, Visitor<?>... visitors) { | ||
List<Visitor> 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<Entry<String, Object>> copyOfPath = path != null ? new ArrayList(path) : new ArrayList<>(); | ||
copyOfPath.add(new AbstractMap.SimpleEntry<>(currentKey, this)); | ||
|
||
getVisitableMap().ifPresent(vm -> { | ||
for (Entry<String, ?> entry : vm.entrySet()) { | ||
List<Entry<String, Object>> newPath = Collections.unmodifiableList(copyOfPath); | ||
|
||
// Copy visitables to avoid ConcurrentModificationException when Visitors add/remove Visitables | ||
for (Visitable<T> visitable : new ArrayList<>((List<Visitable<T>>) 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 (T) this; | ||
} | ||
|
||
default T getTarget(Visitable<T> visitable) { | ||
return (T) visitable; | ||
} | ||
|
||
default Optional<VisitableMap> getVisitableMap() { | ||
return Optional.empty(); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
kubernetes-client-api/src/test/java/io/fabric8/kubernetes/builder/VisitableBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.fabric8.kubernetes.builder; | ||
|
||
public interface VisitableBuilder<T, V> extends Builder<T>, Visitable<V> { | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
kubernetes-client-api/src/test/java/io/fabric8/kubernetes/builder/VisitableMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.fabric8.kubernetes.builder; | ||
|
||
import java.util.stream.Collectors; | ||
import java.lang.Iterable; | ||
import java.util.function.Consumer; | ||
import java.util.HashMap; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.lang.Object; | ||
import java.util.List; | ||
import java.lang.String; | ||
import java.util.Spliterator; | ||
public class VisitableMap extends HashMap<String,List<Visitable<?>>> implements Iterable<Visitable<?>>{ | ||
|
||
|
||
|
||
public List<Visitable<?>> get(Object key) { | ||
if (!containsKey(key)) { | ||
put(String.valueOf(key), new ArrayList()); | ||
} | ||
return super.get(key); | ||
} | ||
|
||
public List<Visitable<?>> aggregate() { | ||
return values().stream().flatMap(l -> l.stream()).collect(Collectors.toList()); | ||
} | ||
|
||
public Iterator<Visitable<?>> iterator() { | ||
return aggregate().iterator(); | ||
} | ||
|
||
public void forEach(Consumer<? super Visitable<?>> action) { | ||
aggregate().forEach(action); | ||
} | ||
|
||
public Spliterator spliterator() { | ||
return aggregate().spliterator(); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.