Java 11 migration: patterns (remaining b-c) (#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
This commit is contained in:
Anurag Agarwal
2019-11-13 01:26:46 +05:30
committed by Ilkka Seppälä
parent 6ef840f3cf
commit 33ea7335b1
63 changed files with 798 additions and 979 deletions

View File

@ -24,7 +24,6 @@
package com.iluwatar.bytecode;
import com.iluwatar.bytecode.util.InstructionConverterUtil;
import java.util.Stack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -51,12 +50,12 @@ public class App {
*/
public static void main(String[] args) {
Wizard wizard = new Wizard();
var wizard = new Wizard();
wizard.setHealth(45);
wizard.setAgility(7);
wizard.setWisdom(11);
VirtualMachine vm = new VirtualMachine();
var vm = new VirtualMachine();
vm.getWizards()[0] = wizard;
interpretInstruction("LITERAL 0", vm);
@ -74,9 +73,8 @@ public class App {
}
private static void interpretInstruction(String instruction, VirtualMachine vm) {
InstructionConverterUtil converter = new InstructionConverterUtil();
vm.execute(converter.convertToByteCode(instruction));
Stack<Integer> stack = vm.getStack();
vm.execute(InstructionConverterUtil.convertToByteCode(instruction));
var stack = vm.getStack();
LOGGER.info(instruction + String.format("%" + (12 - instruction.length()) + "s", "") + stack);
}
}

View File

@ -40,7 +40,7 @@ public enum Instruction {
ADD(10),
DIVIDE(11);
private int value;
private final int value;
Instruction(int value) {
this.value = value;
@ -57,7 +57,7 @@ public enum Instruction {
* @return representation of the instruction
*/
public static Instruction getInstruction(int value) {
for (int i = 0; i < Instruction.values().length; i++) {
for (var i = 0; i < Instruction.values().length; i++) {
if (Instruction.values()[i].getIntValue() == value) {
return Instruction.values()[i];
}

View File

@ -30,7 +30,7 @@ import java.util.Stack;
*/
public class VirtualMachine {
private Stack<Integer> stack = new Stack();
private Stack<Integer> stack = new Stack<>();
private Wizard[] wizards = new Wizard[2];
@ -38,7 +38,7 @@ public class VirtualMachine {
* Constructor.
*/
public VirtualMachine() {
for (int i = 0; i < wizards.length; i++) {
for (var i = 0; i < wizards.length; i++) {
wizards[i] = new Wizard();
}
}
@ -49,10 +49,8 @@ public class VirtualMachine {
* @param bytecode to execute
*/
public void execute(int[] bytecode) {
for (int i = 0; i < bytecode.length; i++) {
for (var i = 0; i < bytecode.length; i++) {
Instruction instruction = Instruction.getInstruction(bytecode[i]);
int wizard;
int amount;
switch (instruction) {
case LITERAL:
// Read the next byte from the bytecode.
@ -60,8 +58,8 @@ public class VirtualMachine {
stack.push(value);
break;
case SET_AGILITY:
amount = stack.pop();
wizard = stack.pop();
var amount = stack.pop();
var wizard = stack.pop();
setAgility(wizard, amount);
break;
case SET_WISDOM:
@ -87,8 +85,8 @@ public class VirtualMachine {
stack.push(getWisdom(wizard));
break;
case ADD:
int a = stack.pop();
int b = stack.pop();
var a = stack.pop();
var b = stack.pop();
stack.push(a + b);
break;
case DIVIDE:

View File

@ -40,15 +40,15 @@ public class InstructionConverterUtil {
return new int[0];
}
String[] splitedInstructions = instructions.trim().split(" ");
int[] bytecode = new int[splitedInstructions.length];
for (int i = 0; i < splitedInstructions.length; i++) {
var splitedInstructions = instructions.trim().split(" ");
var bytecode = new int[splitedInstructions.length];
for (var i = 0; i < splitedInstructions.length; i++) {
if (isValidInstruction(splitedInstructions[i])) {
bytecode[i] = Instruction.valueOf(splitedInstructions[i]).getIntValue();
} else if (isValidInt(splitedInstructions[i])) {
bytecode[i] = Integer.valueOf(splitedInstructions[i]);
bytecode[i] = Integer.parseInt(splitedInstructions[i]);
} else {
String errorMessage = "Invalid instruction or number: " + splitedInstructions[i];
var errorMessage = "Invalid instruction or number: " + splitedInstructions[i];
throw new IllegalArgumentException(errorMessage);
}
}

View File

@ -32,7 +32,6 @@ public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
App.main(new String[]{});
}
}

View File

@ -36,11 +36,11 @@ public class VirtualMachineTest {
@Test
public void testLiteral() {
int[] bytecode = new int[2];
var bytecode = new int[2];
bytecode[0] = LITERAL.getIntValue();
bytecode[1] = 10;
VirtualMachine vm = new VirtualMachine();
var vm = new VirtualMachine();
vm.execute(bytecode);
assertEquals(1, vm.getStack().size());
@ -49,15 +49,15 @@ public class VirtualMachineTest {
@Test
public void testSetHealth() {
int wizardNumber = 0;
int[] bytecode = new int[5];
var wizardNumber = 0;
var bytecode = new int[5];
bytecode[0] = LITERAL.getIntValue();
bytecode[1] = wizardNumber;
bytecode[2] = LITERAL.getIntValue();
bytecode[3] = 50; // health amount
bytecode[4] = SET_HEALTH.getIntValue();
VirtualMachine vm = new VirtualMachine();
var vm = new VirtualMachine();
vm.execute(bytecode);
assertEquals(50, vm.getWizards()[wizardNumber].getHealth());
@ -65,15 +65,15 @@ public class VirtualMachineTest {
@Test
public void testSetAgility() {
int wizardNumber = 0;
int[] bytecode = new int[5];
var wizardNumber = 0;
var bytecode = new int[5];
bytecode[0] = LITERAL.getIntValue();
bytecode[1] = wizardNumber;
bytecode[2] = LITERAL.getIntValue();
bytecode[3] = 50; // agility amount
bytecode[4] = SET_AGILITY.getIntValue();
VirtualMachine vm = new VirtualMachine();
var vm = new VirtualMachine();
vm.execute(bytecode);
assertEquals(50, vm.getWizards()[wizardNumber].getAgility());
@ -81,15 +81,15 @@ public class VirtualMachineTest {
@Test
public void testSetWisdom() {
int wizardNumber = 0;
int[] bytecode = new int[5];
var wizardNumber = 0;
var bytecode = new int[5];
bytecode[0] = LITERAL.getIntValue();
bytecode[1] = wizardNumber;
bytecode[2] = LITERAL.getIntValue();
bytecode[3] = 50; // wisdom amount
bytecode[4] = SET_WISDOM.getIntValue();
VirtualMachine vm = new VirtualMachine();
var vm = new VirtualMachine();
vm.execute(bytecode);
assertEquals(50, vm.getWizards()[wizardNumber].getWisdom());
@ -97,8 +97,8 @@ public class VirtualMachineTest {
@Test
public void testGetHealth() {
int wizardNumber = 0;
int[] bytecode = new int[8];
var wizardNumber = 0;
var bytecode = new int[8];
bytecode[0] = LITERAL.getIntValue();
bytecode[1] = wizardNumber;
bytecode[2] = LITERAL.getIntValue();
@ -108,7 +108,7 @@ public class VirtualMachineTest {
bytecode[6] = wizardNumber;
bytecode[7] = GET_HEALTH.getIntValue();
VirtualMachine vm = new VirtualMachine();
var vm = new VirtualMachine();
vm.execute(bytecode);
assertEquals(Integer.valueOf(50), vm.getStack().pop());
@ -116,13 +116,13 @@ public class VirtualMachineTest {
@Test
public void testPlaySound() {
int wizardNumber = 0;
int[] bytecode = new int[3];
var wizardNumber = 0;
var bytecode = new int[3];
bytecode[0] = LITERAL.getIntValue();
bytecode[1] = wizardNumber;
bytecode[2] = PLAY_SOUND.getIntValue();
VirtualMachine vm = new VirtualMachine();
var vm = new VirtualMachine();
vm.execute(bytecode);
assertEquals(0, vm.getStack().size());
@ -131,13 +131,13 @@ public class VirtualMachineTest {
@Test
public void testSpawnParticles() {
int wizardNumber = 0;
int[] bytecode = new int[3];
var wizardNumber = 0;
var bytecode = new int[3];
bytecode[0] = LITERAL.getIntValue();
bytecode[1] = wizardNumber;
bytecode[2] = SPAWN_PARTICLES.getIntValue();
VirtualMachine vm = new VirtualMachine();
var vm = new VirtualMachine();
vm.execute(bytecode);
assertEquals(0, vm.getStack().size());
@ -146,9 +146,9 @@ public class VirtualMachineTest {
@Test
public void testInvalidInstruction() {
int[] bytecode = new int[1];
var bytecode = new int[1];
bytecode[0] = 999;
VirtualMachine vm = new VirtualMachine();
var vm = new VirtualMachine();
assertThrows(IllegalArgumentException.class, () -> vm.execute(bytecode));
}

View File

@ -34,19 +34,19 @@ import org.junit.jupiter.api.Test;
public class InstructionConverterUtilTest {
@Test
public void testEmptyInstruction() {
String instruction = "";
var instruction = "";
int[] bytecode = InstructionConverterUtil.convertToByteCode(instruction);
var bytecode = InstructionConverterUtil.convertToByteCode(instruction);
Assertions.assertEquals(0, bytecode.length);
}
@Test
public void testInstructions() {
String instructions =
"LITERAL 35 SET_HEALTH SET_WISDOM SET_AGILITY PLAY_SOUND SPAWN_PARTICLES GET_HEALTH ADD DIVIDE";
var instructions = "LITERAL 35 SET_HEALTH SET_WISDOM SET_AGILITY PLAY_SOUND"
+ " SPAWN_PARTICLES GET_HEALTH ADD DIVIDE";
int[] bytecode = InstructionConverterUtil.convertToByteCode(instructions);
var bytecode = InstructionConverterUtil.convertToByteCode(instructions);
Assertions.assertEquals(10, bytecode.length);
Assertions.assertEquals(Instruction.LITERAL.getIntValue(), bytecode[0]);