mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 12:58:37 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -58,9 +58,7 @@ public class App {
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
var vm = new VirtualMachine(
|
||||
new Wizard(45, 7, 11, 0, 0),
|
||||
new Wizard(36, 18, 8, 0, 0));
|
||||
var vm = new VirtualMachine(new Wizard(45, 7, 11, 0, 0), new Wizard(36, 18, 8, 0, 0));
|
||||
|
||||
vm.execute(InstructionConverterUtil.convertToByteCode(LITERAL_0));
|
||||
vm.execute(InstructionConverterUtil.convertToByteCode(LITERAL_0));
|
||||
|
||||
@@ -27,24 +27,21 @@ package com.iluwatar.bytecode;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Representation of instructions understandable by virtual machine.
|
||||
*/
|
||||
/** Representation of instructions understandable by virtual machine. */
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum Instruction {
|
||||
|
||||
LITERAL(1), // e.g. "LITERAL 0", push 0 to stack
|
||||
SET_HEALTH(2), // e.g. "SET_HEALTH", pop health and wizard number, call set health
|
||||
SET_WISDOM(3), // e.g. "SET_WISDOM", pop wisdom and wizard number, call set wisdom
|
||||
SET_AGILITY(4), // e.g. "SET_AGILITY", pop agility and wizard number, call set agility
|
||||
PLAY_SOUND(5), // e.g. "PLAY_SOUND", pop value as wizard number, call play sound
|
||||
LITERAL(1), // e.g. "LITERAL 0", push 0 to stack
|
||||
SET_HEALTH(2), // e.g. "SET_HEALTH", pop health and wizard number, call set health
|
||||
SET_WISDOM(3), // e.g. "SET_WISDOM", pop wisdom and wizard number, call set wisdom
|
||||
SET_AGILITY(4), // e.g. "SET_AGILITY", pop agility and wizard number, call set agility
|
||||
PLAY_SOUND(5), // e.g. "PLAY_SOUND", pop value as wizard number, call play sound
|
||||
SPAWN_PARTICLES(6), // e.g. "SPAWN_PARTICLES", pop value as wizard number, call spawn particles
|
||||
GET_HEALTH(7), // e.g. "GET_HEALTH", pop value as wizard number, push wizard's health
|
||||
GET_AGILITY(8), // e.g. "GET_AGILITY", pop value as wizard number, push wizard's agility
|
||||
GET_WISDOM(9), // e.g. "GET_WISDOM", pop value as wizard number, push wizard's wisdom
|
||||
ADD(10), // e.g. "ADD", pop 2 values, push their sum
|
||||
DIVIDE(11); // e.g. "DIVIDE", pop 2 values, push their division
|
||||
GET_HEALTH(7), // e.g. "GET_HEALTH", pop value as wizard number, push wizard's health
|
||||
GET_AGILITY(8), // e.g. "GET_AGILITY", pop value as wizard number, push wizard's agility
|
||||
GET_WISDOM(9), // e.g. "GET_WISDOM", pop value as wizard number, push wizard's wisdom
|
||||
ADD(10), // e.g. "ADD", pop 2 values, push their sum
|
||||
DIVIDE(11); // e.g. "DIVIDE", pop 2 values, push their division
|
||||
|
||||
private final int intValue;
|
||||
|
||||
|
||||
@@ -29,9 +29,7 @@ import java.util.concurrent.ThreadLocalRandom;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Implementation of virtual machine.
|
||||
*/
|
||||
/** Implementation of virtual machine. */
|
||||
@Getter
|
||||
@Slf4j
|
||||
public class VirtualMachine {
|
||||
@@ -40,19 +38,13 @@ public class VirtualMachine {
|
||||
|
||||
private final Wizard[] wizards = new Wizard[2];
|
||||
|
||||
/**
|
||||
* No-args constructor.
|
||||
*/
|
||||
/** No-args constructor. */
|
||||
public VirtualMachine() {
|
||||
wizards[0] = new Wizard(randomInt(3, 32), randomInt(3, 32), randomInt(3, 32),
|
||||
0, 0);
|
||||
wizards[1] = new Wizard(randomInt(3, 32), randomInt(3, 32), randomInt(3, 32),
|
||||
0, 0);
|
||||
wizards[0] = new Wizard(randomInt(3, 32), randomInt(3, 32), randomInt(3, 32), 0, 0);
|
||||
wizards[1] = new Wizard(randomInt(3, 32), randomInt(3, 32), randomInt(3, 32), 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor taking the wizards as arguments.
|
||||
*/
|
||||
/** Constructor taking the wizards as arguments. */
|
||||
public VirtualMachine(Wizard wizard1, Wizard wizard2) {
|
||||
wizards[0] = wizard1;
|
||||
wizards[1] = wizard2;
|
||||
@@ -112,7 +104,6 @@ public class VirtualMachine {
|
||||
case PLAY_SOUND -> {
|
||||
var wizard = stack.pop();
|
||||
getWizards()[wizard].playSound();
|
||||
|
||||
}
|
||||
case SPAWN_PARTICLES -> {
|
||||
var wizard = stack.pop();
|
||||
|
||||
@@ -26,9 +26,7 @@ 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.
|
||||
|
||||
@@ -24,24 +24,21 @@
|
||||
*/
|
||||
package com.iluwatar.bytecode;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Application test */
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
* <p>Solution: Inserted assertion to check whether the execution of the main method in {@link
|
||||
* App} throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,9 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test for {@link VirtualMachine}
|
||||
*/
|
||||
/** Test for {@link VirtualMachine} */
|
||||
class VirtualMachineTest {
|
||||
|
||||
@Test
|
||||
@@ -55,7 +53,7 @@ class VirtualMachineTest {
|
||||
bytecode[0] = LITERAL.getIntValue();
|
||||
bytecode[1] = wizardNumber;
|
||||
bytecode[2] = LITERAL.getIntValue();
|
||||
bytecode[3] = 50; // health amount
|
||||
bytecode[3] = 50; // health amount
|
||||
bytecode[4] = SET_HEALTH.getIntValue();
|
||||
|
||||
var vm = new VirtualMachine();
|
||||
@@ -71,7 +69,7 @@ class VirtualMachineTest {
|
||||
bytecode[0] = LITERAL.getIntValue();
|
||||
bytecode[1] = wizardNumber;
|
||||
bytecode[2] = LITERAL.getIntValue();
|
||||
bytecode[3] = 50; // agility amount
|
||||
bytecode[3] = 50; // agility amount
|
||||
bytecode[4] = SET_AGILITY.getIntValue();
|
||||
|
||||
var vm = new VirtualMachine();
|
||||
@@ -87,7 +85,7 @@ class VirtualMachineTest {
|
||||
bytecode[0] = LITERAL.getIntValue();
|
||||
bytecode[1] = wizardNumber;
|
||||
bytecode[2] = LITERAL.getIntValue();
|
||||
bytecode[3] = 50; // wisdom amount
|
||||
bytecode[3] = 50; // wisdom amount
|
||||
bytecode[4] = SET_WISDOM.getIntValue();
|
||||
|
||||
var vm = new VirtualMachine();
|
||||
@@ -103,7 +101,7 @@ class VirtualMachineTest {
|
||||
bytecode[0] = LITERAL.getIntValue();
|
||||
bytecode[1] = wizardNumber;
|
||||
bytecode[2] = LITERAL.getIntValue();
|
||||
bytecode[3] = 50; // health amount
|
||||
bytecode[3] = 50; // health amount
|
||||
bytecode[4] = SET_HEALTH.getIntValue();
|
||||
bytecode[5] = LITERAL.getIntValue();
|
||||
bytecode[6] = wizardNumber;
|
||||
|
||||
@@ -28,9 +28,7 @@ import com.iluwatar.bytecode.Instruction;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test for {@link InstructionConverterUtil}
|
||||
*/
|
||||
/** Test for {@link InstructionConverterUtil} */
|
||||
class InstructionConverterUtilTest {
|
||||
|
||||
@Test
|
||||
@@ -44,8 +42,9 @@ class InstructionConverterUtilTest {
|
||||
|
||||
@Test
|
||||
void testInstructions() {
|
||||
var 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";
|
||||
|
||||
var bytecode = InstructionConverterUtil.convertToByteCode(instructions);
|
||||
|
||||
@@ -61,5 +60,4 @@ class InstructionConverterUtilTest {
|
||||
Assertions.assertEquals(Instruction.ADD.getIntValue(), bytecode[8]);
|
||||
Assertions.assertEquals(Instruction.DIVIDE.getIntValue(), bytecode[9]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user