Reputation: 386
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?
Upvotes: 2
Views: 1680
Reputation: 20614
There are multiple problems with this approach:
AtomicReference
etc).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. The code above only works for modifier
being a member not a local variable due to implicit final behavior.
Upvotes: 5