Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve covariance in generator APIs #9303

Merged
merged 12 commits into from
Mar 20, 2024
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package tech.jhipster.lite.module.domain;

import static tech.jhipster.lite.module.domain.javabuild.JavaBuildTool.GRADLE;
import static tech.jhipster.lite.module.domain.javabuild.JavaBuildTool.MAVEN;
import static tech.jhipster.lite.module.domain.properties.SpringConfigurationFormat.PROPERTIES;
import static tech.jhipster.lite.module.domain.javabuild.JavaBuildTool.*;
import static tech.jhipster.lite.module.domain.properties.SpringConfigurationFormat.*;

import java.time.Instant;
import java.util.Collection;
Expand Down Expand Up @@ -132,7 +131,7 @@ private static JHipsterStartupCommands buildStartupCommands(
if (detectedJavaBuildTool.isEmpty()) {
return jHipsterStartupCommands;
}
List<JHipsterStartupCommand> filteredCommands = jHipsterStartupCommands
var filteredCommands = jHipsterStartupCommands
.get()
.stream()
.filter(isStartupCommandCompatibleWith(detectedJavaBuildTool.get()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
package tech.jhipster.lite.module.domain.javabuild.command;

import java.util.ArrayList;
import static tech.jhipster.lite.shared.collection.domain.JHipsterCollections.*;

import java.util.Collection;
import java.util.List;
import tech.jhipster.lite.shared.collection.domain.JHipsterCollections;
import tech.jhipster.lite.shared.error.domain.Assert;

public record JavaBuildCommands(Collection<JavaBuildCommand> commands) {
public record JavaBuildCommands(Collection<? extends JavaBuildCommand> commands) {
public static final JavaBuildCommands EMPTY = new JavaBuildCommands(List.of());

public JavaBuildCommands(Collection<JavaBuildCommand> commands) {
public JavaBuildCommands(Collection<? extends JavaBuildCommand> commands) {
this.commands = JHipsterCollections.immutable(commands);
}

public JavaBuildCommands merge(JavaBuildCommands other) {
Assert.notNull("other", other);

List<JavaBuildCommand> mergedCommands = new ArrayList<>();
mergedCommands.addAll(commands());
mergedCommands.addAll(other.commands());

return new JavaBuildCommands(mergedCommands);
return new JavaBuildCommands(concat(commands(), other.commands()));
}

public boolean isEmpty() {
return get().isEmpty();
}

@SuppressWarnings("unchecked")
public Collection<JavaBuildCommand> get() {
return commands();
return (Collection<JavaBuildCommand>) commands();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ private Function<JHipsterModuleJavaBuildProfile, Stream<JavaBuildCommand>> toMav
JavaDependenciesVersions versions,
ProjectJavaDependencies projectJavaDependencies
) {
return profile -> profile.mavenPlugins().buildChanges(versions, projectJavaDependencies, profile.id()).commands().stream();
return profile -> profile.mavenPlugins().buildChanges(versions, projectJavaDependencies, profile.id()).get().stream();
}

private Function<JHipsterModuleJavaBuildProfile, Stream<JavaBuildCommand>> toJavaDependenciesCommands(
JavaDependenciesVersions versions,
ProjectJavaDependencies projectJavaDependencies
) {
return profile -> profile.javaDependencies().buildChanges(versions, projectJavaDependencies, profile.id()).commands().stream();
return profile -> profile.javaDependencies().buildChanges(versions, projectJavaDependencies, profile.id()).get().stream();
}

public static final class JHipsterModuleJavaBuildProfilesBuilder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package tech.jhipster.lite.module.domain.landscape;

import static java.util.function.Predicate.*;

import java.util.Collection;
import java.util.Optional;
import java.util.stream.Stream;
import tech.jhipster.lite.shared.error.domain.Assert;

public record JHipsterLandscapeDependencies(Collection<JHipsterLandscapeDependency> dependencies) {
public record JHipsterLandscapeDependencies(Collection<? extends JHipsterLandscapeDependency> dependencies) {
public JHipsterLandscapeDependencies {
Assert.notEmpty("dependencies", dependencies);
}

public static Optional<JHipsterLandscapeDependencies> of(Collection<JHipsterLandscapeDependency> dependencies) {
return Optional.ofNullable(dependencies).filter(dep -> !dep.isEmpty()).map(JHipsterLandscapeDependencies::new);
}

public Collection<JHipsterLandscapeDependency> get() {
return dependencies();
public static Optional<JHipsterLandscapeDependencies> of(Collection<? extends JHipsterLandscapeDependency> dependencies) {
return Optional.ofNullable(dependencies).filter(not(Collection::isEmpty)).map(JHipsterLandscapeDependencies::new);
}

public long count() {
return dependencies().size();
}

public Stream<JHipsterLandscapeDependency> stream() {
return dependencies().stream();
return dependencies().stream().map(JHipsterLandscapeDependency.class::cast);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Collection;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
import tech.jhipster.lite.module.domain.JHipsterFeatureSlug;
import tech.jhipster.lite.module.domain.JHipsterSlug;
Expand Down Expand Up @@ -30,15 +29,11 @@ private Optional<JHipsterLandscapeDependencies> buildDependencies() {
.map(JHipsterLandscapeModule::dependencies)
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(toDependencies())
.flatMap(JHipsterLandscapeDependencies::stream)
.toList()
);
}

private Function<JHipsterLandscapeDependencies, Stream<JHipsterLandscapeDependency>> toDependencies() {
return dep -> dep.dependencies().stream();
}

@Override
public JHipsterFeatureSlug slug() {
return slug;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import tech.jhipster.lite.module.domain.JHipsterSlug;
import tech.jhipster.lite.shared.error.domain.Assert;

public record JHipsterLandscapeLevel(Collection<JHipsterLandscapeElement> elements) {
public JHipsterLandscapeLevel(Collection<JHipsterLandscapeElement> elements) {
public record JHipsterLandscapeLevel(Collection<? extends JHipsterLandscapeElement> elements) {
public JHipsterLandscapeLevel(Collection<? extends JHipsterLandscapeElement> elements) {
Assert.notEmpty("elements", elements);

this.elements = Collections.unmodifiableCollection(elements);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,7 @@ private List<JHipsterLandscapeElement> levelElements(Predicate<JHipsterLandscape
private Predicate<JHipsterLandscapeElement> withAllKnownDependencies(Set<JHipsterSlug> knownSlugs) {
return element ->
knownSlugs.containsAll(
element
.dependencies()
.stream()
.flatMap(dependencies -> dependencies.get().stream())
.map(JHipsterLandscapeDependency::slug)
.toList()
element.dependencies().stream().flatMap(JHipsterLandscapeDependencies::stream).map(JHipsterLandscapeDependency::slug).toList()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private static final class JHipsterLandscapeModuleBuilder

private JHipsterModuleSlug module;
private JHipsterModuleOperation operation;
private Collection<JHipsterLandscapeDependency> dependencies;
private Collection<? extends JHipsterLandscapeDependency> dependencies;
private JHipsterModulePropertiesDefinition propertiesDefinition;

@Override
Expand All @@ -118,7 +118,7 @@ public JHipsterLandscapeModuleDependenciesBuilder propertiesDefinition(JHipsterM
}

@Override
public JHipsterLandscapeModule dependencies(Collection<JHipsterLandscapeDependency> dependencies) {
public JHipsterLandscapeModule dependencies(Collection<? extends JHipsterLandscapeDependency> dependencies) {
this.dependencies = dependencies;

return new JHipsterLandscapeModule(this);
Expand Down Expand Up @@ -146,7 +146,7 @@ public interface JHipsterLandscapeModulePropertiesDefinitionBuilder {
}

public interface JHipsterLandscapeModuleDependenciesBuilder {
JHipsterLandscapeModule dependencies(Collection<JHipsterLandscapeDependency> dependencies);
JHipsterLandscapeModule dependencies(Collection<? extends JHipsterLandscapeDependency> dependencies);

default JHipsterLandscapeModule withoutDependencies() {
return dependencies(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.function.Consumer;
import tech.jhipster.lite.shared.error.domain.Assert;

public record ContentReplacers(Collection<ContentReplacer> replacers) {
public record ContentReplacers(Collection<? extends ContentReplacer> replacers) {
public ContentReplacers {
Assert.field("replacers", replacers).notNull().noNullElement();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ private JHipsterModuleOptionalReplacementsFactory(JHipsterModuleOptionalReplacem
upgrades = Optional.empty();
}

private JHipsterModuleOptionalReplacementsFactory(Collection<ContentReplacer> replacers, JHipsterUpgradeFilesReplacements upgrade) {
private JHipsterModuleOptionalReplacementsFactory(
Collection<? extends ContentReplacer> replacers,
JHipsterUpgradeFilesReplacements upgrade
) {
super(replacers);
this.upgrades = Optional.of(upgrade);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected JHipsterModuleReplacementsFactory(JHipsterModuleReplacementsFactoryBui
this.replacers = JHipsterCollections.immutable(builder.replacers);
}

protected JHipsterModuleReplacementsFactory(Collection<ContentReplacer> replacers) {
protected JHipsterModuleReplacementsFactory(Collection<? extends ContentReplacer> replacers) {
Assert.notNull("replacers", replacers);

this.replacers = JHipsterCollections.immutable(replacers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import java.util.Collection;
import tech.jhipster.lite.shared.collection.domain.JHipsterCollections;

public record JHipsterStartupCommands(Collection<JHipsterStartupCommand> commands) {
public JHipsterStartupCommands(Collection<JHipsterStartupCommand> commands) {
public record JHipsterStartupCommands(Collection<? extends JHipsterStartupCommand> commands) {
public JHipsterStartupCommands(Collection<? extends JHipsterStartupCommand> commands) {
this.commands = JHipsterCollections.immutable(commands);
}

@SuppressWarnings("unchecked")
public Collection<JHipsterStartupCommand> get() {
return commands;
return (Collection<JHipsterStartupCommand>) commands;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tech.jhipster.lite.shared.collection.domain;

import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.*;

import java.util.Collection;
import java.util.Collections;
Expand All @@ -14,15 +14,15 @@ public final class JHipsterCollections {

private JHipsterCollections() {}

public static <T> Collection<T> immutable(Collection<T> collection) {
public static <T> Collection<T> immutable(Collection<? extends T> collection) {
if (collection == null) {
return List.of();
}

return Collections.unmodifiableCollection(collection);
}

public static <K, V> Map<K, V> immutable(Map<K, V> map) {
public static <K, V> Map<K, V> immutable(Map<? extends K, ? extends V> map) {
if (map == null) {
return Map.of();
}
Expand All @@ -31,16 +31,18 @@ public static <K, V> Map<K, V> immutable(Map<K, V> map) {
}

@SafeVarargs
public static <T> Collection<T> concat(Collection<T>... collections) {
return Stream.of(collections).filter(Objects::nonNull).flatMap(Collection::stream).toList();
public static <T> Collection<T> concat(Collection<? extends T>... collections) {
return Stream.of(collections).filter(Objects::nonNull).flatMap(Collection::stream).map(item -> (T) item).toList();
}

@SafeVarargs
public static <K, V> Map<K, V> concat(Map<K, V>... maps) {
return Stream.of(maps)
.filter(Objects::nonNull)
.map(Map::entrySet)
.flatMap(Collection::stream)
.collect(toMap(Entry::getKey, Entry::getValue, (value1, value2) -> value2));
public static <K, V> Map<K, V> concat(Map<? extends K, ? extends V>... maps) {
return Collections.unmodifiableMap(
Stream.of(maps)
.filter(Objects::nonNull)
.map(Map::entrySet)
.flatMap(Collection::stream)
.collect(toMap(Entry::getKey, Entry::getValue, (value1, value2) -> value2))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public final class {{ baseName }}Collections {
* input collection
* @return An immutable collection
*/
public static <T> Collection<T> immutable(Collection<T> collection) {
public static <T> Collection<T> immutable(Collection<? extends T> collection) {
if (collection == null) {
return Set.of();
}
Expand All @@ -39,7 +39,7 @@ public final class {{ baseName }}Collections {
* input set
* @return An immutable set
*/
public static <T> Set<T> immutable(Set<T> set) {
public static <T> Set<T> immutable(Set<? extends T> set) {
if (set == null) {
return Set.of();
}
Expand All @@ -56,7 +56,7 @@ public final class {{ baseName }}Collections {
* input list
* @return An immutable list
*/
public static <T> List<T> immutable(List<T> list) {
public static <T> List<T> immutable(List<? extends T> list) {
if (list == null) {
return List.of();
}
Expand All @@ -71,7 +71,7 @@ public final class {{ baseName }}Collections {
* @param <V> value type of this map
* @return An immutable map
*/
public static <K, V> Map<K, V> immutable(Map<K, V> map) {
public static <K, V> Map<K, V> immutable(Map<? extends K, ? extends V> map) {
if (map == null) {
return Map.of();
}
Expand Down
Loading
Loading