Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1fa8a60
Sharding Pattern (#1056)
Azureyjt Nov 8, 2019
6d1c0b1
Resolves checkstyle errors for ambassador, async-method-invocation, b…
anuragagarwal561994 Nov 9, 2019
efc17fc
Resolves checkstyle errors for business-delegate, bytecode, caching (…
anuragagarwal561994 Nov 9, 2019
31f27a7
Resolves checkstyle errors for callback, chain, circuit-breaker (#1060)
anuragagarwal561994 Nov 9, 2019
2f49648
Resolves checkstyle errors for collection-pipeline, command, commande…
anuragagarwal561994 Nov 9, 2019
4f9ee01
Resolves checkstyle errors for converter, cqrs (#1063)
anuragagarwal561994 Nov 10, 2019
dda0953
Resolves checkstyle errors for guarded-suspension, half-sync-half-asy…
anuragagarwal561994 Nov 10, 2019
7f06f3b
Resolves checkstyle errors for intercepting-filter, interpreter, iter…
anuragagarwal561994 Nov 10, 2019
eae09fc
Resolves checkstyle errors for api-gateway, lazy-loading, leader-elec…
anuragagarwal561994 Nov 10, 2019
01e489c
Resolves checkstyle errors for dao data-bus data-locality data-mapper…
anuragagarwal561994 Nov 10, 2019
f2c91eb
Resolves checkstyle errors for delegation dependency-injection dirty-…
anuragagarwal561994 Nov 10, 2019
7c888e8
Resolves checkstyle errors for eip-* (#1069)
anuragagarwal561994 Nov 10, 2019
5ae2ce6
Resolves checkstyle errors for event-* (#1070)
anuragagarwal561994 Nov 10, 2019
4dae1fa
Resolves checkstyle errors for execute-around extension-objects (#1071)
anuragagarwal561994 Nov 10, 2019
9c8ad44
Resolves checkstyle errors for patterns starting with letter r (#1072)
anuragagarwal561994 Nov 10, 2019
b92eb52
Resolves checkstyle errors for template-method thread-pool throttling…
anuragagarwal561994 Nov 10, 2019
f0f0143
Resolves checkstyle errors for trampoline twin typeobjectpattern unit…
anuragagarwal561994 Nov 10, 2019
c441831
Java 11 migration: ambassador async-method-invocation balking bridge …
anuragagarwal561994 Nov 11, 2019
329479d
#984 update Ambassador readme
iluwatar Nov 11, 2019
0272d71
#984 update Bridge readme
iluwatar Nov 11, 2019
2628cc0
#984 update Builder readme
iluwatar Nov 11, 2019
c954a43
Resolves checkstyle errors for facade factory-kit spatial-partition s…
anuragagarwal561994 Nov 11, 2019
37599eb
Resolves checkstyle errors for feature-toggle fluentinterface flux fl…
anuragagarwal561994 Nov 11, 2019
3907951
Resolves checkstyle issues for semaphore servant serverless service-l…
anuragagarwal561994 Nov 11, 2019
1e76d91
Resolves checkstyle errors for abstract-document abstract-factory acy…
anuragagarwal561994 Nov 11, 2019
6ef840f
Resolves checkstyle errors for naked-objects null-object object-mothe…
anuragagarwal561994 Nov 12, 2019
33ea733
Java 11 migration: patterns (remaining b-c) (#1081)
anuragagarwal561994 Nov 12, 2019
3c57bf7
#984 #987 update readmes
iluwatar Nov 12, 2019
f04fc3c
Java 11 migration: patterns starting with a (#1084)
anuragagarwal561994 Nov 13, 2019
160b737
Add another real world example for Builder
iluwatar Nov 13, 2019
50467c9
Java 11 migration: patterns (t-v) (#1085)
anuragagarwal561994 Nov 14, 2019
cc571f4
Saga pattern (#1062)
besok Nov 14, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Java 11 migration: patterns (remaining b-c) (iluwatar#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
  • Loading branch information
anuragagarwal561994 authored and iluwatar committed Nov 12, 2019
commit 33ea7335b11559eccdf125e42faf8eb340f710a4
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public class App {
*/
public static void main(String[] args) {

BusinessDelegate businessDelegate = new BusinessDelegate();
BusinessLookup businessLookup = new BusinessLookup();
var businessDelegate = new BusinessDelegate();
var businessLookup = new BusinessLookup();
businessLookup.setEjbService(new EjbService());
businessLookup.setJmsService(new JmsService());

businessDelegate.setLookupService(businessLookup);
businessDelegate.setServiceType(ServiceType.EJB);

Client client = new Client(businessDelegate);
var client = new Client(businessDelegate);
client.doTask();

businessDelegate.setServiceType(ServiceType.JMS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void setup() {
public void testBusinessDelegate() {

// setup a client object
Client client = new Client(businessDelegate);
var client = new Client(businessDelegate);

// set the service type
businessDelegate.setServiceType(ServiceType.EJB);
Expand Down
10 changes: 4 additions & 6 deletions bytecode/src/main/java/com/iluwatar/bytecode/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -74,9 +73,8 @@ public static void main(String[] args) {
}

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);
}
}
4 changes: 2 additions & 2 deletions bytecode/src/main/java/com/iluwatar/bytecode/Instruction.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public enum Instruction {
ADD(10),
DIVIDE(11);

private int value;
private final int value;

Instruction(int value) {
this.value = value;
Expand All @@ -57,7 +57,7 @@ public int getIntValue() {
* @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];
}
Expand Down
16 changes: 7 additions & 9 deletions bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
*/
public class VirtualMachine {

private Stack<Integer> stack = new Stack();
private Stack<Integer> stack = new Stack<>();

private Wizard[] wizards = new Wizard[2];

/**
* Constructor.
*/
public VirtualMachine() {
for (int i = 0; i < wizards.length; i++) {
for (var i = 0; i < wizards.length; i++) {
wizards[i] = new Wizard();
}
}
Expand All @@ -49,19 +49,17 @@ public 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.
int value = bytecode[++i];
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:
Expand All @@ -87,8 +85,8 @@ public void execute(int[] bytecode) {
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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public static int[] convertToByteCode(String instructions) {
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);
}
}
Expand Down
3 changes: 1 addition & 2 deletions bytecode/src/test/java/com/iluwatar/bytecode/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class AppTest {

@Test
public void test() {
String[] args = {};
App.main(args);
App.main(new String[]{});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -49,56 +49,56 @@ public void testLiteral() {

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

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

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

@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();
Expand All @@ -108,21 +108,21 @@ public void testGetHealth() {
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());
}

@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());
Expand All @@ -131,13 +131,13 @@ public void testPlaySound() {

@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());
Expand All @@ -146,9 +146,9 @@ public void testSpawnParticles() {

@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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@
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]);
Expand Down
18 changes: 9 additions & 9 deletions caching/src/main/java/com/iluwatar/caching/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static void main(String[] args) {
// true to run the tests with MongoDB (provided that MongoDB is
// installed and socket connection is open).
AppManager.initCacheCapacity(3);
App app = new App();
var app = new App();
app.useReadAndWriteThroughStrategy();
app.useReadThroughAndWriteAroundStrategy();
app.useReadThroughAndWriteBehindStrategy();
Expand All @@ -90,7 +90,7 @@ public void useReadAndWriteThroughStrategy() {
LOGGER.info("# CachingPolicy.THROUGH");
AppManager.initCachingPolicy(CachingPolicy.THROUGH);

UserAccount userAccount1 = new UserAccount("001", "John", "He is a boy.");
var userAccount1 = new UserAccount("001", "John", "He is a boy.");

AppManager.save(userAccount1);
LOGGER.info(AppManager.printCacheContent());
Expand All @@ -105,7 +105,7 @@ public void useReadThroughAndWriteAroundStrategy() {
LOGGER.info("# CachingPolicy.AROUND");
AppManager.initCachingPolicy(CachingPolicy.AROUND);

UserAccount userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
var userAccount2 = new UserAccount("002", "Jane", "She is a girl.");

AppManager.save(userAccount2);
LOGGER.info(AppManager.printCacheContent());
Expand All @@ -127,9 +127,9 @@ public void useReadThroughAndWriteBehindStrategy() {
LOGGER.info("# CachingPolicy.BEHIND");
AppManager.initCachingPolicy(CachingPolicy.BEHIND);

UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
UserAccount userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");

AppManager.save(userAccount3);
AppManager.save(userAccount4);
Expand All @@ -152,9 +152,9 @@ public void useCacheAsideStategy() {
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
LOGGER.info(AppManager.printCacheContent());

UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
UserAccount userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
AppManager.save(userAccount3);
AppManager.save(userAccount4);
AppManager.save(userAccount5);
Expand Down
Loading