Skip to content

Commit 9162a9b

Browse files
raveningRakesh Venkatesh
andauthored
Avoid multiple if else (#6171)
Use map to avoid multiple if else statement. This will support more types in future mithout much code change Co-authored-by: Rakesh Venkatesh <rakeshv@apache.org>
1 parent 1593736 commit 9162a9b

File tree

1 file changed

+33
-43
lines changed

1 file changed

+33
-43
lines changed

api/src/main/java/com/cloud/hypervisor/Hypervisor.java

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@
1818

1919
import com.cloud.storage.Storage.ImageFormat;
2020

21+
import java.util.HashMap;
22+
import java.util.Locale;
23+
import java.util.Map;
24+
2125
public class Hypervisor {
2226

27+
static Map<String, HypervisorType> hypervisorTypeMap;
28+
static Map<HypervisorType, ImageFormat> supportedImageFormatMap;
29+
2330
public static enum HypervisorType {
2431
None, //for storage hosts
2532
XenServer,
@@ -36,37 +43,32 @@ public static enum HypervisorType {
3643

3744
Any; /*If you don't care about the hypervisor type*/
3845

46+
static {
47+
hypervisorTypeMap = new HashMap<>();
48+
hypervisorTypeMap.put("xenserver", HypervisorType.XenServer);
49+
hypervisorTypeMap.put("kvm", HypervisorType.KVM);
50+
hypervisorTypeMap.put("vmware", HypervisorType.VMware);
51+
hypervisorTypeMap.put("hyperv", HypervisorType.Hyperv);
52+
hypervisorTypeMap.put("virtualbox", HypervisorType.VirtualBox);
53+
hypervisorTypeMap.put("parallels", HypervisorType.Parralels);
54+
hypervisorTypeMap.put("baremetal", HypervisorType.BareMetal);
55+
hypervisorTypeMap.put("simulator", HypervisorType.Simulator);
56+
hypervisorTypeMap.put("ovm", HypervisorType.Ovm);
57+
hypervisorTypeMap.put("lxc", HypervisorType.LXC);
58+
hypervisorTypeMap.put("any", HypervisorType.Any);
59+
hypervisorTypeMap.put("ovm3", HypervisorType.Ovm3);
60+
61+
supportedImageFormatMap = new HashMap<>();
62+
supportedImageFormatMap.put(HypervisorType.XenServer, ImageFormat.VHD);
63+
supportedImageFormatMap.put(HypervisorType.KVM, ImageFormat.QCOW2);
64+
supportedImageFormatMap.put(HypervisorType.VMware, ImageFormat.OVA);
65+
supportedImageFormatMap.put(HypervisorType.Ovm, ImageFormat.RAW);
66+
supportedImageFormatMap.put(HypervisorType.Ovm3, ImageFormat.RAW);
67+
}
68+
3969
public static HypervisorType getType(String hypervisor) {
40-
if (hypervisor == null) {
41-
return HypervisorType.None;
42-
}
43-
if (hypervisor.equalsIgnoreCase("XenServer")) {
44-
return HypervisorType.XenServer;
45-
} else if (hypervisor.equalsIgnoreCase("KVM")) {
46-
return HypervisorType.KVM;
47-
} else if (hypervisor.equalsIgnoreCase("VMware")) {
48-
return HypervisorType.VMware;
49-
} else if (hypervisor.equalsIgnoreCase("Hyperv")) {
50-
return HypervisorType.Hyperv;
51-
} else if (hypervisor.equalsIgnoreCase("VirtualBox")) {
52-
return HypervisorType.VirtualBox;
53-
} else if (hypervisor.equalsIgnoreCase("Parralels")) {
54-
return HypervisorType.Parralels;
55-
} else if (hypervisor.equalsIgnoreCase("BareMetal")) {
56-
return HypervisorType.BareMetal;
57-
} else if (hypervisor.equalsIgnoreCase("Simulator")) {
58-
return HypervisorType.Simulator;
59-
} else if (hypervisor.equalsIgnoreCase("Ovm")) {
60-
return HypervisorType.Ovm;
61-
} else if (hypervisor.equalsIgnoreCase("LXC")) {
62-
return HypervisorType.LXC;
63-
} else if (hypervisor.equalsIgnoreCase("Any")) {
64-
return HypervisorType.Any;
65-
} else if (hypervisor.equalsIgnoreCase("Ovm3")) {
66-
return HypervisorType.Ovm3;
67-
} else {
68-
return HypervisorType.None;
69-
}
70+
return hypervisor == null ? HypervisorType.None :
71+
hypervisorTypeMap.getOrDefault(hypervisor.toLowerCase(Locale.ROOT), HypervisorType.None);
7072
}
7173

7274
/**
@@ -76,19 +78,7 @@ public static HypervisorType getType(String hypervisor) {
7678
* @return
7779
*/
7880
public static ImageFormat getSupportedImageFormat(HypervisorType hyperType) {
79-
if (hyperType == HypervisorType.XenServer) {
80-
return ImageFormat.VHD;
81-
} else if (hyperType == HypervisorType.KVM) {
82-
return ImageFormat.QCOW2;
83-
} else if (hyperType == HypervisorType.VMware) {
84-
return ImageFormat.OVA;
85-
} else if (hyperType == HypervisorType.Ovm) {
86-
return ImageFormat.RAW;
87-
} else if (hyperType == HypervisorType.Ovm3) {
88-
return ImageFormat.RAW;
89-
} else {
90-
return null;
91-
}
81+
return supportedImageFormatMap.getOrDefault(hyperType, null);
9282
}
9383
}
9484

0 commit comments

Comments
 (0)