leimooo leimooo - 5 months ago 14
Java Question

A adjustable/variable switch case in java

I have a lot of methods that look like the following:

switch(myEnum) {
case a :
case b :
case c :
case d :
modifier = 1.1;
break;
default :
break;
}


It would be perfect if i had the opportunity to make every case and the modifier variable, so i can have one method with 6 parameters, 4 for the cases and 2 for the modifier. Something like the following (pseudo):

public void setModifier(case1, case2, case3, case4, modifier, value) {
switch(myEnum) {
case case1 :
case case2 :
case case3 :
case case4 :
modifier = value;
break;
default :
break;
}
}


modifier and value shouldnt be a problem, but how do i solve the variable switch cases? Is this even possible in java with a switch-statement?

Answer

There are multiple problems with this approach:

  • You cannot change the value of a parameter passed so that the value is changed for the calling code (in Java, except for a holder kind type like AtomicReference etc).
  • Trying to make code generic because of possible reuse is not always a good idea, this tends to create complex generic code instead of readable specific.
  • It looks like your code is mostly an if, so why note write it in this way?

If I understand your snippet right, I would try the following inline approach:

if (EnumSet.of(a, b, c, d).contains(myEnum)) {
  modifier = 1.1;
}

This is from my perspective much cleaner and therefore has less need to extract it in a general way and very flexible (any number of enum values and any kind of action possible).

If you really need such an approach, a functional interface can do the job:

setModifier(myEnum, () -> modifier = 1.1, a, b, c, d);

public <E extends Enum<E>> void setModifier(E current, Runnable action, E... allowed) {
  Arrays.stream(allowed)
      .filter(one -> one == current)
      .findAny()
      .ifPresent(found -> action.run());
}

As you can see this creates a lot more code which is not necessary if not used spread around classes very often.

Comments