fix: equals/hashCode for plugin dependency, make them sorted too, closes #1327

This commit is contained in:
MiniDigger | Martin 2024-05-04 13:16:47 +02:00
parent 0d9e9995a0
commit 9a2e7a3bde
2 changed files with 14 additions and 6 deletions

View File

@ -11,9 +11,10 @@ import java.util.Objects;
import org.jdbi.v3.core.mapper.Nested;
import org.jdbi.v3.core.mapper.reflect.JdbiConstructor;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.NotNull;
@AtLeastOneNotNull(fieldNames = {"name", "namespace"}, includeBlankStrings = true, message = "Must specify a name or namespace for a dependency")
public class PluginDependency implements Named {
public class PluginDependency implements Named, Comparable<PluginDependency> {
@Schema(description = "Name of the plugin dependency. For non-external dependencies, this should be the Hangar project name", example = "Maintenance")
private final String name;
@ -72,15 +73,20 @@ public class PluginDependency implements Named {
if (this == o) return true;
if (o == null || this.getClass() != o.getClass()) return false;
final PluginDependency that = (PluginDependency) o;
return this.required == that.required && Objects.equals(this.name, that.name) && Objects.equals(this.externalUrl, that.externalUrl);
return this.required == that.required && Objects.equals(this.name, that.name) && Objects.equals(this.externalUrl, that.externalUrl) && Objects.equals(this.platform, that.platform);
}
@Override
public int hashCode() {
return Objects.hash(this.name, this.required, this.externalUrl);
return Objects.hash(this.name, this.required, this.externalUrl, this.platform);
}
public static PluginDependency of(final String name, final boolean required, final Platform platform) {
return new PluginDependency(name, required, platform);
}
@Override
public int compareTo(final @NotNull PluginDependency o) {
return this.name.compareTo(o.name);
}
}

View File

@ -24,10 +24,12 @@ import io.papermc.hangar.service.internal.PlatformService;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;
import io.papermc.hangar.util.VersionFormatter;
import org.springframework.beans.factory.annotation.Autowired;
@ -78,14 +80,14 @@ public class VersionDependencyService extends HangarComponent {
platformDependenciesFormatted.put(entry.getKey(), formattedVersionRange);
});
final Map<Platform, Set<PluginDependency>> pluginDependencies = this.versionsApiDAO.getPluginDependencies(versionId).stream()
.collect(Collectors.groupingBy(PluginDependency::getPlatform, Collectors.toSet()));
final Map<Platform, SortedSet<PluginDependency>> pluginDependencies = this.versionsApiDAO.getPluginDependencies(versionId).stream()
.collect(Collectors.groupingBy(PluginDependency::getPlatform, Collectors.toCollection(TreeSet::new)));
final Map<Platform, PlatformVersionDownload> downloads = this.downloadService.getDownloads(user, project, versionName, versionId);
return new DownloadsAndDependencies(pluginDependencies, platformDependencies, platformDependenciesFormatted, downloads);
}
public record DownloadsAndDependencies(Map<Platform, Set<PluginDependency>> pluginDependencies,
public record DownloadsAndDependencies(Map<Platform, SortedSet<PluginDependency>> pluginDependencies,
Map<Platform, SortedSet<String>> platformDependencies,
Map<Platform, String> platformDependenciesFormatted,
Map<Platform, PlatformVersionDownload> downloads