Skip to content

Commit 33ea733

Browse files
anuragagarwal561994iluwatar
authored andcommitted
Java 11 migration: patterns (remaining b-c) (iluwatar#1081)
* Moves business-delegate pattern to java 11 * Moves bytecode pattern to java 11 * Moves caching pattern to java 11 * Moves callback pattern to java 11 * Moves chain pattern to java 11 * Moves circuit-breaker pattern to java 11 * Moves collection-pipeline pattern to java 11 * Moves command pattern to java 11 * Moves commander pattern to java 11 * Moves composite pattern to java 11 * Corrects test cases
1 parent 6ef840f commit 33ea733

File tree

63 files changed

+794
-975
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+794
-975
lines changed

business-delegate/src/main/java/com/iluwatar/business/delegate/App.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ public class App {
4646
*/
4747
public static void main(String[] args) {
4848

49-
BusinessDelegate businessDelegate = new BusinessDelegate();
50-
BusinessLookup businessLookup = new BusinessLookup();
49+
var businessDelegate = new BusinessDelegate();
50+
var businessLookup = new BusinessLookup();
5151
businessLookup.setEjbService(new EjbService());
5252
businessLookup.setJmsService(new JmsService());
5353

5454
businessDelegate.setLookupService(businessLookup);
5555
businessDelegate.setServiceType(ServiceType.EJB);
5656

57-
Client client = new Client(businessDelegate);
57+
var client = new Client(businessDelegate);
5858
client.doTask();
5959

6060
businessDelegate.setServiceType(ServiceType.JMS);

business-delegate/src/test/java/com/iluwatar/business/delegate/BusinessDelegateTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void setup() {
7676
public void testBusinessDelegate() {
7777

7878
// setup a client object
79-
Client client = new Client(businessDelegate);
79+
var client = new Client(businessDelegate);
8080

8181
// set the service type
8282
businessDelegate.setServiceType(ServiceType.EJB);

bytecode/src/main/java/com/iluwatar/bytecode/App.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package com.iluwatar.bytecode;
2525

2626
import com.iluwatar.bytecode.util.InstructionConverterUtil;
27-
import java.util.Stack;
2827
import org.slf4j.Logger;
2928
import org.slf4j.LoggerFactory;
3029

@@ -51,12 +50,12 @@ public class App {
5150
*/
5251
public static void main(String[] args) {
5352

54-
Wizard wizard = new Wizard();
53+
var wizard = new Wizard();
5554
wizard.setHealth(45);
5655
wizard.setAgility(7);
5756
wizard.setWisdom(11);
5857

59-
VirtualMachine vm = new VirtualMachine();
58+
var vm = new VirtualMachine();
6059
vm.getWizards()[0] = wizard;
6160

6261
interpretInstruction("LITERAL 0", vm);
@@ -74,9 +73,8 @@ public static void main(String[] args) {
7473
}
7574

7675
private static void interpretInstruction(String instruction, VirtualMachine vm) {
77-
InstructionConverterUtil converter = new InstructionConverterUtil();
78-
vm.execute(converter.convertToByteCode(instruction));
79-
Stack<Integer> stack = vm.getStack();
76+
vm.execute(InstructionConverterUtil.convertToByteCode(instruction));
77+
var stack = vm.getStack();
8078
LOGGER.info(instruction + String.format("%" + (12 - instruction.length()) + "s", "") + stack);
8179
}
8280
}

bytecode/src/main/java/com/iluwatar/bytecode/Instruction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public enum Instruction {
4040
ADD(10),
4141
DIVIDE(11);
4242

43-
private int value;
43+
private final int value;
4444

4545
Instruction(int value) {
4646
this.value = value;
@@ -57,7 +57,7 @@ public int getIntValue() {
5757
* @return representation of the instruction
5858
*/
5959
public static Instruction getInstruction(int value) {
60-
for (int i = 0; i < Instruction.values().length; i++) {
60+
for (var i = 0; i < Instruction.values().length; i++) {
6161
if (Instruction.values()[i].getIntValue() == value) {
6262
return Instruction.values()[i];
6363
}

bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
*/
3131
public class VirtualMachine {
3232

33-
private Stack<Integer> stack = new Stack();
33+
private Stack<Integer> stack = new Stack<>();
3434

3535
private Wizard[] wizards = new Wizard[2];
3636

3737
/**
3838
* Constructor.
3939
*/
4040
public VirtualMachine() {
41-
for (int i = 0; i < wizards.length; i++) {
41+
for (var i = 0; i < wizards.length; i++) {
4242
wizards[i] = new Wizard();
4343
}
4444
}
@@ -49,19 +49,17 @@ public VirtualMachine() {
4949
* @param bytecode to execute
5050
*/
5151
public void execute(int[] bytecode) {
52-
for (int i = 0; i < bytecode.length; i++) {
52+
for (var i = 0; i < bytecode.length; i++) {
5353
Instruction instruction = Instruction.getInstruction(bytecode[i]);
54-
int wizard;
55-
int amount;
5654
switch (instruction) {
5755
case LITERAL:
5856
// Read the next byte from the bytecode.
5957
int value = bytecode[++i];
6058
stack.push(value);
6159
break;
6260
case SET_AGILITY:
63-
amount = stack.pop();
64-
wizard = stack.pop();
61+
var amount = stack.pop();
62+
var wizard = stack.pop();
6563
setAgility(wizard, amount);
6664
break;
6765
case SET_WISDOM:
@@ -87,8 +85,8 @@ public void execute(int[] bytecode) {
8785
stack.push(getWisdom(wizard));
8886
break;
8987
case ADD:
90-
int a = stack.pop();
91-
int b = stack.pop();
88+
var a = stack.pop();
89+
var b = stack.pop();
9290
stack.push(a + b);
9391
break;
9492
case DIVIDE:

bytecode/src/main/java/com/iluwatar/bytecode/util/InstructionConverterUtil.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ public static int[] convertToByteCode(String instructions) {
4040
return new int[0];
4141
}
4242

43-
String[] splitedInstructions = instructions.trim().split(" ");
44-
int[] bytecode = new int[splitedInstructions.length];
45-
for (int i = 0; i < splitedInstructions.length; i++) {
43+
var splitedInstructions = instructions.trim().split(" ");
44+
var bytecode = new int[splitedInstructions.length];
45+
for (var i = 0; i < splitedInstructions.length; i++) {
4646
if (isValidInstruction(splitedInstructions[i])) {
4747
bytecode[i] = Instruction.valueOf(splitedInstructions[i]).getIntValue();
4848
} else if (isValidInt(splitedInstructions[i])) {
49-
bytecode[i] = Integer.valueOf(splitedInstructions[i]);
49+
bytecode[i] = Integer.parseInt(splitedInstructions[i]);
5050
} else {
51-
String errorMessage = "Invalid instruction or number: " + splitedInstructions[i];
51+
var errorMessage = "Invalid instruction or number: " + splitedInstructions[i];
5252
throw new IllegalArgumentException(errorMessage);
5353
}
5454
}

bytecode/src/test/java/com/iluwatar/bytecode/AppTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public class AppTest {
3232

3333
@Test
3434
public void test() {
35-
String[] args = {};
36-
App.main(args);
35+
App.main(new String[]{});
3736
}
3837
}

bytecode/src/test/java/com/iluwatar/bytecode/VirtualMachineTest.java

+22-22
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public class VirtualMachineTest {
3636

3737
@Test
3838
public void testLiteral() {
39-
int[] bytecode = new int[2];
39+
var bytecode = new int[2];
4040
bytecode[0] = LITERAL.getIntValue();
4141
bytecode[1] = 10;
4242

43-
VirtualMachine vm = new VirtualMachine();
43+
var vm = new VirtualMachine();
4444
vm.execute(bytecode);
4545

4646
assertEquals(1, vm.getStack().size());
@@ -49,56 +49,56 @@ public void testLiteral() {
4949

5050
@Test
5151
public void testSetHealth() {
52-
int wizardNumber = 0;
53-
int[] bytecode = new int[5];
52+
var wizardNumber = 0;
53+
var bytecode = new int[5];
5454
bytecode[0] = LITERAL.getIntValue();
5555
bytecode[1] = wizardNumber;
5656
bytecode[2] = LITERAL.getIntValue();
5757
bytecode[3] = 50; // health amount
5858
bytecode[4] = SET_HEALTH.getIntValue();
5959

60-
VirtualMachine vm = new VirtualMachine();
60+
var vm = new VirtualMachine();
6161
vm.execute(bytecode);
6262

6363
assertEquals(50, vm.getWizards()[wizardNumber].getHealth());
6464
}
6565

6666
@Test
6767
public void testSetAgility() {
68-
int wizardNumber = 0;
69-
int[] bytecode = new int[5];
68+
var wizardNumber = 0;
69+
var bytecode = new int[5];
7070
bytecode[0] = LITERAL.getIntValue();
7171
bytecode[1] = wizardNumber;
7272
bytecode[2] = LITERAL.getIntValue();
7373
bytecode[3] = 50; // agility amount
7474
bytecode[4] = SET_AGILITY.getIntValue();
7575

76-
VirtualMachine vm = new VirtualMachine();
76+
var vm = new VirtualMachine();
7777
vm.execute(bytecode);
7878

7979
assertEquals(50, vm.getWizards()[wizardNumber].getAgility());
8080
}
8181

8282
@Test
8383
public void testSetWisdom() {
84-
int wizardNumber = 0;
85-
int[] bytecode = new int[5];
84+
var wizardNumber = 0;
85+
var bytecode = new int[5];
8686
bytecode[0] = LITERAL.getIntValue();
8787
bytecode[1] = wizardNumber;
8888
bytecode[2] = LITERAL.getIntValue();
8989
bytecode[3] = 50; // wisdom amount
9090
bytecode[4] = SET_WISDOM.getIntValue();
9191

92-
VirtualMachine vm = new VirtualMachine();
92+
var vm = new VirtualMachine();
9393
vm.execute(bytecode);
9494

9595
assertEquals(50, vm.getWizards()[wizardNumber].getWisdom());
9696
}
9797

9898
@Test
9999
public void testGetHealth() {
100-
int wizardNumber = 0;
101-
int[] bytecode = new int[8];
100+
var wizardNumber = 0;
101+
var bytecode = new int[8];
102102
bytecode[0] = LITERAL.getIntValue();
103103
bytecode[1] = wizardNumber;
104104
bytecode[2] = LITERAL.getIntValue();
@@ -108,21 +108,21 @@ public void testGetHealth() {
108108
bytecode[6] = wizardNumber;
109109
bytecode[7] = GET_HEALTH.getIntValue();
110110

111-
VirtualMachine vm = new VirtualMachine();
111+
var vm = new VirtualMachine();
112112
vm.execute(bytecode);
113113

114114
assertEquals(Integer.valueOf(50), vm.getStack().pop());
115115
}
116116

117117
@Test
118118
public void testPlaySound() {
119-
int wizardNumber = 0;
120-
int[] bytecode = new int[3];
119+
var wizardNumber = 0;
120+
var bytecode = new int[3];
121121
bytecode[0] = LITERAL.getIntValue();
122122
bytecode[1] = wizardNumber;
123123
bytecode[2] = PLAY_SOUND.getIntValue();
124124

125-
VirtualMachine vm = new VirtualMachine();
125+
var vm = new VirtualMachine();
126126
vm.execute(bytecode);
127127

128128
assertEquals(0, vm.getStack().size());
@@ -131,13 +131,13 @@ public void testPlaySound() {
131131

132132
@Test
133133
public void testSpawnParticles() {
134-
int wizardNumber = 0;
135-
int[] bytecode = new int[3];
134+
var wizardNumber = 0;
135+
var bytecode = new int[3];
136136
bytecode[0] = LITERAL.getIntValue();
137137
bytecode[1] = wizardNumber;
138138
bytecode[2] = SPAWN_PARTICLES.getIntValue();
139139

140-
VirtualMachine vm = new VirtualMachine();
140+
var vm = new VirtualMachine();
141141
vm.execute(bytecode);
142142

143143
assertEquals(0, vm.getStack().size());
@@ -146,9 +146,9 @@ public void testSpawnParticles() {
146146

147147
@Test
148148
public void testInvalidInstruction() {
149-
int[] bytecode = new int[1];
149+
var bytecode = new int[1];
150150
bytecode[0] = 999;
151-
VirtualMachine vm = new VirtualMachine();
151+
var vm = new VirtualMachine();
152152

153153
assertThrows(IllegalArgumentException.class, () -> vm.execute(bytecode));
154154
}

bytecode/src/test/java/com/iluwatar/bytecode/util/InstructionConverterUtilTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@
3434
public class InstructionConverterUtilTest {
3535
@Test
3636
public void testEmptyInstruction() {
37-
String instruction = "";
37+
var instruction = "";
3838

39-
int[] bytecode = InstructionConverterUtil.convertToByteCode(instruction);
39+
var bytecode = InstructionConverterUtil.convertToByteCode(instruction);
4040

4141
Assertions.assertEquals(0, bytecode.length);
4242
}
4343

4444
@Test
4545
public void testInstructions() {
46-
String instructions =
47-
"LITERAL 35 SET_HEALTH SET_WISDOM SET_AGILITY PLAY_SOUND SPAWN_PARTICLES GET_HEALTH ADD DIVIDE";
46+
var instructions = "LITERAL 35 SET_HEALTH SET_WISDOM SET_AGILITY PLAY_SOUND"
47+
+ " SPAWN_PARTICLES GET_HEALTH ADD DIVIDE";
4848

49-
int[] bytecode = InstructionConverterUtil.convertToByteCode(instructions);
49+
var bytecode = InstructionConverterUtil.convertToByteCode(instructions);
5050

5151
Assertions.assertEquals(10, bytecode.length);
5252
Assertions.assertEquals(Instruction.LITERAL.getIntValue(), bytecode[0]);

caching/src/main/java/com/iluwatar/caching/App.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static void main(String[] args) {
7676
// true to run the tests with MongoDB (provided that MongoDB is
7777
// installed and socket connection is open).
7878
AppManager.initCacheCapacity(3);
79-
App app = new App();
79+
var app = new App();
8080
app.useReadAndWriteThroughStrategy();
8181
app.useReadThroughAndWriteAroundStrategy();
8282
app.useReadThroughAndWriteBehindStrategy();
@@ -90,7 +90,7 @@ public void useReadAndWriteThroughStrategy() {
9090
LOGGER.info("# CachingPolicy.THROUGH");
9191
AppManager.initCachingPolicy(CachingPolicy.THROUGH);
9292

93-
UserAccount userAccount1 = new UserAccount("001", "John", "He is a boy.");
93+
var userAccount1 = new UserAccount("001", "John", "He is a boy.");
9494

9595
AppManager.save(userAccount1);
9696
LOGGER.info(AppManager.printCacheContent());
@@ -105,7 +105,7 @@ public void useReadThroughAndWriteAroundStrategy() {
105105
LOGGER.info("# CachingPolicy.AROUND");
106106
AppManager.initCachingPolicy(CachingPolicy.AROUND);
107107

108-
UserAccount userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
108+
var userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
109109

110110
AppManager.save(userAccount2);
111111
LOGGER.info(AppManager.printCacheContent());
@@ -127,9 +127,9 @@ public void useReadThroughAndWriteBehindStrategy() {
127127
LOGGER.info("# CachingPolicy.BEHIND");
128128
AppManager.initCachingPolicy(CachingPolicy.BEHIND);
129129

130-
UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
131-
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
132-
UserAccount userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
130+
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
131+
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
132+
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
133133

134134
AppManager.save(userAccount3);
135135
AppManager.save(userAccount4);
@@ -152,9 +152,9 @@ public void useCacheAsideStategy() {
152152
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
153153
LOGGER.info(AppManager.printCacheContent());
154154

155-
UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
156-
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
157-
UserAccount userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
155+
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
156+
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
157+
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
158158
AppManager.save(userAccount3);
159159
AppManager.save(userAccount4);
160160
AppManager.save(userAccount5);

0 commit comments

Comments
 (0)