Home > other >  How to conditionally execute many similar repeating lines of code in Java?
How to conditionally execute many similar repeating lines of code in Java?

Time:07-29

I have no idea how to really phrase this question without showing my code, I'm sorry for the vague title. I have two blocks of code that I need to find a way to simplify. I don't know what to do, because to my knowledge, I can't just replace the numbers with an iterator and use a for loop. I want to repeat this to 100 cases with the same pattern. I very much don't want to type all this out, even copy-pasting sounds like a nightmare. Is there any way at all to repeat this using loops?

Thank you :)

switch (count) {
    case 1: {
        binding1 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue1", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
        break;
    }
    case 2: {
        binding1 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue1", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
        binding2 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue2", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
        break;
    }
    case 3: {
        binding1 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue1", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
        binding2 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue2", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
        binding3 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue3", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
        break;
    }
}
switch (hotkeyConfig.bindingCount) {
    case 1:
        while (binding1.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue1);
        }
    case 2:
        while (binding1.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue1);
        }
        while (binding2.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue2);
        }
    case 3:
        while (binding1.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue1);
        }
        while (binding2.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue2);
        }
        while (binding3.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue3);
        }
}

CodePudding user response:

Use an array, instead of three separate variables, for your binding1, binding2, binding3. It might look something like this - I'm kind of guessing at what the classes involved might be.

KeyBinding[] bindings = new KeyBinding[3];
for (int index = 0; index < count; index  ) {
    bindings[index] = KeyBindingHelper.registerKeyBinding(
        new KeyBinding(
            "key.lightningtow.hotkey_keyValue"   (index   1),
            InputUtil.Type.KEYSYM, 
            GLFW.GLFW_KEY_UNKNOWN, 
            "key.category.hotkey"));
}

and if you have your hot key config bind values in a similar array, your second snippet might look like this.

for (int index = 0; index < count; index  ) {
    while (bindings[index].wasPressed()) {
        client.player.sendCommand(hotKeyConfigBindValue[index]);
    }
}

I'm assuming you didn't intend to omit the break statements from your second excerpt.

CodePudding user response:

For the first code block, just reverse the order of the individual cases and allow "fall through":

switch (count) {
    case 3: {
        binding3 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue3", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
    }
    case 2: {
        binding2 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue2", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
    }
    case 1: {
        binding1 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue1", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
        break;
    }
}

You could also reduce the above code by writing a createKeyBinding method:

public KeyBinding createKeyBinding(int index) {
    return new KeyBinding("key.lightningtow.hotkey_keyValue"   index, InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey")
}

Then the first block of code becomes:

switch (count) {
    case 3: {
        binding3 = KeyBindingHelper.registerKeyBinding(createKeyBinding(3));
    }
    case 2: {
        binding2 = KeyBindingHelper.registerKeyBinding(createKeyBinding(2));
    }
    case 1: {
        binding1 = KeyBindingHelper.registerKeyBinding(createKeyBinding(1));
        break;
    }
}

Alternatively, you could use an array rather than individual variables.
Note that since I could not ascertain the type for the variables, such as binding3, I use Object as the type.

Object[] bindings = new Object[count];
for (int i = count; --i >= 0;) {
    bindings[i] = KeyBindingHelper.registerKeyBinding(createKeyBinding(i));
}

The same can be applied to the second code block – assuming that the order of the while statements is not important, i.e.

switch (hotkeyConfig.bindingCount) {
    case 3:
        while (binding3.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue3);
        }
    case 2:
        while (binding2.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue2);
        }
    case 1:
        while (binding1.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue1);
        }
}

However, if the order is important, then if you use an array (as shown above), the second code block can be written as:

for (int i = 0; i < hotkeyConfig.bindingCount; i  ) {
    while (bindings[i].wasPressed()) {
        client.player.sendCommand(hotkeyConfig.bindValues[i]); // assumes appropriate array
    }
}
  • Related