001package org.unix4j.util;
002
003import java.util.function.Predicate;
004
005import static java.util.Objects.requireNonNull;
006
007/**
008 * Enum constants for operating systems.
009 */
010public enum OS {
011        /** Windows, any version */
012        Windows(osName -> osName.contains("win")),
013        /** MAC */
014        Mac(osName -> osName.contains("mac")),
015        /** Linux, AIX or other Unix */
016        Unix(osName -> osName.contains("nix") || osName.contains("nux") || osName.contains("aix")),
017        /** SUN Solaris */
018        Solaris(osName -> osName.contains("sunos")),
019        /** Any other unrecognised OS */
020        Other(osName -> true);
021
022        private final Predicate<? super String> osNameMatcher;
023        
024        OS(final Predicate<? super String> osNameMatcher) {
025                this.osNameMatcher = requireNonNull(osNameMatcher);
026        }
027        
028        private static final OS CURRENT = initCurrent();
029
030        public boolean isCurrent() {
031                return this == CURRENT;
032        }
033
034        public static OS current() {
035                return CURRENT;
036        }
037
038        private static OS initCurrent() {
039                final String osName = System.getProperty("os.name").toLowerCase();
040                for (final OS os : values()) {
041                        if (os.osNameMatcher.test(osName)) {
042                                return os;
043                        }
044                }
045                //should not get here
046                return Other;
047        }
048}