Skip to content

Commit f835d3d

Browse files
anuragagarwal561994iluwatar
authored andcommitted
Java 11 migrate remaining (g,h,i) (iluwatar#1116)
* Moves game-loop to Java 11 * Moves guarded-suspension to Java 11 * Moves half-sync-half-async to Java 11 * Moves hexagonal to Java 11 * Moves intercepting-filter to Java 11 * Moves interpreter to Java 11 * Moves iterator to Java 11
1 parent 7d0a5c0 commit f835d3d

File tree

68 files changed

+453
-532
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+453
-532
lines changed

game-loop/src/main/java/com/iluwatar/gameloop/GameLoop.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void stop() {
7070
* @return {@code true} if the game is running.
7171
*/
7272
public boolean isGameRunning() {
73-
return status == GameStatus.RUNNING ? true : false;
73+
return status == GameStatus.RUNNING;
7474
}
7575

7676
/**
@@ -80,7 +80,7 @@ public boolean isGameRunning() {
8080
*/
8181
protected void processInput() {
8282
try {
83-
int lag = new Random().nextInt(200) + 50;
83+
var lag = new Random().nextInt(200) + 50;
8484
Thread.sleep(lag);
8585
} catch (InterruptedException e) {
8686
logger.error(e.getMessage());

game-loop/src/test/java/com/iluwatar/gameloop/AppTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public class AppTest {
3232

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

3938
}

game-loop/src/test/java/com/iluwatar/gameloop/GameLoopTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void testStop() {
6565

6666
@Test
6767
public void testIsGameRunning() {
68-
Assert.assertEquals(false, gameLoop.isGameRunning());
68+
Assert.assertFalse(gameLoop.isGameRunning());
6969
}
7070

7171
}

guarded-suspension/src/main/java/com/iluwatar/guarded/suspension/App.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
package com.iluwatar.guarded.suspension;
2525

26-
import java.util.concurrent.ExecutorService;
2726
import java.util.concurrent.Executors;
2827
import java.util.concurrent.TimeUnit;
2928

@@ -45,14 +44,11 @@ public class App {
4544
* @param args - command line args
4645
*/
4746
public static void main(String[] args) {
48-
GuardedQueue guardedQueue = new GuardedQueue();
49-
ExecutorService executorService = Executors.newFixedThreadPool(3);
47+
var guardedQueue = new GuardedQueue();
48+
var executorService = Executors.newFixedThreadPool(3);
5049

5150
//here we create first thread which is supposed to get from guardedQueue
52-
executorService.execute(() -> {
53-
guardedQueue.get();
54-
}
55-
);
51+
executorService.execute(guardedQueue::get);
5652

5753
// here we wait two seconds to show that the thread which is trying
5854
// to get from guardedQueue will be waiting
@@ -63,10 +59,7 @@ public static void main(String[] args) {
6359
}
6460
// now we execute second thread which will put number to guardedQueue
6561
// and notify first thread that it could get
66-
executorService.execute(() -> {
67-
guardedQueue.put(20);
68-
}
69-
);
62+
executorService.execute(() -> guardedQueue.put(20));
7063
executorService.shutdown();
7164
try {
7265
executorService.awaitTermination(30, TimeUnit.SECONDS);

guarded-suspension/src/test/java/com/iluwatar/guarded/suspension/GuardedQueueTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@
2323

2424
package com.iluwatar.guarded.suspension;
2525

26-
import org.junit.jupiter.api.Test;
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
2727

28-
import java.util.concurrent.ExecutorService;
2928
import java.util.concurrent.Executors;
3029
import java.util.concurrent.TimeUnit;
31-
32-
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import org.junit.jupiter.api.Test;
3331

3432
/**
3533
* Test for Guarded Queue
@@ -39,8 +37,8 @@ public class GuardedQueueTest {
3937

4038
@Test
4139
public void testGet() {
42-
GuardedQueue g = new GuardedQueue();
43-
ExecutorService executorService = Executors.newFixedThreadPool(2);
40+
var g = new GuardedQueue();
41+
var executorService = Executors.newFixedThreadPool(2);
4442
executorService.submit(() -> value = g.get());
4543
executorService.submit(() -> g.put(10));
4644
executorService.shutdown();
@@ -54,7 +52,7 @@ public void testGet() {
5452

5553
@Test
5654
public void testPut() {
57-
GuardedQueue g = new GuardedQueue();
55+
var g = new GuardedQueue();
5856
g.put(12);
5957
assertEquals(Integer.valueOf(12), g.get());
6058
}

half-sync-half-async/src/main/java/com/iluwatar/halfsynchalfasync/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public class App {
7070
* @param args command line args
7171
*/
7272
public static void main(String[] args) {
73-
AsynchronousService service = new AsynchronousService(new LinkedBlockingQueue<>());
73+
var service = new AsynchronousService(new LinkedBlockingQueue<>());
7474
/*
7575
* A new task to calculate sum is received but as this is main thread, it should not block. So
7676
* it passes it to the asynchronous task layer to compute and proceeds with handling other

half-sync-half-async/src/test/java/com/iluwatar/halfsynchalfasync/AppTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@
2525

2626
import org.junit.jupiter.api.Test;
2727

28-
import java.util.concurrent.ExecutionException;
29-
3028
/**
31-
*
3229
* Application test
33-
*
3430
*/
3531
public class AppTest {
3632

half-sync-half-async/src/test/java/com/iluwatar/halfsynchalfasync/AsynchronousServiceTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ public void setUp() {
5757

5858
@Test
5959
public void testPerfectExecution() throws Exception {
60-
final Object result = new Object();
60+
final var result = new Object();
6161
when(task.call()).thenReturn(result);
6262
service.execute(task);
6363

6464
verify(task, timeout(2000)).onPostCall(eq(result));
6565

66-
final InOrder inOrder = inOrder(task);
66+
final var inOrder = inOrder(task);
6767
inOrder.verify(task, times(1)).onPreCall();
6868
inOrder.verify(task, times(1)).call();
6969
inOrder.verify(task, times(1)).onPostCall(eq(result));
@@ -73,13 +73,13 @@ public void testPerfectExecution() throws Exception {
7373

7474
@Test
7575
public void testCallException() throws Exception {
76-
final IOException exception = new IOException();
76+
final var exception = new IOException();
7777
when(task.call()).thenThrow(exception);
7878
service.execute(task);
7979

8080
verify(task, timeout(2000)).onError(eq(exception));
8181

82-
final InOrder inOrder = inOrder(task);
82+
final var inOrder = inOrder(task);
8383
inOrder.verify(task, times(1)).onPreCall();
8484
inOrder.verify(task, times(1)).call();
8585
inOrder.verify(task, times(1)).onError(exception);
@@ -89,13 +89,13 @@ public void testCallException() throws Exception {
8989

9090
@Test
9191
public void testPreCallException() {
92-
final IllegalStateException exception = new IllegalStateException();
92+
final var exception = new IllegalStateException();
9393
doThrow(exception).when(task).onPreCall();
9494
service.execute(task);
9595

9696
verify(task, timeout(2000)).onError(eq(exception));
9797

98-
final InOrder inOrder = inOrder(task);
98+
final var inOrder = inOrder(task);
9999
inOrder.verify(task, times(1)).onPreCall();
100100
inOrder.verify(task, times(1)).onError(exception);
101101

hexagonal/src/main/java/com/iluwatar/hexagonal/App.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package com.iluwatar.hexagonal;
2525

2626
import com.google.inject.Guice;
27-
import com.google.inject.Injector;
2827
import com.iluwatar.hexagonal.domain.LotteryAdministration;
2928
import com.iluwatar.hexagonal.domain.LotteryService;
3029
import com.iluwatar.hexagonal.module.LotteryTestingModule;
@@ -63,14 +62,14 @@ public class App {
6362
*/
6463
public static void main(String[] args) {
6564

66-
Injector injector = Guice.createInjector(new LotteryTestingModule());
65+
var injector = Guice.createInjector(new LotteryTestingModule());
6766

6867
// start new lottery round
69-
LotteryAdministration administration = injector.getInstance(LotteryAdministration.class);
68+
var administration = injector.getInstance(LotteryAdministration.class);
7069
administration.resetLottery();
7170

7271
// submit some lottery tickets
73-
LotteryService service = injector.getInstance(LotteryService.class);
72+
var service = injector.getInstance(LotteryService.class);
7473
SampleData.submitTickets(service, 20);
7574

7675
// perform lottery

hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package com.iluwatar.hexagonal.administration;
2525

2626
import com.google.inject.Guice;
27-
import com.google.inject.Injector;
2827
import com.iluwatar.hexagonal.domain.LotteryAdministration;
2928
import com.iluwatar.hexagonal.domain.LotteryService;
3029
import com.iluwatar.hexagonal.module.LotteryModule;
@@ -46,17 +45,16 @@ public class ConsoleAdministration {
4645
*/
4746
public static void main(String[] args) {
4847
MongoConnectionPropertiesLoader.load();
49-
Injector injector = Guice.createInjector(new LotteryModule());
50-
LotteryAdministration administration = injector.getInstance(LotteryAdministration.class);
51-
LotteryService service = injector.getInstance(LotteryService.class);
48+
var injector = Guice.createInjector(new LotteryModule());
49+
var administration = injector.getInstance(LotteryAdministration.class);
50+
var service = injector.getInstance(LotteryService.class);
5251
SampleData.submitTickets(service, 20);
53-
ConsoleAdministrationSrv consoleAdministration =
54-
new ConsoleAdministrationSrvImpl(administration, LOGGER);
55-
try (Scanner scanner = new Scanner(System.in)) {
56-
boolean exit = false;
52+
var consoleAdministration = new ConsoleAdministrationSrvImpl(administration, LOGGER);
53+
try (var scanner = new Scanner(System.in)) {
54+
var exit = false;
5755
while (!exit) {
5856
printMainMenu();
59-
String cmd = readString(scanner);
57+
var cmd = readString(scanner);
6058
if ("1".equals(cmd)) {
6159
consoleAdministration.getAllSubmittedTickets();
6260
} else if ("2".equals(cmd)) {

hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministrationSrvImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void getAllSubmittedTickets() {
5050

5151
@Override
5252
public void performLottery() {
53-
LotteryNumbers numbers = administration.performLottery();
53+
var numbers = administration.performLottery();
5454
logger.info("The winning numbers: {}", numbers.getNumbersAsString());
5555
logger.info("Time to reset the database for next round, eh?");
5656
}

hexagonal/src/main/java/com/iluwatar/hexagonal/banking/MongoBank.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.mongodb.client.MongoDatabase;
2929
import com.mongodb.client.model.UpdateOptions;
3030
import java.util.ArrayList;
31-
import java.util.List;
3231
import org.bson.Document;
3332

3433
/**
@@ -107,30 +106,31 @@ public MongoCollection<Document> getAccountsCollection() {
107106

108107
@Override
109108
public void setFunds(String bankAccount, int amount) {
110-
Document search = new Document("_id", bankAccount);
111-
Document update = new Document("_id", bankAccount).append("funds", amount);
112-
accountsCollection
113-
.updateOne(search, new Document("$set", update), new UpdateOptions().upsert(true));
109+
var search = new Document("_id", bankAccount);
110+
var update = new Document("_id", bankAccount).append("funds", amount);
111+
var updateOptions = new UpdateOptions().upsert(true);
112+
accountsCollection.updateOne(search, new Document("$set", update), updateOptions);
114113
}
115114

116115
@Override
117116
public int getFunds(String bankAccount) {
118-
Document search = new Document("_id", bankAccount);
119-
List<Document> results = accountsCollection.find(search).limit(1).into(new ArrayList<>());
120-
if (results.size() > 0) {
121-
return results.get(0).getInteger("funds");
122-
} else {
123-
return 0;
124-
}
117+
return accountsCollection
118+
.find(new Document("_id", bankAccount))
119+
.limit(1)
120+
.into(new ArrayList<>())
121+
.stream()
122+
.findFirst()
123+
.map(x -> x.getInteger("funds"))
124+
.orElse(0);
125125
}
126126

127127
@Override
128128
public boolean transferFunds(int amount, String sourceAccount, String destinationAccount) {
129-
int sourceFunds = getFunds(sourceAccount);
129+
var sourceFunds = getFunds(sourceAccount);
130130
if (sourceFunds < amount) {
131131
return false;
132132
} else {
133-
int destFunds = getFunds(destinationAccount);
133+
var destFunds = getFunds(destinationAccount);
134134
setFunds(sourceAccount, sourceFunds - amount);
135135
setFunds(destinationAccount, destFunds + amount);
136136
return true;

hexagonal/src/main/java/com/iluwatar/hexagonal/database/InMemoryTicketRepository.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,12 @@ public class InMemoryTicketRepository implements LotteryTicketRepository {
3838

3939
@Override
4040
public Optional<LotteryTicket> findById(LotteryTicketId id) {
41-
LotteryTicket ticket = tickets.get(id);
42-
if (ticket == null) {
43-
return Optional.empty();
44-
} else {
45-
return Optional.of(ticket);
46-
}
41+
return Optional.ofNullable(tickets.get(id));
4742
}
4843

4944
@Override
5045
public Optional<LotteryTicketId> save(LotteryTicket ticket) {
51-
LotteryTicketId id = new LotteryTicketId();
46+
var id = new LotteryTicketId();
5247
tickets.put(id, ticket);
5348
return Optional.of(id);
5449
}

0 commit comments

Comments
 (0)