From bbbb2ef0684fd96fce3c46b78356c47b624e70cc Mon Sep 17 00:00:00 2001 From: Mateusz Warowny Date: Fri, 24 Mar 2023 16:16:15 +0100 Subject: [PATCH] JAL-4154 Add Platform.forArch method. Platform.forArch allows to specify constants that depend on architecture. --- src/jalview/util/Platform.java | 142 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/src/jalview/util/Platform.java b/src/jalview/util/Platform.java index 5b89db3..11b6270 100644 --- a/src/jalview/util/Platform.java +++ b/src/jalview/util/Platform.java @@ -47,6 +47,7 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.Date; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Properties; import java.util.logging.ConsoleHandler; import java.util.logging.Level; @@ -229,6 +230,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 */ -- 1.7.10.2