I've got two enums: level with 3 values and criticality with 4 values. A combination of those two maps to one of 8 values from priority enum. The mapping is non-linear and may change in future.
What is the best* way to implement a static function that takes level and criticality and outputs a priority?
*best being easy to read and understand, easy and safe to change, and not a performance hog. Extra points for a solution that takes into account that input domain can change in the future.
Ways I considered so far:
nested switch..case. Many lines and lots of boilerplate code. Also error-prone if you forget to return a value in a case. Basically the code looks like this:
switch (bc) {
case C1:
switch (el) {
case E1:
return EmergencyPriority.P1;
case E2:
return EmergencyPriority.P2;
case E3:
return EmergencyPriority.P3;
}
case C2:
switch (el) {
case E1:
return EmergencyPriority.P2;
case E2:
return EmergencyPriority.P3;
case E3:
return EmergencyPriority.P4;
}
case C3:
switch (el) {
case E1:
return EmergencyPriority.P4;
case E2:
return EmergencyPriority.P5;
case E3:
return EmergencyPriority.P6;
}
case C4:
switch (el) {
case E1:
return EmergencyPriority.P6;
case E2:
return EmergencyPriority.P7;
case E3:
return EmergencyPriority.P8;
}
}
Mutikey Map requires an external library and I haven't found a way to nicely insert initial values without many function calls and boilerplate composite keys.
if..else if.. else basically same as switch case but with more boilerplate code. Less error-prone though.
Two dimensional array when using the enum values as integers for array indices you risk failing silently if the positional enum values change.
Your solution here