How do I perform a Boolean To Enum refactor in IntelliJ IDEA?

665 views Asked by At

How do I perform a Boolean To Enum refactor in IntelliJ IDEA?

For example, convert this:

void changeLights(boolean isOn) {
    this.isOn = isOn;
    }

changeLights(true);
changeLights(false);

Into this:

enum LightState { ON, OFF }

void changeLights(LightState lightState) {
    this.lightState = lightState;
    }

changeLights(LightState.ON);
changeLights(LightState.OFF);
1

There are 1 answers

2
Peter Lawrey On BEST ANSWER

I would add a method

@Deprecated
void changeLights(boolean isOn) {
    changeLights(isOn ? LightState.ON : LightState.OFF);
}

Then you can inline this method. Lastly you can "simplify" using the inspection analyse tool to simplify

changeLights(true ? LightState.ON : LightState.OFF); // use IDEA to simplify

to

changeLights(LightState.ON);

similar for false -> changeLights(LightState.OFF);