📍Use lombok, reformat, and optimize the code (#1560)
* Use lombok, reformat, and optimize the code * Fix merge conflicts and some sonar issues Co-authored-by: va1m <va1m@email.com>
This commit is contained in:
@ -24,8 +24,7 @@
|
||||
package com.iluwatar.bytecode;
|
||||
|
||||
import com.iluwatar.bytecode.util.InstructionConverterUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* The intention of Bytecode pattern is to give behavior the flexibility of data by encoding it as
|
||||
@ -40,8 +39,8 @@ import org.slf4j.LoggerFactory;
|
||||
* ensure the behavior being defined can’t break the game, you need to sandbox it from the rest of
|
||||
* the codebase.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Main app method.
|
||||
|
@ -23,9 +23,14 @@
|
||||
|
||||
package com.iluwatar.bytecode;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Representation of instructions understandable by virtual machine.
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum Instruction {
|
||||
|
||||
LITERAL(1),
|
||||
@ -40,15 +45,7 @@ public enum Instruction {
|
||||
ADD(10),
|
||||
DIVIDE(11);
|
||||
|
||||
private final int value;
|
||||
|
||||
Instruction(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getIntValue() {
|
||||
return value;
|
||||
}
|
||||
private final int intValue;
|
||||
|
||||
/**
|
||||
* Converts integer value to Instruction.
|
||||
|
@ -24,10 +24,12 @@
|
||||
package com.iluwatar.bytecode;
|
||||
|
||||
import java.util.Stack;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Implementation of virtual machine.
|
||||
*/
|
||||
@Getter
|
||||
public class VirtualMachine {
|
||||
|
||||
private final Stack<Integer> stack = new Stack<>();
|
||||
@ -108,10 +110,6 @@ public class VirtualMachine {
|
||||
}
|
||||
}
|
||||
|
||||
public Stack<Integer> getStack() {
|
||||
return stack;
|
||||
}
|
||||
|
||||
public void setHealth(int wizard, int amount) {
|
||||
wizards[wizard].setHealth(amount);
|
||||
}
|
||||
@ -135,8 +133,4 @@ public class VirtualMachine {
|
||||
public int getAgility(int wizard) {
|
||||
return wizards[wizard].getAgility();
|
||||
}
|
||||
|
||||
public Wizard[] getWizards() {
|
||||
return wizards;
|
||||
}
|
||||
}
|
||||
|
@ -23,15 +23,18 @@
|
||||
|
||||
package com.iluwatar.bytecode;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* This class represent game objects which properties can be changed by instructions interpreted by
|
||||
* virtual machine.
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@Slf4j
|
||||
public class Wizard {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Wizard.class);
|
||||
|
||||
private int health;
|
||||
|
||||
@ -41,30 +44,6 @@ public class Wizard {
|
||||
private int numberOfPlayedSounds;
|
||||
private int numberOfSpawnedParticles;
|
||||
|
||||
public int getHealth() {
|
||||
return health;
|
||||
}
|
||||
|
||||
public void setHealth(int health) {
|
||||
this.health = health;
|
||||
}
|
||||
|
||||
public int getAgility() {
|
||||
return agility;
|
||||
}
|
||||
|
||||
public void setAgility(int agility) {
|
||||
this.agility = agility;
|
||||
}
|
||||
|
||||
public int getWisdom() {
|
||||
return wisdom;
|
||||
}
|
||||
|
||||
public void setWisdom(int wisdom) {
|
||||
this.wisdom = wisdom;
|
||||
}
|
||||
|
||||
public void playSound() {
|
||||
LOGGER.info("Playing sound");
|
||||
numberOfPlayedSounds++;
|
||||
@ -75,11 +54,4 @@ public class Wizard {
|
||||
numberOfSpawnedParticles++;
|
||||
}
|
||||
|
||||
public int getNumberOfPlayedSounds() {
|
||||
return numberOfPlayedSounds;
|
||||
}
|
||||
|
||||
public int getNumberOfSpawnedParticles() {
|
||||
return numberOfSpawnedParticles;
|
||||
}
|
||||
}
|
||||
|
@ -23,19 +23,19 @@
|
||||
|
||||
package com.iluwatar.bytecode;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.iluwatar.bytecode.Instruction.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test for {@Link VirtualMachine}
|
||||
* Test for {@link VirtualMachine}
|
||||
*/
|
||||
public class VirtualMachineTest {
|
||||
class VirtualMachineTest {
|
||||
|
||||
@Test
|
||||
public void testLiteral() {
|
||||
void testLiteral() {
|
||||
var bytecode = new int[2];
|
||||
bytecode[0] = LITERAL.getIntValue();
|
||||
bytecode[1] = 10;
|
||||
@ -48,7 +48,7 @@ public class VirtualMachineTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetHealth() {
|
||||
void testSetHealth() {
|
||||
var wizardNumber = 0;
|
||||
var bytecode = new int[5];
|
||||
bytecode[0] = LITERAL.getIntValue();
|
||||
@ -64,7 +64,7 @@ public class VirtualMachineTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetAgility() {
|
||||
void testSetAgility() {
|
||||
var wizardNumber = 0;
|
||||
var bytecode = new int[5];
|
||||
bytecode[0] = LITERAL.getIntValue();
|
||||
@ -80,7 +80,7 @@ public class VirtualMachineTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetWisdom() {
|
||||
void testSetWisdom() {
|
||||
var wizardNumber = 0;
|
||||
var bytecode = new int[5];
|
||||
bytecode[0] = LITERAL.getIntValue();
|
||||
@ -96,7 +96,7 @@ public class VirtualMachineTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetHealth() {
|
||||
void testGetHealth() {
|
||||
var wizardNumber = 0;
|
||||
var bytecode = new int[8];
|
||||
bytecode[0] = LITERAL.getIntValue();
|
||||
@ -115,7 +115,7 @@ public class VirtualMachineTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlaySound() {
|
||||
void testPlaySound() {
|
||||
var wizardNumber = 0;
|
||||
var bytecode = new int[3];
|
||||
bytecode[0] = LITERAL.getIntValue();
|
||||
@ -130,7 +130,7 @@ public class VirtualMachineTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpawnParticles() {
|
||||
void testSpawnParticles() {
|
||||
var wizardNumber = 0;
|
||||
var bytecode = new int[3];
|
||||
bytecode[0] = LITERAL.getIntValue();
|
||||
@ -145,7 +145,7 @@ public class VirtualMachineTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidInstruction() {
|
||||
void testInvalidInstruction() {
|
||||
var bytecode = new int[1];
|
||||
bytecode[0] = 999;
|
||||
var vm = new VirtualMachine();
|
||||
|
@ -24,16 +24,16 @@
|
||||
package com.iluwatar.bytecode.util;
|
||||
|
||||
import com.iluwatar.bytecode.Instruction;
|
||||
import com.iluwatar.bytecode.util.InstructionConverterUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test for {@Link InstructionConverterUtil}
|
||||
* Test for {@link InstructionConverterUtil}
|
||||
*/
|
||||
public class InstructionConverterUtilTest {
|
||||
class InstructionConverterUtilTest {
|
||||
|
||||
@Test
|
||||
public void testEmptyInstruction() {
|
||||
void testEmptyInstruction() {
|
||||
var instruction = "";
|
||||
|
||||
var bytecode = InstructionConverterUtil.convertToByteCode(instruction);
|
||||
@ -42,7 +42,7 @@ public class InstructionConverterUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInstructions() {
|
||||
void testInstructions() {
|
||||
var instructions = "LITERAL 35 SET_HEALTH SET_WISDOM SET_AGILITY PLAY_SOUND"
|
||||
+ " SPAWN_PARTICLES GET_HEALTH ADD DIVIDE";
|
||||
|
||||
|
Reference in New Issue
Block a user