com.byteground.Platform Maven / Gradle / Ivy
/**
* Copyright © 2009-2014 ByTeGround, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.byteground;
import java.util.HashMap;
import java.util.Map;
final public class Platform {
public static final Platform UNKNOWN = new Platform(Os.UNKNOWN, Architecture.UNKNOWN);
public static final Platform WINDOWS_32 = new Platform(Os.WINDOWS, Architecture._32);
public static final Platform WINDOWS_64 = new Platform(Os.WINDOWS, Architecture._64);
public static final Platform LINUX_32 = new Platform(Os.LINUX, Architecture._32);
public static final Platform LINUX_64 = new Platform(Os.LINUX, Architecture._64);
public static final Platform MACOSX_32 = new Platform(Os.MACOSX, Architecture._32);
public static final Platform MACOSX_64 = new Platform(Os.MACOSX, Architecture._64);
private static final Platform[] PLATFORMS = new Platform[]{
WINDOWS_32, WINDOWS_64,
LINUX_32, LINUX_64,
MACOSX_32, MACOSX_64
};
private static final Map PLATFORM_MAP = new HashMap();
static {
for (Platform platform : PLATFORMS) {
PLATFORM_MAP.put(platform.name, platform);
}
}
public static Platform[] getAll() {
return PLATFORMS;
}
public static Platform getByName(String name) {
Platform platform = PLATFORM_MAP.get(name);
if (platform == null)
return UNKNOWN;
else
return platform;
}
public static Boolean isCurrent(Platform platform) {
return platform != null && platform.equals(getCurrent());
}
// @see http://lopica.sourceforge.net/os.html
public static Platform getCurrent() {
final Os os = Os.getCurrent();
final Architecture architecture = Architecture.getCurrent();
final Platform platform = PLATFORM_MAP.get(os.name + architecture.name);
if (platform == null)
return UNKNOWN;
else
return platform;
}
private Platform(Os os, Architecture architecture) {
this.os = os;
this.architecture = architecture;
this.name = this.os.name + this.architecture.name;
}
@Override
public int hashCode() {
return this.name.hashCode();
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
else if (o instanceof Platform) {
Platform p = (Platform) o;
return this.name.equals(p.name);
} else
return false;
}
@Override
public String toString() {
return this.name;
}
public final Os os;
public final Architecture architecture;
public final String name;
}