From d1efb4f983ffb4a797db0720c348afb00224a1c2 Mon Sep 17 00:00:00 2001 From: Glavo Date: Wed, 28 Oct 2026 03:13:28 +0800 Subject: [PATCH] refactor: update TabItem to improve nullability handling and simplify getNode logic --- .../jackhuang/hmcl/ui/construct/TabItem.java | 40 ++++++++----------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TabItem.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TabItem.java index 34c253ea59..7eaa9299dd 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TabItem.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TabItem.java @@ -13,16 +13,16 @@ /// /// @param the type of content node @NotNullByDefault -public final class TabItem { +public final class TabItem { private @Nullable T node; - private @Nullable Supplier nodeSupplier; + private @Nullable Supplier nodeSupplier; /// Creates a tab with no text, content, or content factory. public TabItem() { } - /// Creates a tab whose content is supplied lazily by [#initializeIfNeeded()]. - public TabItem(Supplier nodeSupplier) { + /// Creates a tab whose content is supplied lazily by `nodeSupplier`. + public TabItem(Supplier nodeSupplier) { this.nodeSupplier = nodeSupplier; } @@ -50,30 +50,22 @@ public void setText(@Nullable String text) { /// Returns the content node, invoking the factory if content has not been initialized. /// Returns null if no factory is set or the factory returns null; later calls may retry. /// Factory exceptions propagate without assigning content. - public @Nullable T getNode() { - initializeIfNeeded(); - return node; + public T getNode() { + if (node != null) + return node; + + if (nodeSupplier != null) { + node = nodeSupplier.get(); + nodeSupplier = null; + return node; + } + + return null; } /// Returns whether a non-null content node is currently assigned. public boolean isInitialized() { - return node != null; + return nodeSupplier == null; } - /// Invokes the content factory and assigns its result if the content is null - /// and a factory is present. A null result permits another attempt on the next call. - /// Factory exceptions propagate without assigning a result. - /// - /// @return true if the factory returned and its result was assigned, even if null; - /// false if content was already assigned or no factory was present - public boolean initializeIfNeeded() { - if (node == null) { - if (nodeSupplier == null) { - return false; - } - node = nodeSupplier.get(); - return true; - } - return false; - } }