From 2865f2ffca9ec3390c5637e8f7adfb07cbd0f278 Mon Sep 17 00:00:00 2001 From: Glavo Date: Mon, 12 Jan 2026 21:52:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=20InstallerItem.State=20=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E8=87=B3=20sealed=20=E6=8E=A5=E5=8F=A3=20(#5209)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/jackhuang/hmcl/ui/InstallerItem.java | 61 ++++--------------- .../hmcl/ui/download/InstallersPage.java | 4 +- 2 files changed, 14 insertions(+), 51 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/InstallerItem.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/InstallerItem.java index ba14e1f66..f16c09715 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/InstallerItem.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/InstallerItem.java @@ -63,7 +63,7 @@ public class InstallerItem extends Control { private final ObjectProperty onInstall = new SimpleObjectProperty<>(this, "onInstall"); private final ObjectProperty onRemove = new SimpleObjectProperty<>(this, "onRemove"); - public interface State { + public sealed interface State { } public static final class InstallableState implements State { @@ -73,46 +73,10 @@ public class InstallerItem extends Control { } } - public static final class IncompatibleState implements State { - private final String incompatibleItemName; - private final String incompatibleItemVersion; - - public IncompatibleState(String incompatibleItemName, String incompatibleItemVersion) { - this.incompatibleItemName = incompatibleItemName; - this.incompatibleItemVersion = incompatibleItemVersion; - } - - public String getIncompatibleItemName() { - return incompatibleItemName; - } - - public String getIncompatibleItemVersion() { - return incompatibleItemVersion; - } + public record IncompatibleState(String incompatibleItemName, String incompatibleItemVersion) implements State { } - public static final class InstalledState implements State { - private final String version; - private final boolean external; - private final boolean incompatibleWithGame; - - public InstalledState(String version, boolean external, boolean incompatibleWithGame) { - this.version = version; - this.external = external; - this.incompatibleWithGame = incompatibleWithGame; - } - - public String getVersion() { - return version; - } - - public boolean isExternal() { - return external; - } - - public boolean isIncompatibleWithGame() { - return incompatibleWithGame; - } + public record InstalledState(String version, boolean external, boolean incompatibleWithGame) implements State { } public enum Style { @@ -369,21 +333,20 @@ public class InstallerItem extends Control { statusLabel.textProperty().bind(Bindings.createStringBinding(() -> { State state = control.resolvedStateProperty.get(); - if (state instanceof InstalledState) { - InstalledState s = (InstalledState) state; - if (s.incompatibleWithGame) { - return i18n("install.installer.change_version", s.version); + if (state instanceof InstalledState installedState) { + if (installedState.incompatibleWithGame) { + return i18n("install.installer.change_version", installedState.version); } - if (s.external) { - return i18n("install.installer.external_version", s.version); + if (installedState.external) { + return i18n("install.installer.external_version", installedState.version); } - return i18n("install.installer.version", s.version); + return i18n("install.installer.version", installedState.version); } else if (state instanceof InstallableState) { return control.style == Style.CARD ? i18n("install.installer.do_not_install") : i18n("install.installer.not_installed"); - } else if (state instanceof IncompatibleState) { - return i18n("install.installer.incompatible", i18n("install.installer." + ((IncompatibleState) state).incompatibleItemName)); + } else if (state instanceof IncompatibleState incompatibleState) { + return i18n("install.installer.incompatible", i18n("install.installer." + incompatibleState.incompatibleItemName)); } else { throw new AssertionError("Unknown state type: " + state.getClass()); } @@ -404,7 +367,7 @@ public class InstallerItem extends Control { } else { removeButton.visibleProperty().bind(Bindings.createBooleanBinding(() -> { State state = control.resolvedStateProperty.get(); - return state instanceof InstalledState && !((InstalledState) state).external; + return state instanceof InstalledState installedState && !installedState.external; }, control.resolvedStateProperty)); } removeButton.managedProperty().bind(removeButton.visibleProperty()); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/InstallersPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/InstallersPage.java index 346c4a0df..3635a73d2 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/InstallersPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/InstallersPage.java @@ -50,7 +50,7 @@ public class InstallersPage extends AbstractInstallersPage { @Override public String getTitle() { - return group.getGame().versionProperty().get().getVersion(); + return group.getGame().versionProperty().get().version(); } private String getVersion(String id) { @@ -111,7 +111,7 @@ public class InstallersPage extends AbstractInstallersPage { } private void setTxtNameWithLoaders() { - StringBuilder nameBuilder = new StringBuilder(group.getGame().versionProperty().get().getVersion()); + StringBuilder nameBuilder = new StringBuilder(group.getGame().versionProperty().get().version()); for (InstallerItem library : group.getLibraries()) { String libraryId = library.getLibraryId().replace(LibraryAnalyzer.LibraryType.MINECRAFT.getPatchId(), "");