Skip to content

Commit

Permalink
fix: equals/hashCode for plugin dependency, make them sorted too, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniDigger committed May 4, 2024
1 parent 0d9e999 commit 9a2e7a3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
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;
Expand Down Expand Up @@ -72,15 +73,20 @@ public boolean equals(final Object o) {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
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;
Expand Down Expand Up @@ -78,14 +80,14 @@ public DownloadsAndDependencies addDownloadsAndDependencies(final String user, f
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
Expand Down

0 comments on commit 9a2e7a3

Please sign in to comment.