001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.tools;
003
004import java.util.Locale;
005
006/**
007 * Enum listing the supported platforms (operating system families).
008 * @since 12776
009 */
010public enum Platform {
011
012    /**
013     * Unik-like platform. This is the default when the platform cannot be identified.
014     */
015    UNIXOID {
016        @Override
017        public <T> T accept(PlatformVisitor<T> visitor) {
018            return visitor.visitUnixoid();
019        }
020    },
021    /**
022     * Windows platform.
023     */
024    WINDOWS {
025        @Override
026        public <T> T accept(PlatformVisitor<T> visitor) {
027            return visitor.visitWindows();
028        }
029    },
030    /**
031     * macOS (previously OS X) platform.
032     */
033    OSX {
034        @Override
035        public <T> T accept(PlatformVisitor<T> visitor) {
036            return visitor.visitOsx();
037        }
038    };
039
040    private static volatile Platform platform;
041
042    /**
043     * Support for the visitor pattern.
044     * @param <T> type that will be the result of the visiting operation
045     * @param visitor the visitor
046     * @return result of the operation
047     */
048    public abstract <T> T accept(PlatformVisitor<T> visitor);
049
050    /**
051     * Identifies the current operating system family.
052     * @return the the current operating system family
053     */
054    public static Platform determinePlatform() {
055        if (platform == null) {
056            String os = Utils.getSystemProperty("os.name");
057            if (os == null) {
058                Logging.warn("Your operating system has no name, so I'm guessing its some kind of *nix.");
059                platform = Platform.UNIXOID;
060            } else if (os.toLowerCase(Locale.ENGLISH).startsWith("windows")) {
061                platform = Platform.WINDOWS;
062            } else if ("Linux".equals(os) || "Solaris".equals(os) ||
063                    "SunOS".equals(os) || "AIX".equals(os) ||
064                    "FreeBSD".equals(os) || "NetBSD".equals(os) || "OpenBSD".equals(os)) {
065                platform = Platform.UNIXOID;
066            } else if (os.toLowerCase(Locale.ENGLISH).startsWith("mac os x")) {
067                platform = Platform.OSX;
068            } else {
069                Logging.warn("I don't know your operating system '"+os+"', so I'm guessing its some kind of *nix.");
070                platform = Platform.UNIXOID;
071            }
072        }
073        return platform;
074    }
075
076}