Record Class Platform
- Record Components:
os- the operating system familyarch- the processor architecture the JVM was built for, normalized:x86_64,aarch64, or whateveros.archsaid, lower-cased, when it is neither
Every module has something it does differently per platform: which modifier a menu
accelerator means, whether a window may carry an icon, where the system fonts live, which
native library directory to load from, whether the JVM has to be started on the first thread.
Each of those used to read os.name for itself, and spelled the test its own way —
startsWith("Mac") here, contains("mac") there, and a contains("win")
that only failed to match Darwin because the macOS test happened to come first on the
same line. This is the one reading, so that a platform added or a spelling corrected is a
change in one file.
It answers about the JVM, never about the machine. An x86_64 JVM under Rosetta on an
Apple Silicon Mac reports os.arch=x86_64 and can load only an x86_64 library; asking
the hardware would find arm64, hand the JVM a library it cannot map, and produce an
UnsatisfiedLinkError that names no cause. The same holds for a 32-bit JVM on a 64-bit
Windows install. So the architecture here is what the JVM says it is.
of(String, String) is the reading as a pure function of the two property values,
so that every branch a caller takes can be tested on a machine that is not that platform.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe operating system families the toolkit tells apart. -
Constructor Summary
ConstructorsConstructorDescriptionPlatform(Platform.Os os, String arch) Creates an instance of aPlatformrecord class. -
Method Summary
Modifier and TypeMethodDescriptionarch()Returns the value of thearchrecord component.static Platformcurrent()final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.booleanisLinux()booleanisMacOs()booleanstatic PlatformThe platform a givenos.nameandos.archdescribe.os()Returns the value of theosrecord component.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
Method Details
-
current
- Returns:
- the platform this JVM is running on, read once from the system properties
-
of
The platform a givenos.nameandos.archdescribe.The family is matched by substring of the lower-cased name, macOS first:
macordarwin, thenwin, thenlinuxornux. The order matters once —Darwincontainswin— and this is the only place it has to be right. The architecture folds the two spellings each of the common ones has (amd64/x86_64,arm64/aarch64) into one, and passes any other through lower-cased rather than pretending to know it.- Parameters:
osName- whatSystem.getProperty("os.name")answered; never nullosArch- whatSystem.getProperty("os.arch")answered; never null- Returns:
- the platform they describe
-
isMacOs
public boolean isMacOs()- Returns:
- whether this is macOS
-
isWindows
public boolean isWindows()- Returns:
- whether this is Windows
-
isLinux
public boolean isLinux()- Returns:
- whether this is Linux
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
os
Returns the value of theosrecord component.- Returns:
- the value of the
osrecord component
-
arch
Returns the value of thearchrecord component.- Returns:
- the value of the
archrecord component
-