From: Mateusz Warowny Date: Fri, 24 Mar 2023 15:16:15 +0000 (+0100) Subject: JAL-4154 Add Platform.forArch method. X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=14b480c749094aaa8d0aad58a3480479586d46cb;p=jalview.git JAL-4154 Add Platform.forArch method. Platform.forArch allows to specify constants that depend on architecture. --- diff --git a/src/jalview/util/Platform.java b/src/jalview/util/Platform.java index 573e2d5..5f51255 100644 --- a/src/jalview/util/Platform.java +++ b/src/jalview/util/Platform.java @@ -33,6 +33,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; +import java.util.Objects; import java.util.Properties; import javax.swing.SwingUtilities; @@ -138,6 +139,147 @@ public class Platform } /** + * Construct the value that depends on the system architecture. The methods + * setting the value for subsequent platforms are chained after this call and + * finalized with a {@link PlatformDependentValue#value() value()} call. + * + * Example: {@code + * Platform.forArch(120).forMac(114).forWin(112).forLinux(115).value(); + * } + * + * @param + * type of the value + * @param defaultValue + * default value used if platform not determined + * @return platform dependent value wrapper object + */ + public static PlatformDependentValue forArch(T defaultValue) + { + return new PlatformDependentValue(defaultValue); + } + + /** + * + * @author mmwarowny + * + * @param + * type of the value + */ + public static class PlatformDependentValue + { + private T defaultValue = null; + + private T macValue = null; + + private T winValue = null; + + private T linuxValue = null; + + private T jsValue = null; + + private T headlessValue = null; + + private PlatformDependentValue(T value) + { + Objects.requireNonNull(value); + defaultValue = value; + } + + /** + * Set the value used on Mac platform. + * + * @param value + * parameter value + * @return + */ + public PlatformDependentValue forMac(T value) + { + Objects.requireNonNull(value); + macValue = value; + return this; + } + + /** + * Set the value used on Windows platform. + * + * @param value + * parameter value + * @return + */ + public PlatformDependentValue forWin(T value) + { + Objects.requireNonNull(value); + winValue = value; + return this; + } + + /** + * Set the value used on Linux platform. + * + * @param value + * parameter value + * @return + */ + public PlatformDependentValue forLinux(T value) + { + Objects.requireNonNull(value); + linuxValue = value; + return this; + } + + /** + * Set the value used on JS platform. + * + * @param value + * parameter value + * @return + */ + public PlatformDependentValue forJS(T value) + { + Objects.requireNonNull(value); + jsValue = value; + return this; + } + + /** + * Set the value used on headless platform. The headless value takes + * precedence over other platforms if set. + * + * @param value + * parameter value + * @return + */ + public PlatformDependentValue forHeadless(T value) + { + Objects.requireNonNull(value); + headlessValue = value; + return this; + } + + /** + * Get the value of the parameter respecting the platform. The headless + * platform takes precedence over any other platform if it has the value + * set. + * + * @return parameter value depending on the platform + */ + public T value() + { + if (headlessValue != null && isHeadless()) + return headlessValue; + if (macValue != null && isMac()) + return macValue; + if (winValue != null && isWin()) + return winValue; + if (linuxValue != null && isLinux()) + return linuxValue; + if (jsValue != null && isJS()) + return jsValue; + return defaultValue; + } + } + + /** * * @return nominal maximum command line length for this platform */