001package org.unix4j.util;
002
003/**
004 * Enum constants for operating systems.
005 */
006public enum OS {
007        /** Windows, any version */
008        Windows {
009                @Override
010                public boolean isCurrent(String osName) {
011                        return osName.indexOf("win") >= 0;
012                }
013        },
014        /** MAC */
015        Mac {
016                @Override
017                public boolean isCurrent(String osName) {
018                        return osName.indexOf("mac") >= 0;
019                }
020        },
021        /** Linux or other Unix */
022        Unix {
023                @Override
024                public boolean isCurrent(String osName) {
025                        return osName.indexOf("nix") >= 0 || osName.indexOf("nux") >= 0;
026                }
027        },
028        /** SUN Solaris */
029        Solaris {
030                @Override
031                public boolean isCurrent(String osName) {
032                        return osName.indexOf("sunos") >= 0;
033                }
034        };
035        
036        public boolean isCurrent() {
037                return isCurrent(System.getProperty("os.name").toLowerCase());
038        }
039
040        abstract protected boolean isCurrent(String osName);
041        
042        public static OS current() {
043                for (final OS os : values()) {
044                        if (os.isCurrent()) return os;
045                }
046                throw new IllegalStateException("Cannot evaluate OS constant for current operating system: " + System.getProperty("os.name"));
047        }
048}