Resolves checkstyle errors for business-delegate, bytecode, caching (#1059)
* Reduces checkstyle errors in business-delegate * Reduces checkstyle errors in bytecode * Reduces checkstyle errors in caching
This commit is contained in:
committed by
Ilkka Seppälä
parent
6d1c0b1563
commit
efc17fcc70
@ -24,57 +24,59 @@
|
||||
package com.iluwatar.bytecode;
|
||||
|
||||
import com.iluwatar.bytecode.util.InstructionConverterUtil;
|
||||
import java.util.Stack;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* The intention of Bytecode pattern is to give behavior the flexibility of data by encoding it as instructions
|
||||
* for a virtual machine.
|
||||
* An instruction set defines the low-level operations that can be performed. A series of instructions is encoded as
|
||||
* a sequence of bytes. A virtual machine executes these instructions one at a time,
|
||||
* using a stack for intermediate values. By combining instructions, complex high-level behavior can be defined.
|
||||
*
|
||||
* This pattern should be used when there is a need to define high number of behaviours and implementation engine
|
||||
* is not a good choice because
|
||||
* It is too lowe level
|
||||
* Iterating on it takes too long due to slow compile times or other tooling issues.
|
||||
* It has too much trust. If you want to ensure the behavior being defined can’t break the game,
|
||||
* you need to sandbox it from the rest of the codebase.
|
||||
* The intention of Bytecode pattern is to give behavior the flexibility of data by encoding it as
|
||||
* instructions for a virtual machine. An instruction set defines the low-level operations that can
|
||||
* be performed. A series of instructions is encoded as a sequence of bytes. A virtual machine
|
||||
* executes these instructions one at a time, using a stack for intermediate values. By combining
|
||||
* instructions, complex high-level behavior can be defined.
|
||||
*
|
||||
* <p>This pattern should be used when there is a need to define high number of behaviours and
|
||||
* implementation engine is not a good choice because It is too lowe level Iterating on it takes too
|
||||
* long due to slow compile times or other tooling issues. It has too much trust. If you want to
|
||||
* ensure the behavior being defined can’t break the game, you need to sandbox it from the rest of
|
||||
* the codebase.
|
||||
*/
|
||||
public class App {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Main app method
|
||||
* Main app method.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
VirtualMachine vm = new VirtualMachine();
|
||||
|
||||
Wizard wizard = new Wizard();
|
||||
wizard.setHealth(45);
|
||||
wizard.setAgility(7);
|
||||
wizard.setWisdom(11);
|
||||
|
||||
VirtualMachine vm = new VirtualMachine();
|
||||
vm.getWizards()[0] = wizard;
|
||||
|
||||
interpretInstruction("LITERAL 0", vm);
|
||||
interpretInstruction( "LITERAL 0", vm);
|
||||
interpretInstruction( "GET_HEALTH", vm);
|
||||
interpretInstruction( "LITERAL 0", vm);
|
||||
interpretInstruction( "GET_AGILITY", vm);
|
||||
interpretInstruction( "LITERAL 0", vm);
|
||||
interpretInstruction( "GET_WISDOM ", vm);
|
||||
interpretInstruction( "ADD", vm);
|
||||
interpretInstruction( "LITERAL 2", vm);
|
||||
interpretInstruction( "DIVIDE", vm);
|
||||
interpretInstruction( "ADD", vm);
|
||||
interpretInstruction( "SET_HEALTH", vm);
|
||||
interpretInstruction("LITERAL 0", vm);
|
||||
interpretInstruction("GET_HEALTH", vm);
|
||||
interpretInstruction("LITERAL 0", vm);
|
||||
interpretInstruction("GET_AGILITY", vm);
|
||||
interpretInstruction("LITERAL 0", vm);
|
||||
interpretInstruction("GET_WISDOM ", vm);
|
||||
interpretInstruction("ADD", vm);
|
||||
interpretInstruction("LITERAL 2", vm);
|
||||
interpretInstruction("DIVIDE", vm);
|
||||
interpretInstruction("ADD", vm);
|
||||
interpretInstruction("SET_HEALTH", vm);
|
||||
}
|
||||
|
||||
private static void interpretInstruction(String instruction, VirtualMachine vm) {
|
||||
InstructionConverterUtil converter = new InstructionConverterUtil();
|
||||
vm.execute(converter.convertToByteCode(instruction));
|
||||
LOGGER.info(instruction + String.format("%" + (12 - instruction.length()) + "s", "" ) + vm.getStack());
|
||||
Stack<Integer> stack = vm.getStack();
|
||||
LOGGER.info(instruction + String.format("%" + (12 - instruction.length()) + "s", "") + stack);
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@
|
||||
package com.iluwatar.bytecode;
|
||||
|
||||
/**
|
||||
* Representation of instructions understandable by virtual machine
|
||||
* Representation of instructions understandable by virtual machine.
|
||||
*/
|
||||
public enum Instruction {
|
||||
|
||||
@ -51,7 +51,8 @@ public enum Instruction {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts integer value to Instruction
|
||||
* Converts integer value to Instruction.
|
||||
*
|
||||
* @param value value of instruction
|
||||
* @return representation of the instruction
|
||||
*/
|
||||
|
@ -26,7 +26,7 @@ package com.iluwatar.bytecode;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Implementation of virtual machine
|
||||
* Implementation of virtual machine.
|
||||
*/
|
||||
public class VirtualMachine {
|
||||
|
||||
@ -35,7 +35,7 @@ public class VirtualMachine {
|
||||
private Wizard[] wizards = new Wizard[2];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public VirtualMachine() {
|
||||
for (int i = 0; i < wizards.length; i++) {
|
||||
@ -44,7 +44,8 @@ public class VirtualMachine {
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes provided bytecode
|
||||
* Executes provided bytecode.
|
||||
*
|
||||
* @param bytecode to execute
|
||||
*/
|
||||
public void execute(int[] bytecode) {
|
||||
|
@ -27,7 +27,8 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* This class represent game objects which properties can be changed by instructions interpreted by virtual machine
|
||||
* This class represent game objects which properties can be changed by instructions interpreted by
|
||||
* virtual machine.
|
||||
*/
|
||||
public class Wizard {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Wizard.class);
|
||||
|
@ -26,11 +26,11 @@ package com.iluwatar.bytecode.util;
|
||||
import com.iluwatar.bytecode.Instruction;
|
||||
|
||||
/**
|
||||
* Utility class used for instruction validation and conversion
|
||||
* Utility class used for instruction validation and conversion.
|
||||
*/
|
||||
public class InstructionConverterUtil {
|
||||
/**
|
||||
* Converts instructions represented as String
|
||||
* Converts instructions represented as String.
|
||||
*
|
||||
* @param instructions to convert
|
||||
* @return array of int representing bytecode
|
||||
@ -48,7 +48,8 @@ public class InstructionConverterUtil {
|
||||
} else if (isValidInt(splitedInstructions[i])) {
|
||||
bytecode[i] = Integer.valueOf(splitedInstructions[i]);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid instruction or number: " + splitedInstructions[i]);
|
||||
String errorMessage = "Invalid instruction or number: " + splitedInstructions[i];
|
||||
throw new IllegalArgumentException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user