Enum Class Platform

java.lang.Object
java.lang.Enum<Platform>
nl.colorize.util.Platform
All Implemented Interfaces:
Serializable, Comparable<Platform>, Constable

public enum Platform extends Enum<Platform>
Provides access to the underlying platform. This includes basic information such as the platform's name, but also platform-specific behavior such as the expected location for storing application data.

Most of this information can also be obtained from system properties, environment variables, or the System class. However, working directly with these properties is error-prone, since the properties are scattered across various locations, need to be parsed by applications, and do not always behave consistently across different platforms. In addition to that, the standard APIs have not always been updated to reflect modern best practices for each platform. For example, the java.util.prefs API for storing application preferences was never updated for the introduction of the Mac App Store in 2010, or Mac App Store sandboxing in 2014. This class therefore provides a way to manage application data and preferences in a way that is considered suitable and native for each platform.

  • Enum Constant Details

    • WINDOWS

      public static final Platform WINDOWS
    • MAC

      public static final Platform MAC
    • LINUX

      public static final Platform LINUX
    • GOOGLE_CLOUD

      public static final Platform GOOGLE_CLOUD
    • ANDROID

      public static final Platform ANDROID
    • IOS

      public static final Platform IOS
    • TEAVM

      public static final Platform TEAVM
    • UNKNOWN

      public static final Platform UNKNOWN
  • Method Details

    • values

      public static Platform[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static Platform valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null
    • toString

      public String toString()
      Returns the display name for this platform family. Examples are "macOS", "Windows", and "Google Cloud". The platform family does not include the platform version, use getPlatformName() to obtain the complete display name.
      Overrides:
      toString in class Enum<Platform>
    • getPlatform

      public static Platform getPlatform()
      Returns the current platform family. This does not return the specific platform version, as most platform-specific behavior is based on the platform family rather than the specific versions. getPlatformName() can be used to obtain the full platform display name if version-specific behavior is required.

      When running on TeaVM, this method will always return TEAVM, regardless of the underlying operating system and/or browser.

    • getPlatformName

      public static String getPlatformName()
      Returns the display name for the current platform, for example "Windows 11" or "macOS Ventura". Platform-specific behavior is more likely to be based on the platform family rather than the specific platform. Applications should therefore prefer getPlatform() instead of parsing the display name string returned by this method.

      When running via TeaVM, this method will always return "TeaVM", regardless of the underlying operating system and/or browser.

    • is

      public static boolean is(Platform platformFamily)
      Convenience method that can be used to check the current platform. For example, using Platform.is(Platform.MAC) is a shorthand for Platform.getPlatform().equals(Platform.MAC).
    • isWindows

      public static boolean isWindows()
    • isMac

      public static boolean isMac()
    • isLinux

      public static boolean isLinux()
    • isGoogleCloud

      public static boolean isGoogleCloud()
    • isTeaVM

      public static boolean isTeaVM()
    • isMacAppStore

      public static boolean isMacAppStore()
      Returns true if the current platform is running on a Mac and if the Java Virtual Machine is running from within the Mac App Store sandbox.
    • getJavaVersion

      public static Version getJavaVersion()
      Returns the version of the Java Virtual Machine. Examples of returned version numbers are "1.7.0_55" and "21.0.1". Detection is based on the value of the java.version system property. Returns Version.UNKNOWN if the current platform does not provide Java version information.
    • getUsedHeapMemory

      @Deprecated public static long getUsedHeapMemory()
      Deprecated.
      Returns the amount of heap memory that is currently being used by the Java Virtual Machine, in bytes.
    • getNativeLibraryPath

      public static List<File> getNativeLibraryPath()
      Returns the native library path, indicated by the system property java.library.path, as a list of File objects that represent each directory.
      Throws:
      UnsupportedOperationException - if the current platform does not allow the application to load or access native libraries at runtime.
    • getUserWorkingDirectory

      public static File getUserWorkingDirectory()
      Returns a File that points to the current working directory.
      Throws:
      UnsupportedOperationException - on platforms that do not have the concept of a working directory.
    • getUserAccount

      public static String getUserAccount()
      Returns the name of the current user.
      Throws:
      UnsupportedOperationException - on platforms that do not support user accounts. Examples are Android and Google App Engine.
    • getUserHomeDir

      public static File getUserHomeDir()
      Returns a File that points to the current user's home directory.
      Throws:
      UnsupportedOperationException - on platforms that do not support user accounts. Examples are Android and Google App Engine.
    • getUserDesktopDir

      public static File getUserDesktopDir()
      Returns a File that points to the current user's desktop directory. Note that this method is only available on desktop platforms, and that even then some platforms might not allow applications to write directly to the user's desktop.
      Throws:
      UnsupportedOperationException - if the user desktop is not available or not accessible.
    • getApplicationDataDirectory

      public static File getApplicationDataDirectory(String app)
      Returns the platform's directory for storing application data, for the application with the specified name.
      Throws:
      IllegalArgumentException - If the application name cannot be used as a directory name.
      UnsupportedOperationException - if the platform does not allow application data (e.g. Google App Engine).
    • getApplicationData

      public static File getApplicationData(String app, String path)
      Returns a File handle that points to a file inside the platform's directory for storing application data, for the application with the specified name. If the application data directory does not already exist it will be created.
      Parameters:
      app - Name of the application for which to create the directory.
      Throws:
      IllegalArgumentException - If the application name cannot be used as a directory name, if the path is empty, or if an absolute path is used instead of a relative path.
      UnsupportedOperationException - if the platform does not allow application data (e.g. Google App Engine).
    • getUserDataDirectory

      public static File getUserDataDirectory()
      Returns the platform's standard directory for storing user data (e.g. "My Documents" on Windows).
      Throws:
      UnsupportedOperationException - if the platform does not allow access to user files (e.g. Google App Engine).
    • getUserData

      public static File getUserData(String path)
      Returns a File handle that points to a file in the platform's standard directory for storing user data (e.g. "My Documents" on Windows).
      Throws:
      IllegalArgumentException - if the path is absolute.
      UnsupportedOperationException - if the platform does not allow access to user files (e.g. Google App Engine).
    • getTempDir

      public static File getTempDir()
      Returns the platform's directory for temporary files.
      Throws:
      UnsupportedOperationException - if the platform has no writable file system. An example is Google App Engine.
    • getDefaultTimeZone

      public static TimeZone getDefaultTimeZone()
      Returns the default timezone. By default, this will return the Europe/Amsterdam time zone. The default time zone can be set explicitly using the environment variable "COLORIZE_TIMEZONE". If the requested time zone is not available on the current platform, the GMT time zone is used instead.