refactoring: issue#2376 (#2491)

Changed all the switch expression according to the JAVA 17.
This commit is contained in:
akshatarora0013 authored and GitHub committed 2023-04-01 17:48:38 +03:00
1 parent fc7e672419
commit 9d6ae392b8
23 files changed
+165 -281

No files matched your search

+8 -11
View File
@@ -153,18 +153,15 @@ public static class FactoryMaker {
} }
public static KingdomFactory makeFactory(KingdomType type) { public static KingdomFactory makeFactory(KingdomType type) {
switch (type) { return switch (type) {
case ELF: case ELF -> new ElfKingdomFactory();
return new ElfKingdomFactory(); case ORC -> new OrcKingdomFactory();
case ORC: default -> throw new IllegalArgumentException("KingdomType not supported.");
return new OrcKingdomFactory(); };
default:
throw new IllegalArgumentException("KingdomType not supported.");
}
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
var app = new App(); var app = new App();
LOGGER.info("Elf Kingdom"); LOGGER.info("Elf Kingdom");
@@ -175,8 +172,8 @@ public static void main(String[] args) {
LOGGER.info("Orc Kingdom"); LOGGER.info("Orc Kingdom");
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC)); app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC));
-- similar use of the orc factory --similar use of the orc factory
} }
``` ```
## Class diagram ## Class diagram
@@ -54,14 +54,11 @@ public class Kingdom {
* The factory method to create KingdomFactory concrete objects. * The factory method to create KingdomFactory concrete objects.
*/ */
public static KingdomFactory makeFactory(KingdomType type) { public static KingdomFactory makeFactory(KingdomType type) {
switch (type) { return switch (type) {
case ELF: case ELF -> new ElfKingdomFactory();
return new ElfKingdomFactory(); case ORC -> new OrcKingdomFactory();
case ORC: default -> throw new IllegalArgumentException("KingdomType not supported.");
return new OrcKingdomFactory(); };
default:
throw new IllegalArgumentException("KingdomType not supported.");
}
} }
} }
} }
+5 -6
View File
@@ -245,16 +245,15 @@ public class DefaultCircuitBreaker implements CircuitBreaker {
public void setState(State state) { public void setState(State state) {
this.state = state; this.state = state;
switch (state) { switch (state) {
case OPEN: case OPEN -> {
this.failureCount = failureThreshold; this.failureCount = failureThreshold;
this.lastFailureTime = System.nanoTime(); this.lastFailureTime = System.nanoTime();
break; }
case HALF_OPEN: case HALF_OPEN -> {
this.failureCount = failureThreshold; this.failureCount = failureThreshold;
this.lastFailureTime = System.nanoTime() - retryTimePeriod; this.lastFailureTime = System.nanoTime() - retryTimePeriod;
break; }
default: default -> this.failureCount = 0;
this.failureCount = 0;
} }
} }
@@ -113,16 +113,15 @@ public class DefaultCircuitBreaker implements CircuitBreaker {
public void setState(State state) { public void setState(State state) {
this.state = state; this.state = state;
switch (state) { switch (state) {
case OPEN: case OPEN -> {
this.failureCount = failureThreshold; this.failureCount = failureThreshold;
this.lastFailureTime = System.nanoTime(); this.lastFailureTime = System.nanoTime();
break; }
case HALF_OPEN: case HALF_OPEN -> {
this.failureCount = failureThreshold; this.failureCount = failureThreshold;
this.lastFailureTime = System.nanoTime() - retryTimePeriod; this.lastFailureTime = System.nanoTime() - retryTimePeriod;
break; }
default: default -> this.failureCount = 0;
this.failureCount = 0;
} }
} }
@@ -21,18 +21,18 @@ public class PlayerInputComponent implements InputComponent {
@Override @Override
public void update(GameObject gameObject, int e) { public void update(GameObject gameObject, int e) {
switch (e) { switch (e) {
case KeyEvent.KEY_LOCATION_LEFT: case KeyEvent.KEY_LOCATION_LEFT -> {
gameObject.updateVelocity(-WALK_ACCELERATION); gameObject.updateVelocity(-WALK_ACCELERATION);
LOGGER.info(gameObject.getName() + " has moved left."); LOGGER.info(gameObject.getName() + " has moved left.");
break; }
case KeyEvent.KEY_LOCATION_RIGHT: case KeyEvent.KEY_LOCATION_RIGHT -> {
gameObject.updateVelocity(WALK_ACCELERATION); gameObject.updateVelocity(WALK_ACCELERATION);
LOGGER.info(gameObject.getName() + " has moved right."); LOGGER.info(gameObject.getName() + " has moved right.");
break; }
default: default -> {
LOGGER.info(gameObject.getName() + "'s velocity is unchanged due to the invalid input"); LOGGER.info(gameObject.getName() + "'s velocity is unchanged due to the invalid input");
gameObject.updateVelocity(0); gameObject.updateVelocity(0);
break; // incorrect input } // incorrect input
} }
} }
} }
+7 -18
View File
@@ -36,6 +36,7 @@ Let's take our goldmine example from above. Here we have the dwarven mine worker
there's a base class `DwarvenMineWorker`: there's a base class `DwarvenMineWorker`:
```java ```java
@Slf4j @Slf4j
public abstract class DwarvenMineWorker { public abstract class DwarvenMineWorker {
@@ -57,24 +58,12 @@ public abstract class DwarvenMineWorker {
private void action(Action action) { private void action(Action action) {
switch (action) { switch (action) {
case GO_TO_SLEEP: case GO_TO_SLEEP -> goToSleep();
goToSleep(); case WAKE_UP -> wakeUp();
break; case GO_HOME -> goHome();
case WAKE_UP: case GO_TO_MINE -> goToMine();
wakeUp(); case WORK -> work();
break; default -> LOGGER.info("Undefined action");
case GO_HOME:
goHome();
break;
case GO_TO_MINE:
goToMine();
break;
case WORK:
work();
break;
default:
LOGGER.info("Undefined action");
break;
} }
} }
@@ -51,24 +51,12 @@ public abstract class DwarvenMineWorker {
private void action(Action action) { private void action(Action action) {
switch (action) { switch (action) {
case GO_TO_SLEEP: case GO_TO_SLEEP -> goToSleep();
goToSleep(); case WAKE_UP -> wakeUp();
break; case GO_HOME -> goHome();
case WAKE_UP: case GO_TO_MINE -> goToMine();
wakeUp(); case WORK -> work();
break; default -> LOGGER.info("Undefined action");
case GO_HOME:
goHome();
break;
case GO_TO_MINE:
goToMine();
break;
case WORK:
work();
break;
default:
LOGGER.info("Undefined action");
break;
} }
} }
+8 -8
View File
@@ -80,20 +80,20 @@ public class PotionFactory {
var potion = potions.get(type); var potion = potions.get(type);
if (potion == null) { if (potion == null) {
switch (type) { switch (type) {
case HEALING: case HEALING -> {
potion = new HealingPotion(); potion = new HealingPotion();
potions.put(type, potion); potions.put(type, potion);
break; }
case HOLY_WATER: case HOLY_WATER -> {
potion = new HolyWaterPotion(); potion = new HolyWaterPotion();
potions.put(type, potion); potions.put(type, potion);
break; }
case INVISIBILITY: case INVISIBILITY -> {
potion = new InvisibilityPotion(); potion = new InvisibilityPotion();
potions.put(type, potion); potions.put(type, potion);
break; }
default: default -> {
break; }
} }
} }
return potion; return potion;
@@ -44,23 +44,13 @@ public class PotionFactory {
var potion = potions.get(type); var potion = potions.get(type);
if (potion == null) { if (potion == null) {
switch (type) { switch (type) {
case HEALING: case HEALING -> potion = new HealingPotion();
potion = new HealingPotion(); case HOLY_WATER -> potion = new HolyWaterPotion();
break; case INVISIBILITY -> potion = new InvisibilityPotion();
case HOLY_WATER: case POISON -> potion = new PoisonPotion();
potion = new HolyWaterPotion(); case STRENGTH -> potion = new StrengthPotion();
break; default -> {
case INVISIBILITY: }
potion = new InvisibilityPotion();
break;
case POISON:
potion = new PoisonPotion();
break;
case STRENGTH:
potion = new StrengthPotion();
break;
default:
break;
} }
if (potion != null) { if (potion != null) {
potions.put(type, potion); potions.put(type, potion);
@@ -101,13 +101,10 @@ public class App {
* @return expression * @return expression
*/ */
public static Expression getOperatorInstance(String s, Expression left, Expression right) { public static Expression getOperatorInstance(String s, Expression left, Expression right) {
switch (s) { return switch (s) {
case "+": case "+" -> new PlusExpression(left, right);
return new PlusExpression(left, right); case "-" -> new MinusExpression(left, right);
case "-": default -> new MultiplyExpression(left, right);
return new MinusExpression(left, right); };
default:
return new MultiplyExpression(left, right);
}
} }
} }
@@ -105,32 +105,32 @@ public abstract class AbstractInstance implements Instance, Runnable {
*/ */
private void processMessage(Message message) { private void processMessage(Message message) {
switch (message.getType()) { switch (message.getType()) {
case ELECTION: case ELECTION -> {
LOGGER.info(INSTANCE + localId + " - Election Message handling..."); LOGGER.info(INSTANCE + localId + " - Election Message handling...");
handleElectionMessage(message); handleElectionMessage(message);
break; }
case LEADER: case LEADER -> {
LOGGER.info(INSTANCE + localId + " - Leader Message handling..."); LOGGER.info(INSTANCE + localId + " - Leader Message handling...");
handleLeaderMessage(message); handleLeaderMessage(message);
break; }
case HEARTBEAT: case HEARTBEAT -> {
LOGGER.info(INSTANCE + localId + " - Heartbeat Message handling..."); LOGGER.info(INSTANCE + localId + " - Heartbeat Message handling...");
handleHeartbeatMessage(message); handleHeartbeatMessage(message);
break; }
case ELECTION_INVOKE: case ELECTION_INVOKE -> {
LOGGER.info(INSTANCE + localId + " - Election Invoke Message handling..."); LOGGER.info(INSTANCE + localId + " - Election Invoke Message handling...");
handleElectionInvokeMessage(); handleElectionInvokeMessage();
break; }
case LEADER_INVOKE: case LEADER_INVOKE -> {
LOGGER.info(INSTANCE + localId + " - Leader Invoke Message handling..."); LOGGER.info(INSTANCE + localId + " - Leader Invoke Message handling...");
handleLeaderInvokeMessage(); handleLeaderInvokeMessage();
break; }
case HEARTBEAT_INVOKE: case HEARTBEAT_INVOKE -> {
LOGGER.info(INSTANCE + localId + " - Heartbeat Invoke Message handling..."); LOGGER.info(INSTANCE + localId + " - Heartbeat Invoke Message handling...");
handleHeartbeatInvokeMessage(); handleHeartbeatInvokeMessage();
break; }
default: default -> {
break; }
} }
} }
+8 -11
View File
@@ -155,18 +155,15 @@ public static class FactoryMaker {
} }
public static KingdomFactory makeFactory(KingdomType type) { public static KingdomFactory makeFactory(KingdomType type) {
switch (type) { return switch (type) {
case ELF: case ELF -> new ElfKingdomFactory();
return new ElfKingdomFactory(); case ORC -> new OrcKingdomFactory();
case ORC: default -> throw new IllegalArgumentException("KingdomType not supported.");
return new OrcKingdomFactory(); };
default:
throw new IllegalArgumentException("KingdomType not supported.");
}
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
var app = new App(); var app = new App();
LOGGER.info("Elf Kingdom"); LOGGER.info("Elf Kingdom");
@@ -177,8 +174,8 @@ public static void main(String[] args) {
LOGGER.info("Orc Kingdom"); LOGGER.info("Orc Kingdom");
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC)); app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC));
-- similar use of the orc factory --similar use of the orc factory
} }
``` ```
## Diagrama de clases ## Diagrama de clases
+8 -11
View File
@@ -153,18 +153,15 @@ public static class FactoryMaker {
} }
public static KingdomFactory makeFactory(KingdomType type) { public static KingdomFactory makeFactory(KingdomType type) {
switch (type) { return switch (type) {
case ELF: case ELF -> new ElfKingdomFactory();
return new ElfKingdomFactory(); case ORC -> new OrcKingdomFactory();
case ORC: default -> throw new IllegalArgumentException("KingdomType not supported.");
return new OrcKingdomFactory(); };
default:
throw new IllegalArgumentException("KingdomType not supported.");
}
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
var app = new App(); var app = new App();
LOGGER.info("Elf Kingdom"); LOGGER.info("Elf Kingdom");
@@ -175,8 +172,8 @@ public static void main(String[] args) {
LOGGER.info("Orc Kingdom"); LOGGER.info("Orc Kingdom");
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC)); app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC));
-- similar use of the orc factory --similar use of the orc factory
} }
``` ```
## क्लास डायग्राम ## क्लास डायग्राम
+8 -12
View File
@@ -145,7 +145,6 @@ Agora, podemos projetar uma fábrica para nossas diferentes fábricas do reino.
O cliente pode usar o `FactoryMaker` para criar uma fatoração concreta, que uma vez, produzirá diferentes objetos concretos (derivados de `Army`, `King`, `Castle`). O cliente pode usar o `FactoryMaker` para criar uma fatoração concreta, que uma vez, produzirá diferentes objetos concretos (derivados de `Army`, `King`, `Castle`).
Neste exemplo, também usamos um enum para parametrizar qual tipo de fábrica do reino o cliente solicitará. Neste exemplo, também usamos um enum para parametrizar qual tipo de fábrica do reino o cliente solicitará.
```java ```java
public static class FactoryMaker { public static class FactoryMaker {
@@ -154,18 +153,15 @@ public static class FactoryMaker {
} }
public static KingdomFactory makeFactory(KingdomType type) { public static KingdomFactory makeFactory(KingdomType type) {
switch (type) { return switch (type) {
case ELF: case ELF -> new ElfKingdomFactory();
return new ElfKingdomFactory(); case ORC -> new OrcKingdomFactory();
case ORC: default -> throw new IllegalArgumentException("KingdomType not supported.");
return new OrcKingdomFactory(); };
default:
throw new IllegalArgumentException("KingdomType not supported.");
}
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
var app = new App(); var app = new App();
LOGGER.info("Elf Kingdom"); LOGGER.info("Elf Kingdom");
@@ -176,8 +172,8 @@ public static void main(String[] args) {
LOGGER.info("Orc Kingdom"); LOGGER.info("Orc Kingdom");
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC)); app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC));
-- similar use of the orc factory --similar use of the orc factory
} }
``` ```
## Diagrama de classes ## Diagrama de classes
+7 -18
View File
@@ -37,6 +37,7 @@ Vamos pegar nosso exemplo da mina de ouro acima. Aqui temos uma hierarquia de an
Primeiro há uma classe base `DwarvenMineWorker`: Primeiro há uma classe base `DwarvenMineWorker`:
```java ```java
@Slf4j @Slf4j
public abstract class DwarvenMineWorker { public abstract class DwarvenMineWorker {
@@ -58,24 +59,12 @@ public abstract class DwarvenMineWorker {
private void action(Action action) { private void action(Action action) {
switch (action) { switch (action) {
case GO_TO_SLEEP: case GO_TO_SLEEP -> goToSleep();
goToSleep(); case WAKE_UP -> wakeUp();
break; case GO_HOME -> goHome();
case WAKE_UP: case GO_TO_MINE -> goToMine();
wakeUp(); case WORK -> work();
break; default -> LOGGER.info("Undefined action");
case GO_HOME:
goHome();
break;
case GO_TO_MINE:
goToMine();
break;
case WORK:
work();
break;
default:
LOGGER.info("Undefined action");
break;
} }
} }
+8 -11
View File
@@ -155,18 +155,15 @@ public static class FactoryMaker {
} }
public static KingdomFactory makeFactory(KingdomType type) { public static KingdomFactory makeFactory(KingdomType type) {
switch (type) { return switch (type) {
case ELF: case ELF -> new ElfKingdomFactory();
return new ElfKingdomFactory(); case ORC -> new OrcKingdomFactory();
case ORC: default -> throw new IllegalArgumentException("KingdomType not supported.");
return new OrcKingdomFactory(); };
default:
throw new IllegalArgumentException("KingdomType not supported.");
}
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
var app = new App(); var app = new App();
LOGGER.info("Elf Kingdom"); LOGGER.info("Elf Kingdom");
@@ -177,8 +174,8 @@ public static void main(String[] args) {
LOGGER.info("Orc Kingdom"); LOGGER.info("Orc Kingdom");
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC)); app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC));
-- Tương tự với nhà máy Orc --Tương tự với nhà máy Orc
} }
``` ```
## Sơ đồ lớp ## Sơ đồ lớp
+8 -11
View File
@@ -137,18 +137,15 @@ public static class FactoryMaker {
} }
public static KingdomFactory makeFactory(KingdomType type) { public static KingdomFactory makeFactory(KingdomType type) {
switch (type) { return switch (type) {
case ELF: case ELF -> new ElfKingdomFactory();
return new ElfKingdomFactory(); case ORC -> new OrcKingdomFactory();
case ORC: default -> throw new IllegalArgumentException("KingdomType not supported.");
return new OrcKingdomFactory(); };
default:
throw new IllegalArgumentException("KingdomType not supported.");
}
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
var app = new App(); var app = new App();
LOGGER.info("Elf Kingdom"); LOGGER.info("Elf Kingdom");
@@ -159,8 +156,8 @@ public static void main(String[] args) {
LOGGER.info("Orc Kingdom"); LOGGER.info("Orc Kingdom");
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC)); app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC));
-- similar use of the orc factory --similar use of the orc factory
} }
``` ```
## 类图 ## 类图
+5 -6
View File
@@ -234,16 +234,15 @@ public class DefaultCircuitBreaker implements CircuitBreaker {
public void setState(State state) { public void setState(State state) {
this.state = state; this.state = state;
switch (state) { switch (state) {
case OPEN: case OPEN -> {
this.failureCount = failureThreshold; this.failureCount = failureThreshold;
this.lastFailureTime = System.nanoTime(); this.lastFailureTime = System.nanoTime();
break; }
case HALF_OPEN: case HALF_OPEN -> {
this.failureCount = failureThreshold; this.failureCount = failureThreshold;
this.lastFailureTime = System.nanoTime() - retryTimePeriod; this.lastFailureTime = System.nanoTime() - retryTimePeriod;
break; }
default: default -> this.failureCount = 0;
this.failureCount = 0;
} }
} }
+7 -18
View File
@@ -29,6 +29,7 @@ tag:
使用上面金矿的例子。这里我们有矮人的矿工等级制度。 使用上面金矿的例子。这里我们有矮人的矿工等级制度。
```java ```java
@Slf4j @Slf4j
public abstract class DwarvenMineWorker { public abstract class DwarvenMineWorker {
@@ -50,24 +51,12 @@ public abstract class DwarvenMineWorker {
private void action(Action action) { private void action(Action action) {
switch (action) { switch (action) {
case GO_TO_SLEEP: case GO_TO_SLEEP -> goToSleep();
goToSleep(); case WAKE_UP -> wakeUp();
break; case GO_HOME -> goHome();
case WAKE_UP: case GO_TO_MINE -> goToMine();
wakeUp(); case WORK -> work();
break; default -> LOGGER.info("Undefined action");
case GO_HOME:
goHome();
break;
case GO_TO_MINE:
goToMine();
break;
case WORK:
work();
break;
default:
LOGGER.info("Undefined action");
break;
} }
} }
+8 -16
View File
@@ -70,24 +70,16 @@ public class Star {
ageYears *= 2; ageYears *= 2;
massTons *= 8; massTons *= 8;
switch (type) { switch (type) {
case RED_GIANT: case RED_GIANT -> type = StarType.WHITE_DWARF;
type = StarType.WHITE_DWARF; case SUN -> type = StarType.RED_GIANT;
break; case SUPERNOVA -> type = StarType.DEAD;
case SUN: case WHITE_DWARF -> type = StarType.SUPERNOVA;
type = StarType.RED_GIANT; case DEAD -> {
break;
case SUPERNOVA:
type = StarType.DEAD;
break;
case WHITE_DWARF:
type = StarType.SUPERNOVA;
break;
case DEAD:
ageYears *= 2; ageYears *= 2;
massTons = 0; massTons = 0;
break; }
default: default -> {
break; }
} }
} }
@@ -49,24 +49,16 @@ public class Star {
ageYears *= 2; ageYears *= 2;
massTons *= 8; massTons *= 8;
switch (type) { switch (type) {
case RED_GIANT: case RED_GIANT -> type = StarType.WHITE_DWARF;
type = StarType.WHITE_DWARF; case SUN -> type = StarType.RED_GIANT;
break; case SUPERNOVA -> type = StarType.DEAD;
case SUN: case WHITE_DWARF -> type = StarType.SUPERNOVA;
type = StarType.RED_GIANT; case DEAD -> {
break;
case SUPERNOVA:
type = StarType.DEAD;
break;
case WHITE_DWARF:
type = StarType.SUPERNOVA;
break;
case DEAD:
ageYears *= 2; ageYears *= 2;
massTons = 0; massTons = 0;
break; }
default: default -> {
break; }
} }
} }
@@ -26,30 +26,17 @@ public final class CalculatorViewModel {
*/ */
void handleAction(final CalculatorAction action) { void handleAction(final CalculatorAction action) {
switch (action.tag()) { switch (action.tag()) {
case AdditionCalculatorAction.TAG: case AdditionCalculatorAction.TAG -> add();
add(); case SubtractionCalculatorAction.TAG -> subtract();
break; case MultiplicationCalculatorAction.TAG -> multiply();
case DivisionCalculatorAction.TAG -> divide();
case SubtractionCalculatorAction.TAG: case SetVariableCalculatorAction.TAG -> {
subtract();
break;
case MultiplicationCalculatorAction.TAG:
multiply();
break;
case DivisionCalculatorAction.TAG:
divide();
break;
case SetVariableCalculatorAction.TAG:
SetVariableCalculatorAction setVariableAction = SetVariableCalculatorAction setVariableAction =
(SetVariableCalculatorAction) action; (SetVariableCalculatorAction) action;
setVariable(setVariableAction.getVariable()); setVariable(setVariableAction.getVariable());
break; }
default -> {
default: }
break;
} }
} }
@@ -45,16 +45,12 @@ public class RangeShardManager extends ShardManager {
@Override @Override
protected int allocateShard(Data data) { protected int allocateShard(Data data) {
var type = data.getType(); var type = data.getType();
switch (type) { return switch (type) {
case TYPE_1: case TYPE_1 -> 1;
return 1; case TYPE_2 -> 2;
case TYPE_2: case TYPE_3 -> 3;
return 2; default -> -1;
case TYPE_3: };
return 3;
default:
return -1;
}
} }
} }