Skip to content

Commit

Permalink
a more concise implementation of merge not override
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP authored and jsenko committed Oct 29, 2024
1 parent 495b4d6 commit 76988ab
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static io.apicurio.registry.operator.resource.app.AppDeploymentResource.getContainerFromPodTemplateSpec;
import static io.apicurio.registry.operator.utils.Mapper.YAML_MAPPER;
import static io.apicurio.registry.operator.utils.Utils.isBlank;
import static io.apicurio.registry.operator.utils.Utils.mergeNotOverride;

public class ResourceFactory {

Expand Down Expand Up @@ -141,7 +140,12 @@ private static void mergeDeploymentPodTemplateSpec(
if (c.getPorts() == null) {
c.setPorts(new ArrayList<>());
}
mergeNotOverride(c.getPorts(), ports, ContainerPort::getName);
var targetPorts = c.getPorts();
ports.forEach(sourcePort -> {
if (!targetPorts.stream().anyMatch(targetPort -> sourcePort.getName().equals(targetPort.getName()))) {
targetPorts.add(sourcePort);
}
});
if (c.getReadinessProbe() == null) {
c.setReadinessProbe(readinessProbe);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package io.apicurio.registry.operator.utils;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static java.util.Objects.requireNonNull;

public class Utils {

private Utils() {
Expand All @@ -15,41 +9,4 @@ public static boolean isBlank(String value) {
return value == null || value.isBlank();
}

/**
* Merge source map into target map, NOT overriding any entries with the same key.
*
* @param target must not be null
*/
public static <V> void mergeNotOverride(Map<String, V> target, Map<String, V> source) {
requireNonNull(target);
if (source != null) {
source.forEach(target::putIfAbsent);
}
}

/**
* Merge source list into target list, NOT overriding any items based on the equality of the extracted
* key.
*
* @param target must not be null
*/
public static <T, K> void mergeNotOverride(List<T> target, List<T> source, Function<T, K> extractKey) {
requireNonNull(target);
if (source != null) {
for (T sval : source) {
K skey = extractKey.apply(sval);
boolean skip = false;
for (int ti = 0; ti < target.size(); ti++) {
K tkey = extractKey.apply(target.get(ti));
if (skey.equals(tkey)) {
skip = true;
break;
}
}
if (!skip) {
target.add(sval);
}
}
}
}
}

0 comments on commit 76988ab

Please sign in to comment.