Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.kubernetes.client.extended.event.v1beta1;
package io.kubernetes.client.extended.event.v1;

import io.kubernetes.client.custom.V1Patch;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.CoreV1Event;
import io.kubernetes.client.openapi.models.V1beta1Event;
import io.kubernetes.client.openapi.models.EventsV1Event;

// placeholder interface for event v1beta1 api
// placeholder interface for event v1 api
public interface EventSink {

CoreV1Event create(V1beta1Event event) throws ApiException;
CoreV1Event create(EventsV1Event event) throws ApiException;

CoreV1Event update(V1beta1Event event) throws ApiException;
CoreV1Event update(EventsV1Event event) throws ApiException;

CoreV1Event patch(V1beta1Event event, V1Patch patch) throws ApiException;
CoreV1Event patch(EventsV1Event event, V1Patch patch) throws ApiException;
}
69 changes: 46 additions & 23 deletions fluent/src/main/java/io/kubernetes/client/fluent/BaseFluent.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
*/
package io.kubernetes.client.fluent;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

public class BaseFluent<F extends Fluent<F>>
implements io.kubernetes.client.fluent.Fluent<F>, Visitable<F> {
public class BaseFluent<F extends Fluent<F>> implements Fluent<F>, Visitable<F> {
public static final String VISIT = "visit";
public final VisitableMap _visitables = new VisitableMap();

Expand All @@ -45,34 +46,34 @@ public class BaseFluent<F extends Fluent<F>>
}

public static <T> List<T> build(
java.util.List<? extends io.kubernetes.client.fluent.Builder<? extends T>> list) {
List<? extends io.kubernetes.client.fluent.Builder<? extends T>> list) {
return list == null
? null
: new ArrayList<T>(list.stream().map(Builder::build).collect(Collectors.toList()));
}

public static <T> Set<T> build(
java.util.Set<? extends io.kubernetes.client.fluent.Builder<? extends T>> set) {
Set<? extends io.kubernetes.client.fluent.Builder<? extends T>> set) {
return set == null
? null
: new LinkedHashSet<T>(set.stream().map(Builder::build).collect(Collectors.toSet()));
}

public static <T> java.util.List<T> aggregate(java.util.List<? extends T>... lists) {
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> java.util.Set<T> aggregate(java.util.Set<? extends T>... sets) {
public static <T> Set<T> aggregate(Set<? extends T>... sets) {
return new LinkedHashSet(
Arrays.stream(sets).filter(Objects::nonNull).collect(Collectors.toSet()));
}

public F accept(Visitor... visitors) {
public F accept(io.kubernetes.client.fluent.Visitor... visitors) {
return accept(Collections.emptyList(), visitors);
}

public <V> F accept(Class<V> type, io.kubernetes.client.fluent.Visitor<V> visitor) {
public <V> F accept(Class<V> type, Visitor<V> visitor) {
return accept(
Collections.emptyList(),
new Visitor<V>() {
Expand All @@ -82,7 +83,7 @@ public Class<V> getType() {
}

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

Expand All @@ -93,7 +94,15 @@ public void visit(V element) {
});
}

public F accept(java.util.List<Object> path, io.kubernetes.client.fluent.Visitor... visitors) {
public F accept(
List<Entry<String, Object>> path, io.kubernetes.client.fluent.Visitor... visitors) {
return accept(path, "", visitors);
}

public F accept(
List<Entry<String, Object>> path,
String currentKey,
io.kubernetes.client.fluent.Visitor... visitors) {
Arrays.stream(visitors)
.map(v -> VisitorListener.wrap(v))
.filter(v -> ((Visitor) v).canVisit(path, this))
Expand All @@ -103,18 +112,32 @@ public F accept(java.util.List<Object> path, io.kubernetes.client.fluent.Visitor
((Visitor) v).visit(path, this);
});

List<Object> copyOfPath = path != null ? new ArrayList(path) : new ArrayList<>();
copyOfPath.add(this);
List<Object> newPath = Collections.unmodifiableList(copyOfPath);

for (Visitable visitable : _visitables) {
Arrays.stream(visitors)
.filter(v -> v.getType() != null && v.getType().isAssignableFrom(visitable.getClass()))
.forEach(v -> visitable.accept(newPath, v));
Arrays.stream(visitors)
.filter(v -> v.getType() == null || !v.getType().isAssignableFrom(visitable.getClass()))
.forEach(v -> visitable.accept(newPath, v));
}
List<Entry<String, Object>> copyOfPath = path != null ? new ArrayList(path) : new ArrayList<>();
copyOfPath.add(new AbstractMap.SimpleEntry<String, Object>(currentKey, this));

_visitables.forEach(
(key, visitables) -> {
List<Entry<String, Object>> newPath = Collections.unmodifiableList(copyOfPath);
// Copy visitables to avoid ConcurrrentModificationException when Visitors add/remove
// Visitables
new ArrayList<>(visitables)
.forEach(
visitable -> {
Arrays.stream(visitors)
.filter(
v ->
v.getType() != null
&& v.getType().isAssignableFrom(visitable.getClass()))
.forEach(v -> visitable.accept(newPath, key, v));

Arrays.stream(visitors)
.filter(
v ->
v.getType() == null
|| !v.getType().isAssignableFrom(visitable.getClass()))
.forEach(v -> visitable.accept(newPath, key, v));
});
});
return (F) this;
}

Expand All @@ -125,7 +148,7 @@ public int hashCode() {
return result;
}

public boolean equals(java.lang.Object obj) {
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@
package io.kubernetes.client.fluent;

import java.util.List;
import java.util.Map.Entry;
import java.util.function.Predicate;

public class DelegatingVisitor<T> implements Visitor<T> {
DelegatingVisitor(Class<T> type, io.kubernetes.client.fluent.Visitor<T> delegate) {
DelegatingVisitor(Class<T> type, Visitor<T> delegate) {
this.type = type;
this.delegate = delegate;
}

private final java.lang.Class<T> type;
private final io.kubernetes.client.fluent.Visitor<T> delegate;
private final Class<T> type;
private final Visitor<T> delegate;

public java.lang.Class<T> getType() {
public Class<T> getType() {
return type;
}

Expand All @@ -36,11 +37,11 @@ public int order() {
return delegate.order();
}

public void visit(List<Object> path, T target) {
public void visit(List<Entry<String, Object>> path, T target) {
delegate.visit(path, target);
}

public <F> Predicate<java.util.List<java.lang.Object>> getRequirement() {
public <F> Predicate<List<Entry<String, Object>>> getRequirement() {
return delegate.getRequirement();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package io.kubernetes.client.fluent;

import java.util.List;
import java.util.Map.Entry;

public class PathAwareTypedVisitor<V, P> extends TypedVisitor<V> {
PathAwareTypedVisitor() {
Expand All @@ -26,19 +27,19 @@ public class PathAwareTypedVisitor<V, P> extends TypedVisitor<V> {
}

private final Class<V> type;
private final java.lang.Class<P> parentType;
private final Class<P> parentType;

public void visit(V element) {}

public void visit(List<Object> path, V element) {
public void visit(List<Entry<String, Object>> path, V element) {
visit(element);
}

public P getParent(java.util.List<java.lang.Object> path) {
public P getParent(List<Entry<String, Object>> path) {
return path.size() - 1 >= 0 ? (P) path.get(path.size() - 1) : null;
}

public java.lang.Class<P> getParentType() {
public Class<P> getParentType() {
return parentType;
}
}
11 changes: 10 additions & 1 deletion fluent/src/main/java/io/kubernetes/client/fluent/Visitable.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;

public interface Visitable<T> {
default <V> T accept(Class<V> type, Visitor<V> visitor) {
Expand All @@ -40,7 +41,15 @@ default T accept(io.kubernetes.client.fluent.Visitor... visitors) {
return getTarget(this);
}

default T accept(List<Object> path, io.kubernetes.client.fluent.Visitor... visitors) {
default T accept(
List<Entry<String, Object>> path, io.kubernetes.client.fluent.Visitor... visitors) {
return accept(path, "", visitors);
}

default T accept(
List<Entry<String, Object>> path,
String currentKey,
io.kubernetes.client.fluent.Visitor... visitors) {
for (Visitor visitor : visitors) {
if (visitor.canVisit(path, this)) {
visitor.visit(path, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@
import java.util.stream.Collectors;

public class VisitableMap extends HashMap<String, List<Visitable<?>>>
implements Iterable<io.kubernetes.client.fluent.Visitable<?>> {
public java.util.List<io.kubernetes.client.fluent.Visitable<?>> get(Object key) {
implements Iterable<Visitable<?>> {
public List<Visitable<?>> get(Object key) {
if (!containsKey(key)) {
put(String.valueOf(key), new ArrayList());
}
return super.get(key);
}

public java.util.List<io.kubernetes.client.fluent.Visitable<?>> aggregate() {
public List<Visitable<?>> aggregate() {
return values().stream().flatMap(l -> l.stream()).collect(Collectors.toList());
}

public Iterator<io.kubernetes.client.fluent.Visitable<?>> iterator() {
public Iterator<Visitable<?>> iterator() {
return aggregate().iterator();
}

public void forEach(Consumer<? super io.kubernetes.client.fluent.Visitable<?>> action) {
public void forEach(Consumer<? super Visitable<?>> action) {
aggregate().forEach(action);
}

Expand Down
28 changes: 16 additions & 12 deletions fluent/src/main/java/io/kubernetes/client/fluent/Visitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map.Entry;
import java.util.function.Predicate;

@FunctionalInterface
Expand All @@ -32,11 +33,11 @@ default int order() {
return 0;
}

default void visit(List<Object> path, T element) {
default void visit(List<Entry<String, Object>> path, T element) {
visit(element);
}

default <F> Boolean canVisit(java.util.List<java.lang.Object> path, F target) {
default <F> Boolean canVisit(List<Entry<String, Object>> path, F target) {
if (target == null) {
return false;
}
Expand All @@ -56,7 +57,7 @@ default <F> Boolean canVisit(java.util.List<java.lang.Object> path, F target) {
}
}

default <F> java.lang.Boolean hasVisitMethodMatching(F target) {
default <F> Boolean hasVisitMethodMatching(F target) {
for (Method method : getClass().getMethods()) {
if (!method.getName().equals("visit") || method.getParameterTypes().length != 1) {
continue;
Expand All @@ -71,24 +72,27 @@ default <F> java.lang.Boolean hasVisitMethodMatching(F target) {
return false;
}

default <T> Predicate<java.util.List<java.lang.Object>> getRequirement() {
default <T> Predicate<List<Entry<String, Object>>> getRequirement() {
return p -> true;
}

default <I> java.util.function.Predicate<java.util.List<java.lang.Object>> hasItem(
java.lang.Class<I> type, java.util.function.Predicate<I> predicate) {
Predicate<List<Object>> result =
l -> l.stream().filter(i -> type.isInstance(i)).map(i -> type.cast(i)).anyMatch(predicate);
default <I> Predicate<List<Entry<String, Object>>> hasItem(
Class<I> type, Predicate<I> predicate) {
Predicate<List<Entry<String, Object>>> result =
l ->
l.stream()
.map(Entry::getValue)
.filter(i -> type.isInstance(i))
.map(i -> type.cast(i))
.anyMatch(predicate);
return result;
}

default <P> Visitor<T> addRequirement(
java.lang.Class<P> type, java.util.function.Predicate<P> predicate) {
default <P> Visitor<T> addRequirement(Class<P> type, Predicate<P> predicate) {
return addRequirement(predicate);
}

default io.kubernetes.client.fluent.Visitor<T> addRequirement(
java.util.function.Predicate predicate) {
default Visitor<T> addRequirement(Predicate predicate) {
return new DelegatingVisitor(getType(), this) {
@Override
public Predicate<List<Object>> getRequirement() {
Expand Down
Loading