GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Java 11 migration: patterns (remaining b-c) (#1081) · jllogic/java-design-patterns@33ea733

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • business-delegate/src
    • main/java/com/iluwatar/business/delegate
      • App.java
    • test/java/com/iluwatar/business/delegate
      • BusinessDelegateTest.java
  • bytecode/src
    • main/java/com/iluwatar/bytecode
      • App.java
      • Instruction.java
      • VirtualMachine.java
      • util
        • InstructionConverterUtil.java
    • test/java/com/iluwatar/bytecode
      • AppTest.java
      • VirtualMachineTest.java
      • util
        • InstructionConverterUtilTest.java
  • caching/src
    • main/java/com/iluwatar/caching
      • App.java
      • AppManager.java
      • CacheStore.java
      • DbManager.java
      • LruCache.java
    • test/java/com/iluwatar/caching
      • AppTest.java
      • CachingTest.java
  • callback/src
    • main/java/com/iluwatar/callback
      • App.java
      • LambdasApp.java
      • Task.java
    • test/java/com/iluwatar/callback
      • AppTest.java
      • CallbackTest.java
  • chain/src
    • main/java/com/iluwatar/chain
      • App.java
      • OrcKing.java
    • test/java/com/iluwatar/chain
      • AppTest.java
      • OrcKingTest.java
  • circuit-breaker/src/test/java/com/iluwatar/circuitbreaker
    • CircuitBreakerTest.java
    • DelayedServiceTest.java
    • MonitoringServiceTest.java
  • collection-pipeline/src
    • main/java/com/iluwatar/collectionpipeline
      • App.java
      • ImperativeProgramming.java
    • test/java/com/iluwatar/collectionpipeline
      • AppTest.java
  • commander/src
    • main/java/com/iluwatar/commander
      • AppEmployeeDbFailCases.java
      • AppMessagingFailCases.java
      • AppPaymentFailCases.java
      • AppQueueFailCases.java
      • AppShippingFailCases.java
      • Commander.java
      • Order.java
      • Retry.java
      • employeehandle
        • EmployeeDatabase.java
        • EmployeeHandle.java
      • messagingservice
        • MessagingDatabase.java
        • MessagingService.java
      • paymentservice
        • PaymentDatabase.java
        • PaymentService.java
      • queue
        • Queue.java
        • QueueDatabase.java
        • QueueTask.java
      • shippingservice
        • ShippingDatabase.java
        • ShippingService.java
    • test/java/com/iluwatar/commander
      • RetryTest.java
  • command/src
    • main/java/com/iluwatar/command
      • App.java
      • ShrinkSpell.java
      • Wizard.java
    • test/java/com/iluwatar/command
      • AppTest.java
      • CommandTest.java
  • composite/src
    • main/java/com/iluwatar/composite
      • App.java
      • LetterComposite.java
      • Messenger.java
      • Sentence.java
      • Word.java
    • test/java/com/iluwatar/composite
      • AppTest.java
      • MessengerTest.java

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎business-delegate/src/main/java/com/iluwatar/business/delegate/App.java‎

Copy file name to clipboardExpand all lines: business-delegate/src/main/java/com/iluwatar/business/delegate/App.java
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ public class App {
4646
*/
4747
public static void main(String[] args) {
4848

49-
BusinessDelegate businessDelegate = new BusinessDelegate();
50-
BusinessLookup businessLookup = new BusinessLookup();
49+
var businessDelegate = new BusinessDelegate();
50+
var businessLookup = new BusinessLookup();
5151
businessLookup.setEjbService(new EjbService());
5252
businessLookup.setJmsService(new JmsService());
5353

5454
businessDelegate.setLookupService(businessLookup);
5555
businessDelegate.setServiceType(ServiceType.EJB);
5656

57-
Client client = new Client(businessDelegate);
57+
var client = new Client(businessDelegate);
5858
client.doTask();
5959

6060
businessDelegate.setServiceType(ServiceType.JMS);
Collapse file

‎business-delegate/src/test/java/com/iluwatar/business/delegate/BusinessDelegateTest.java‎

Copy file name to clipboardExpand all lines: business-delegate/src/test/java/com/iluwatar/business/delegate/BusinessDelegateTest.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void setup() {
7676
public void testBusinessDelegate() {
7777

7878
// setup a client object
79-
Client client = new Client(businessDelegate);
79+
var client = new Client(businessDelegate);
8080

8181
// set the service type
8282
businessDelegate.setServiceType(ServiceType.EJB);
Collapse file

‎bytecode/src/main/java/com/iluwatar/bytecode/App.java‎

Copy file name to clipboardExpand all lines: bytecode/src/main/java/com/iluwatar/bytecode/App.java
+4-6Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package com.iluwatar.bytecode;
2525

2626
import com.iluwatar.bytecode.util.InstructionConverterUtil;
27-
import java.util.Stack;
2827
import org.slf4j.Logger;
2928
import org.slf4j.LoggerFactory;
3029

@@ -51,12 +50,12 @@ public class App {
5150
*/
5251
public static void main(String[] args) {
5352

54-
Wizard wizard = new Wizard();
53+
var wizard = new Wizard();
5554
wizard.setHealth(45);
5655
wizard.setAgility(7);
5756
wizard.setWisdom(11);
5857

59-
VirtualMachine vm = new VirtualMachine();
58+
var vm = new VirtualMachine();
6059
vm.getWizards()[0] = wizard;
6160

6261
interpretInstruction("LITERAL 0", vm);
@@ -74,9 +73,8 @@ public static void main(String[] args) {
7473
}
7574

7675
private static void interpretInstruction(String instruction, VirtualMachine vm) {
77-
InstructionConverterUtil converter = new InstructionConverterUtil();
78-
vm.execute(converter.convertToByteCode(instruction));
79-
Stack<Integer> stack = vm.getStack();
76+
vm.execute(InstructionConverterUtil.convertToByteCode(instruction));
77+
var stack = vm.getStack();
8078
LOGGER.info(instruction + String.format("%" + (12 - instruction.length()) + "s", "") + stack);
8179
}
8280
}
Collapse file

‎bytecode/src/main/java/com/iluwatar/bytecode/Instruction.java‎

Copy file name to clipboardExpand all lines: bytecode/src/main/java/com/iluwatar/bytecode/Instruction.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public enum Instruction {
4040
ADD(10),
4141
DIVIDE(11);
4242

43-
private int value;
43+
private final int value;
4444

4545
Instruction(int value) {
4646
this.value = value;
@@ -57,7 +57,7 @@ public int getIntValue() {
5757
* @return representation of the instruction
5858
*/
5959
public static Instruction getInstruction(int value) {
60-
for (int i = 0; i < Instruction.values().length; i++) {
60+
for (var i = 0; i < Instruction.values().length; i++) {
6161
if (Instruction.values()[i].getIntValue() == value) {
6262
return Instruction.values()[i];
6363
}
Collapse file

‎bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java‎

Copy file name to clipboardExpand all lines: bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java
+7-9Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
*/
3131
public class VirtualMachine {
3232

33-
private Stack<Integer> stack = new Stack();
33+
private Stack<Integer> stack = new Stack<>();
3434

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

3737
/**
3838
* Constructor.
3939
*/
4040
public VirtualMachine() {
41-
for (int i = 0; i < wizards.length; i++) {
41+
for (var i = 0; i < wizards.length; i++) {
4242
wizards[i] = new Wizard();
4343
}
4444
}
@@ -49,19 +49,17 @@ public VirtualMachine() {
4949
* @param bytecode to execute
5050
*/
5151
public void execute(int[] bytecode) {
52-
for (int i = 0; i < bytecode.length; i++) {
52+
for (var i = 0; i < bytecode.length; i++) {
5353
Instruction instruction = Instruction.getInstruction(bytecode[i]);
54-
int wizard;
55-
int amount;
5654
switch (instruction) {
5755
case LITERAL:
5856
// Read the next byte from the bytecode.
5957
int value = bytecode[++i];
6058
stack.push(value);
6159
break;
6260
case SET_AGILITY:
63-
amount = stack.pop();
64-
wizard = stack.pop();
61+
var amount = stack.pop();
62+
var wizard = stack.pop();
6563
setAgility(wizard, amount);
6664
break;
6765
case SET_WISDOM:
@@ -87,8 +85,8 @@ public void execute(int[] bytecode) {
8785
stack.push(getWisdom(wizard));
8886
break;
8987
case ADD:
90-
int a = stack.pop();
91-
int b = stack.pop();
88+
var a = stack.pop();
89+
var b = stack.pop();
9290
stack.push(a + b);
9391
break;
9492
case DIVIDE:
Collapse file

‎bytecode/src/main/java/com/iluwatar/bytecode/util/InstructionConverterUtil.java‎

Copy file name to clipboardExpand all lines: bytecode/src/main/java/com/iluwatar/bytecode/util/InstructionConverterUtil.java
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ public static int[] convertToByteCode(String instructions) {
4040
return new int[0];
4141
}
4242

43-
String[] splitedInstructions = instructions.trim().split(" ");
44-
int[] bytecode = new int[splitedInstructions.length];
45-
for (int i = 0; i < splitedInstructions.length; i++) {
43+
var splitedInstructions = instructions.trim().split(" ");
44+
var bytecode = new int[splitedInstructions.length];
45+
for (var i = 0; i < splitedInstructions.length; i++) {
4646
if (isValidInstruction(splitedInstructions[i])) {
4747
bytecode[i] = Instruction.valueOf(splitedInstructions[i]).getIntValue();
4848
} else if (isValidInt(splitedInstructions[i])) {
49-
bytecode[i] = Integer.valueOf(splitedInstructions[i]);
49+
bytecode[i] = Integer.parseInt(splitedInstructions[i]);
5050
} else {
51-
String errorMessage = "Invalid instruction or number: " + splitedInstructions[i];
51+
var errorMessage = "Invalid instruction or number: " + splitedInstructions[i];
5252
throw new IllegalArgumentException(errorMessage);
5353
}
5454
}
Collapse file

‎bytecode/src/test/java/com/iluwatar/bytecode/AppTest.java‎

Copy file name to clipboardExpand all lines: bytecode/src/test/java/com/iluwatar/bytecode/AppTest.java
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public class AppTest {
3232

3333
@Test
3434
public void test() {
35-
String[] args = {};
36-
App.main(args);
35+
App.main(new String[]{});
3736
}
3837
}
Collapse file

‎bytecode/src/test/java/com/iluwatar/bytecode/VirtualMachineTest.java‎

Copy file name to clipboardExpand all lines: bytecode/src/test/java/com/iluwatar/bytecode/VirtualMachineTest.java
+22-22Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public class VirtualMachineTest {
3636

3737
@Test
3838
public void testLiteral() {
39-
int[] bytecode = new int[2];
39+
var bytecode = new int[2];
4040
bytecode[0] = LITERAL.getIntValue();
4141
bytecode[1] = 10;
4242

43-
VirtualMachine vm = new VirtualMachine();
43+
var vm = new VirtualMachine();
4444
vm.execute(bytecode);
4545

4646
assertEquals(1, vm.getStack().size());
@@ -49,56 +49,56 @@ public void testLiteral() {
4949

5050
@Test
5151
public void testSetHealth() {
52-
int wizardNumber = 0;
53-
int[] bytecode = new int[5];
52+
var wizardNumber = 0;
53+
var bytecode = new int[5];
5454
bytecode[0] = LITERAL.getIntValue();
5555
bytecode[1] = wizardNumber;
5656
bytecode[2] = LITERAL.getIntValue();
5757
bytecode[3] = 50; // health amount
5858
bytecode[4] = SET_HEALTH.getIntValue();
5959

60-
VirtualMachine vm = new VirtualMachine();
60+
var vm = new VirtualMachine();
6161
vm.execute(bytecode);
6262

6363
assertEquals(50, vm.getWizards()[wizardNumber].getHealth());
6464
}
6565

6666
@Test
6767
public void testSetAgility() {
68-
int wizardNumber = 0;
69-
int[] bytecode = new int[5];
68+
var wizardNumber = 0;
69+
var bytecode = new int[5];
7070
bytecode[0] = LITERAL.getIntValue();
7171
bytecode[1] = wizardNumber;
7272
bytecode[2] = LITERAL.getIntValue();
7373
bytecode[3] = 50; // agility amount
7474
bytecode[4] = SET_AGILITY.getIntValue();
7575

76-
VirtualMachine vm = new VirtualMachine();
76+
var vm = new VirtualMachine();
7777
vm.execute(bytecode);
7878

7979
assertEquals(50, vm.getWizards()[wizardNumber].getAgility());
8080
}
8181

8282
@Test
8383
public void testSetWisdom() {
84-
int wizardNumber = 0;
85-
int[] bytecode = new int[5];
84+
var wizardNumber = 0;
85+
var bytecode = new int[5];
8686
bytecode[0] = LITERAL.getIntValue();
8787
bytecode[1] = wizardNumber;
8888
bytecode[2] = LITERAL.getIntValue();
8989
bytecode[3] = 50; // wisdom amount
9090
bytecode[4] = SET_WISDOM.getIntValue();
9191

92-
VirtualMachine vm = new VirtualMachine();
92+
var vm = new VirtualMachine();
9393
vm.execute(bytecode);
9494

9595
assertEquals(50, vm.getWizards()[wizardNumber].getWisdom());
9696
}
9797

9898
@Test
9999
public void testGetHealth() {
100-
int wizardNumber = 0;
101-
int[] bytecode = new int[8];
100+
var wizardNumber = 0;
101+
var bytecode = new int[8];
102102
bytecode[0] = LITERAL.getIntValue();
103103
bytecode[1] = wizardNumber;
104104
bytecode[2] = LITERAL.getIntValue();
@@ -108,21 +108,21 @@ public void testGetHealth() {
108108
bytecode[6] = wizardNumber;
109109
bytecode[7] = GET_HEALTH.getIntValue();
110110

111-
VirtualMachine vm = new VirtualMachine();
111+
var vm = new VirtualMachine();
112112
vm.execute(bytecode);
113113

114114
assertEquals(Integer.valueOf(50), vm.getStack().pop());
115115
}
116116

117117
@Test
118118
public void testPlaySound() {
119-
int wizardNumber = 0;
120-
int[] bytecode = new int[3];
119+
var wizardNumber = 0;
120+
var bytecode = new int[3];
121121
bytecode[0] = LITERAL.getIntValue();
122122
bytecode[1] = wizardNumber;
123123
bytecode[2] = PLAY_SOUND.getIntValue();
124124

125-
VirtualMachine vm = new VirtualMachine();
125+
var vm = new VirtualMachine();
126126
vm.execute(bytecode);
127127

128128
assertEquals(0, vm.getStack().size());
@@ -131,13 +131,13 @@ public void testPlaySound() {
131131

132132
@Test
133133
public void testSpawnParticles() {
134-
int wizardNumber = 0;
135-
int[] bytecode = new int[3];
134+
var wizardNumber = 0;
135+
var bytecode = new int[3];
136136
bytecode[0] = LITERAL.getIntValue();
137137
bytecode[1] = wizardNumber;
138138
bytecode[2] = SPAWN_PARTICLES.getIntValue();
139139

140-
VirtualMachine vm = new VirtualMachine();
140+
var vm = new VirtualMachine();
141141
vm.execute(bytecode);
142142

143143
assertEquals(0, vm.getStack().size());
@@ -146,9 +146,9 @@ public void testSpawnParticles() {
146146

147147
@Test
148148
public void testInvalidInstruction() {
149-
int[] bytecode = new int[1];
149+
var bytecode = new int[1];
150150
bytecode[0] = 999;
151-
VirtualMachine vm = new VirtualMachine();
151+
var vm = new VirtualMachine();
152152

153153
assertThrows(IllegalArgumentException.class, () -> vm.execute(bytecode));
154154
}
Collapse file

‎bytecode/src/test/java/com/iluwatar/bytecode/util/InstructionConverterUtilTest.java‎

Copy file name to clipboardExpand all lines: bytecode/src/test/java/com/iluwatar/bytecode/util/InstructionConverterUtilTest.java
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@
3434
public class InstructionConverterUtilTest {
3535
@Test
3636
public void testEmptyInstruction() {
37-
String instruction = "";
37+
var instruction = "";
3838

39-
int[] bytecode = InstructionConverterUtil.convertToByteCode(instruction);
39+
var bytecode = InstructionConverterUtil.convertToByteCode(instruction);
4040

4141
Assertions.assertEquals(0, bytecode.length);
4242
}
4343

4444
@Test
4545
public void testInstructions() {
46-
String instructions =
47-
"LITERAL 35 SET_HEALTH SET_WISDOM SET_AGILITY PLAY_SOUND SPAWN_PARTICLES GET_HEALTH ADD DIVIDE";
46+
var instructions = "LITERAL 35 SET_HEALTH SET_WISDOM SET_AGILITY PLAY_SOUND"
47+
+ " SPAWN_PARTICLES GET_HEALTH ADD DIVIDE";
4848

49-
int[] bytecode = InstructionConverterUtil.convertToByteCode(instructions);
49+
var bytecode = InstructionConverterUtil.convertToByteCode(instructions);
5050

5151
Assertions.assertEquals(10, bytecode.length);
5252
Assertions.assertEquals(Instruction.LITERAL.getIntValue(), bytecode[0]);
Collapse file

‎caching/src/main/java/com/iluwatar/caching/App.java‎

Copy file name to clipboardExpand all lines: caching/src/main/java/com/iluwatar/caching/App.java
+9-9Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static void main(String[] args) {
7676
// true to run the tests with MongoDB (provided that MongoDB is
7777
// installed and socket connection is open).
7878
AppManager.initCacheCapacity(3);
79-
App app = new App();
79+
var app = new App();
8080
app.useReadAndWriteThroughStrategy();
8181
app.useReadThroughAndWriteAroundStrategy();
8282
app.useReadThroughAndWriteBehindStrategy();
@@ -90,7 +90,7 @@ public void useReadAndWriteThroughStrategy() {
9090
LOGGER.info("# CachingPolicy.THROUGH");
9191
AppManager.initCachingPolicy(CachingPolicy.THROUGH);
9292

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

9595
AppManager.save(userAccount1);
9696
LOGGER.info(AppManager.printCacheContent());
@@ -105,7 +105,7 @@ public void useReadThroughAndWriteAroundStrategy() {
105105
LOGGER.info("# CachingPolicy.AROUND");
106106
AppManager.initCachingPolicy(CachingPolicy.AROUND);
107107

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

110110
AppManager.save(userAccount2);
111111
LOGGER.info(AppManager.printCacheContent());
@@ -127,9 +127,9 @@ public void useReadThroughAndWriteBehindStrategy() {
127127
LOGGER.info("# CachingPolicy.BEHIND");
128128
AppManager.initCachingPolicy(CachingPolicy.BEHIND);
129129

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

134134
AppManager.save(userAccount3);
135135
AppManager.save(userAccount4);
@@ -152,9 +152,9 @@ public void useCacheAsideStategy() {
152152
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
153153
LOGGER.info(AppManager.printCacheContent());
154154

155-
UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
156-
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
157-
UserAccount userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
155+
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
156+
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
157+
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
158158
AppManager.save(userAccount3);
159159
AppManager.save(userAccount4);
160160
AppManager.save(userAccount5);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.