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
@@ -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);
}
}
@@ -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];
}
@@ -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:
@@ -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);
}
}