package org.unix4j.util;

/**
 * Enum constants for operating systems.
 */
public enum OS {
	/** Windows, any version */
	Windows {
		@Override
		public boolean isCurrent(String osName) {
			return osName.indexOf("win") >= 0;
		}
	},
	/** MAC */
	Mac {
		@Override
		public boolean isCurrent(String osName) {
			return osName.indexOf("mac") >= 0;
		}
	},
	/** Linux or other Unix */
	Unix {
		@Override
		public boolean isCurrent(String osName) {
			return osName.indexOf("nix") >= 0 || osName.indexOf("nux") >= 0;
		}
	},
	/** SUN Solaris */
	Solaris {
		@Override
		public boolean isCurrent(String osName) {
			return osName.indexOf("sunos") >= 0;
		}
	};
	
	public boolean isCurrent() {
		return isCurrent(System.getProperty("os.name").toLowerCase());
	}

	abstract protected boolean isCurrent(String osName);
	
	public static OS current() {
		for (final OS os : values()) {
			if (os.isCurrent()) return os;
		}
		throw new IllegalStateException("Cannot evaluate OS constant for current operating system: " + System.getProperty("os.name"));
	}
}
package org.unix4j.util;

import java.util.function.Predicate;

import static java.util.Objects.requireNonNull;

/**
 * Enum constants for operating systems.
 */
public enum OS {
	/** Windows, any version */
	Windows(osName -> osName.contains("win")),
	/** MAC */
	Mac(osName -> osName.contains("mac")),
	/** Linux, AIX or other Unix */
	Unix(osName -> osName.contains("nix") || osName.contains("nux") || osName.contains("aix")),
	/** SUN Solaris */
	Solaris(osName -> osName.contains("sunos")),
	/** Any other unrecognised OS */
	Other(osName -> true);

	private final Predicate<? super String> osNameMatcher;
	
	OS(final Predicate<? super String> osNameMatcher) {
		this.osNameMatcher = requireNonNull(osNameMatcher);
	}
	
	private static final OS CURRENT = initCurrent();

	public boolean isCurrent() {
		return this == CURRENT;
	}

	public static OS current() {
		return CURRENT;
	}

	private static OS initCurrent() {
		final String osName = System.getProperty("os.name").toLowerCase();
		for (final OS os : values()) {
			if (os.osNameMatcher.test(osName)) {
				return os;
			}
		}
		//should not get here
		return Other;
	}
}
Fix for issue #75: add 'aix' as unix OS and add Other for unrecognised OS instead of throwing an exception