package com.bocloud.ims.service.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.bocloud.common.enums.OSCategory;

/**
 * OSCategory工具类
 * 
 * @author sxp
 * @since Jan 5, 2018
 */
public class OsCategoryUtils {
	private static Map<String, OSCategory> categorys = new HashMap<>(OSCategory.values().length);

	static {
		OSCategory[] values = OSCategory.values();
		for (OSCategory v : values) {
			categorys.put(v.name().toLowerCase(), v);
		}
	}

	/**
	 * 将字符串转换为OSCategory对象
	 * <p>
	 * 如果入参字符串不合法，返回OSCategory.LINUX
	 * </p>
	 * 
	 * @author sxp
	 * @since Jan 5, 2018
	 * @since 2018-01-10 modify by sxp 增加容错性，因为server中存储的OSCategory.LINUX,WINDOWS
	 */
	public static OSCategory getOSCategory(String categoryName) {
		if (StringUtils.isEmpty(categoryName)) {
			return OSCategory.LINUX;
		}

		String key = categoryName.toLowerCase();
		if (categorys.containsKey(key)) {
			return categorys.get(key);
		} else {
			return OSCategory.LINUX;
		}
	}

	/**
	 * 获取合法的操作系统类别
	 * 
	 * @author sxp
	 * @since Jan 12, 2018
	 */
	public static String getValidOSCategoryName(String categoryName) {
		return getOSCategory(categoryName).name();
	}
}
