I have two environment PROD
and STAGING
. In prod environment we have three datacenters ABC
, DEF
and PQR
and staging has one datacenter CORP
. Each datacenter has few machines and I have constant defined for them as shown below:
// NOTE: I can have more machines in each dc in future
public static final ImmutableList<String> ABC_SERVERS = ImmutableList.of("tcp://machineA:8081", "tcp://machineA:8082");
public static final ImmutableList<String> DEF_SERVERS = ImmutableList.of("tcp://machineB:8081", "tcp://machineB:8082");
public static final ImmutableList<String> PQR_SERVERS = ImmutableList.of("tcp://machineC:8081", "tcp://machineC:8082");
public static final ImmutableList<String> STAGING_SERVERS = ImmutableList.of("tcp://machineJ:8087","tcp://machineJ:8088");
Now I have another constant defined in the same class which groups by DC to List of machines for each environment type.
public static final ImmutableMap<Datacenter, ImmutableList<String>> PROD_SERVERS_BY_DC =
ImmutableMap.<Datacenter, ImmutableList<String>>builder()
.put(Datacenter.ABC, ABC_SERVERS).put(Datacenter.DEF, DEF_SERVERS)
.put(Datacenter.PQR, PQR_SERVERS).build();
public static final ImmutableMap<Datacenter, ImmutableList<String>> STAGING_SERVERS_BY_DC =
ImmutableMap.<Datacenter, ImmutableList<String>>builder()
.put(Datacenter.CORP, STAGING_SERVERS).build();
Now in some other class, basis on what environment I am in (Utils.isProd())
, I get either PROD_SERVERS_BY_DC
or STAGING_SERVERS_BY_DC
.
Map<Datacenter, ImmutableList<String>> machinesByDC = Utils.isProd() ? Utils.PROD_SERVERS_BY_DC : Utils.STAGING_SERVERS_BY_DC;
Now I think this can be represented in much better way in some sort of Enum instead of having constants defined like I have above but I am not able to figure out how can I do that? I started off with this but got confuse on how can I have single key for each DC and then multiple values as List of machines for that DC and then I need to group them by environment as well.
// got confuse on how can I make key (DC) and list of values (machines) for each environment type.
public enum DCToMachines {
abc(tcp://machineA:8081", "tcp://machineA:8082"),
private final List<String> machines;
private final Datacenter datacenter;
...
}
While this is a bit tricky without knowing full implementation I might be able to get you started.
Enums are interesting in that they are Immutable which solves the declaring constant fields and variables. Here is my attempt to solve what you are looking for.
I then checked that each "machine" was being added in another class
Also note that to get the actual enum names you use the .values() function of the Enum class.
You forgot an important just : enum may store members (method and fields) specific to each enum value but it may also store
static
members if required.What you need is moving the maps that groups by environment directly in the enum class :
You could use an inner map to return the expected map according to the user request :
Where
Env
is another enum :In the code that I will present I added as an enclosing type of
Datacenter
but it would be probably better in its own file as the environment is probably not a concept used exclusively by datacenters.At last, the way you are using to retrieve servers by environment splits related responsibilities in 2 classes (
Datacenter
enum and the current class) :introducing in the enum a method to do this operation would be nicer :
Which would increase the cohesion of the enum class.
Here is the enum updated :
And here how to use it :
Output :