如何重写此方法以最大限度地提高代码效率并最小化代码长度?
注意:HEADER
是之前定义的常量。
private static URL getURL(String OS, String type, String size) {
String prefix = "";
String prefixLarge = "";
if (type.equals("image")) {
prefix = HEADER + "/image";
prefixLarge = HEADER + "/image_large";
} else {
prefix = HEADER + "/file";
prefixLarge = HEADER + "/file_raw";
}
String suffix = ".zip";
String url = "";
if (OS.contains("Win7.32bit")) {
// win7-32bit system
url = prefix + "Win7.32bit" + suffix;
if (size.contains("origin")) {
url = prefixLarge + "Win7.32bit" + suffix;
}
} else if (OS.contains("Win7.64bit")) {
// win7-64bit system
url = prefix + "Win7.64bit" + suffix;
if (size.contains("origin")) {
url = prefixLarge + "Win7.64bit" + suffix;
}
} else if (OS.contains("WinXP")) {
// winxp-32bit system
url = prefix + "WinXP" + suffix;
if (size.contains("origin")) {
url = prefixLarge + "WinXP" + suffix;
}
} else if (OS.contains("WinXP.64bit")) {
// winxp-64bit system
url = prefix + "WinXP.64bit" + suffix;
if (size.contains("origin")) {
url = prefixLarge + "WinXP.64bit" + suffix;
}
} else if (OS.contains("10.04")) {
// ubuntu10.04 system
url = prefix + "ubuntu10.04" + suffix;
if (size.contains("origin")) {
url = prefixLarge + "ubuntu10.04" + suffix;
}
} else if (OS.contains("12.04")) {
// ubuntu12.04 system
url = prefix + "ubuntu12.04" + suffix;
if (size.contains("origin")) {
url = prefixLarge + "ubuntu12.04" + suffix;
}
}
return url;
}
答案 0 :(得分:7)
创建一个包含所有操作系统名称的Strings
数组。然后使用for
循环遍历该数组。
像这样:
String[] osNames = { "Win7.32bit", "Win7.64bit", "10.04" };
String[] osTypes = { "Win7.32bit", "Win7.64bit", "ubuntu10.04" };
for (int i = 0; i < Math.min(osNames.length, osTypes.length); i++) {
if (OS.contains(osName[i])) {
url = prefix + osType[i] + suffix;
if (size.contains("origin")) {
url = prefixLarge + osType[i] + suffix;
}
break;
}
}
答案 1 :(得分:3)
private static Map <String, String> osMap = new HashMap<String, String>();
static {
osMap.put("Win7.32bit", "Win7.32bit");
osMap.put("Win7.64bit", "Win7.64bit");
osMap.put("WinXP", "WinXP");
osMap.put("WinXP.64bit", "WinXP.64bit");
osMap.put("10.04", "ubuntu10.04");
osMap.put("12.04", "ubuntu12.04");
}
private static URL getURL(String os, String type, String size) {
String prefix = "";
String prefixLarge = "";
if (type.equals("image")) {
prefix = HEADER + "/image";
prefixLarge = HEADER + "/image_large";
} else {
prefix = HEADER + "/file";
prefixLarge = HEADER + "/file_raw";
}
String suffix = ".zip";
String url = "";
String systemType = "";
for(String key : osMap.keySet()) {
if(os.contains(key) {
url = prefix + osMap.get(key) + suffix;
if (size.contains("origin")) {
url = prefixLarge + osMap.get(key) + suffix;
}
break;
}
}
return url;
}
答案 2 :(得分:3)
private static URL getURL(String OS, String type, String size) {
return HEADER
+
size.contains("origin")
? ((type.equals("image") ? "/image_large" : "/file_raw" ))
: ((type.equals("image") ? "/image" : "/file" ))
+
OS.contains("Win7.32bit") ? "Win7.32bit" :
OS.contains("Win7.64bit") ? "Win7.64bit" :
OS.contains("WinXP.64bit") ? "WinXP.64bit" :
OS.contains("WinXP") ? "WinXP" :
OS.contains("10.04") ? "ubuntu10.04" :
OS.contains("12.04") ? "ubuntu12.04" :
""
+
".zip";
}
答案 3 :(得分:2)
String[] osNames={"Win7.32bit", "Win7.64bit"};
for(String os : osNames){
if(OS.contains(os)){
url = prefix + os + suffix;
if (size.contains("origin")) {
url = prefixLarge + os + suffix;
}
break;
}
}
这是删除if-else条件的代码的一部分。注意break语句。