patternjavaMinor
Efficiently returning the string basis on current datacenter
Viewed 0 times
efficientlythebasisdatacenterreturningcurrentstring
Problem
We have machine hostname as -
Here
Now I need to find out which
And also I have two flows as of now:
Problem Statement:
The only difference between
```
public enum DatacenterEnum {
DEV, DC1, DC2, DC3;
public static DatacenterEnum getOurlocation() {
return ourlocation;
dbx111.dc1.host.com
dbx112.dc2.host.com
dcx113.dc3.host.com
dcx115.dev.host.comHere
dc1, dc2, dc3 and dev are our datacenter and we will be having only four datacenter as of now. And also it might be possible that machine hostname can have more dots in between separated by another domain in future.Now I need to find out which
datacenter my current machine is in as I will be running below code on the actual machine. And also I have two flows as of now:
USERFLOW and DEVICEFLOW.public enum FlowTypeEnum {
USERFLOW, DEVICEFLOW
}Problem Statement:
- If my machine is in
DC1and flow type isUSERFLOWthen I need to return/test/datacenter/dc1but if flow type isDEVICEFLOWthen I need to return/testdevice/datacenter/dc1
- But if my machine is in
DC2and flow type isUSERFLOWthen I need to return/test/datacenter/dc2but if flow type isDEVICEFLOWthen I need to return/testdevice/datacenter/dc2.
- And if my machine is in
DC3and flow type isUSERFLOWthen I need to return/test/datacenter/dc3but if flow type isDEVICEFLOWthen I need to return/testdevice/datacenter/dc3.
- But if my machine datacenter is in
DEV, and flow type isUSERFLOWthen I need to return "/test/datacenter/dc1" but if flow type isDEVICEFLOWthen I need to return/testdevice/datacenter/dc1.
The only difference between
USERFLOW and DEVICEFLOW is - For USERFLOW, I need to use /test and for DEVICEFLOW, I need to use /testdevice and other things are same. TestingEnum class -public class TestingDatacenter {
public static void main(String[] args) {
String LOCAL_POOK = DatacenterEnum.getOurlocation().toLocalPook(FlowTypeEnum.USERFLOW);
System.out.println(LOCAL_POOK);
}
}DatacenterEnum class -```
public enum DatacenterEnum {
DEV, DC1, DC2, DC3;
public static DatacenterEnum getOurlocation() {
return ourlocation;
Solution
final is meaningless on static methods :private static final String getHostNameOfServer() {For the following :
// below if else, looks pretty odd, may be it can be improved better?
if (f.equals(FlowTypeEnum.DEVICEFLOW)) {
prefix = "/testdevice";
} else if (f.equals(FlowTypeEnum.USERFLOW)) {
prefix = "/test";
}there are 2 alternatives
-
make a getPrefix() method on FlowTypeEnum, reducing the above code to :
prefix = f.getPrefix();FlowTypeEnum could then look like this :
public enum FlowTypeEnum {
USERFLOW("/test"), DEVICEFLOW("/testdevice");
private final String prefix;
FlowTypeEnum(String prefix) {
this.prefix = prefix;
}
public String getPrefix() {
return prefix;
}
}-
make a map that maps the the FlowTypeEnum instances to a prefix, reducing the above code to :
prefix = mapToPrefix.get(f);In both cases you might even inline the prefix variable.
Try to make code resistant to likely changes. In this case, server names and paths are likely to change. Pull these from config files.
All these static methods don't seem to be at home on the
DatacenterEnum, they don't really operate on DatacenterEnum instances. Perhaps they need to find a home on a new class, where they can be non static.It is unwise to hard code the data centers as enum instances. It simply won't scale. Renaming, removing or adding data centers in the field will require a new release.
Use more meaningful names for variables :
dc->dataCenter
f->flowType
Replace this :
currenthost.indexOf(namepart) >= 0by the more readable :
currenthost.contains(namepart)Code Snippets
private static final String getHostNameOfServer() {// below if else, looks pretty odd, may be it can be improved better?
if (f.equals(FlowTypeEnum.DEVICEFLOW)) {
prefix = "/testdevice";
} else if (f.equals(FlowTypeEnum.USERFLOW)) {
prefix = "/test";
}public enum FlowTypeEnum {
USERFLOW("/test"), DEVICEFLOW("/testdevice");
private final String prefix;
FlowTypeEnum(String prefix) {
this.prefix = prefix;
}
public String getPrefix() {
return prefix;
}
}currenthost.indexOf(namepart) >= 0currenthost.contains(namepart)Context
StackExchange Code Review Q#55627, answer score: 3
Revisions (0)
No revisions yet.