Skip to content

Commit c441831

Browse files
anuragagarwal561994iluwatar
authored andcommitted
Java 11 migration: ambassador async-method-invocation balking bridge builder (iluwatar#1076)
* Moves ambassador pattern to java 11 * Moves async-method-invocation pattern to java 11 * Moves balking pattern to java 11 * Moves bridge pattern to java 11 * Moves builder pattern to java 11
1 parent f0f0143 commit c441831

File tree

27 files changed

+173
-176
lines changed

27 files changed

+173
-176
lines changed

ambassador/README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ A remote services represented as a singleton.
4242
```java
4343
public class RemoteService implements RemoteServiceInterface {
4444

45-
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
45+
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
4646
private static RemoteService service = null;
4747

4848
static synchronized RemoteService getRemoteService() {
@@ -56,14 +56,14 @@ public class RemoteService implements RemoteServiceInterface {
5656

5757
@Override
5858
public long doRemoteFunction(int value) {
59-
6059
long waitTime = (long) Math.floor(Math.random() * 1000);
6160

6261
try {
6362
sleep(waitTime);
6463
} catch (InterruptedException e) {
65-
LOGGER.error("Thread sleep interrupted", e)
64+
LOGGER.error("Thread sleep interrupted", e);
6665
}
66+
6767
return waitTime >= 200 ? value * 10 : -1;
6868
}
6969
}
@@ -137,7 +137,7 @@ public class Client {
137137

138138
long useService(int value) {
139139
long result = serviceAmbassador.doRemoteFunction(value);
140-
LOGGER.info("Service result: " + result)
140+
LOGGER.info("Service result: " + result);
141141
return result;
142142
}
143143
}
@@ -146,10 +146,14 @@ public class Client {
146146
And here are two clients using the service.
147147

148148
```java
149-
Client host1 = new Client();
150-
Client host2 = new Client();
151-
host1.useService(12);
152-
host2.useService(73);
149+
public class App {
150+
public static void main(String[] args) {
151+
Client host1 = new Client();
152+
Client host2 = new Client();
153+
host1.useService(12);
154+
host2.useService(73);
155+
}
156+
}
153157
```
154158

155159
## Applicability

ambassador/src/main/java/com/iluwatar/ambassador/Client.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class Client {
3535
private final ServiceAmbassador serviceAmbassador = new ServiceAmbassador();
3636

3737
long useService(int value) {
38-
long result = serviceAmbassador.doRemoteFunction(value);
38+
var result = serviceAmbassador.doRemoteFunction(value);
3939
LOGGER.info("Service result: " + result);
4040
return result;
4141
}

ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* A remote legacy application represented by a Singleton implementation.
3434
*/
3535
public class RemoteService implements RemoteServiceInterface {
36-
static final int THRESHOLD = 200;
36+
private static final int THRESHOLD = 200;
3737
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
3838
private static RemoteService service = null;
3939
private final RandomProvider randomProvider;
@@ -50,7 +50,7 @@ private RemoteService() {
5050
}
5151

5252
/**
53-
* This constuctor is used for testing purposes only.
53+
* This constructor is used for testing purposes only.
5454
*/
5555
RemoteService(RandomProvider randomProvider) {
5656
this.randomProvider = randomProvider;

ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,19 @@ public long doRemoteFunction(int value) {
4848
}
4949

5050
private long checkLatency(int value) {
51-
long startTime = System.currentTimeMillis();
52-
long result = RemoteService.getRemoteService().doRemoteFunction(value);
53-
long timeTaken = System.currentTimeMillis() - startTime;
51+
var startTime = System.currentTimeMillis();
52+
var result = RemoteService.getRemoteService().doRemoteFunction(value);
53+
var timeTaken = System.currentTimeMillis() - startTime;
5454

5555
LOGGER.info("Time taken (ms): " + timeTaken);
5656
return result;
5757
}
5858

5959
private long safeCall(int value) {
60-
61-
int retries = 0;
62-
long result = FAILURE;
60+
var retries = 0;
61+
var result = (long) FAILURE;
6362

6463
for (int i = 0; i < RETRIES; i++) {
65-
6664
if (retries >= RETRIES) {
6765
return FAILURE;
6866
}

ambassador/src/test/java/com/iluwatar/ambassador/AppTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
/**
2929
* Application test
3030
*/
31-
public class AppTest {
31+
class AppTest {
3232

3333
@Test
34-
public void test() {
34+
void test() {
3535
App.main(new String[]{});
3636
}
3737
}

ambassador/src/test/java/com/iluwatar/ambassador/ClientTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@
3030
/**
3131
* Test for {@link Client}
3232
*/
33-
public class ClientTest {
33+
class ClientTest {
3434

3535
@Test
36-
public void test() {
37-
36+
void test() {
3837
Client client = new Client();
39-
long result = client.useService(10);
38+
var result = client.useService(10);
4039

4140
assertTrue(result == 100 || result == RemoteService.FAILURE);
4241
}

ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,31 @@
2323

2424
package com.iluwatar.ambassador;
2525

26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
2628
import com.iluwatar.ambassador.util.RandomProvider;
2729
import org.junit.jupiter.api.Test;
2830

29-
import static org.junit.jupiter.api.Assertions.assertEquals;
30-
import static org.junit.jupiter.api.Assertions.assertTrue;
31-
3231
/**
3332
* Test for {@link RemoteService}
3433
*/
35-
public class RemoteServiceTest {
34+
class RemoteServiceTest {
3635

3736
@Test
38-
public void testFailedCall() {
39-
RemoteService remoteService = new RemoteService(
40-
new StaticRandomProvider(0.21));
41-
long result = remoteService.doRemoteFunction(10);
37+
void testFailedCall() {
38+
var remoteService = new RemoteService(new StaticRandomProvider(0.21));
39+
var result = remoteService.doRemoteFunction(10);
4240
assertEquals(RemoteServiceInterface.FAILURE, result);
4341
}
4442

4543
@Test
46-
public void testSuccessfulCall() {
47-
RemoteService remoteService = new RemoteService(
48-
new StaticRandomProvider(0.2));
49-
long result = remoteService.doRemoteFunction(10);
44+
void testSuccessfulCall() {
45+
var remoteService = new RemoteService(new StaticRandomProvider(0.2));
46+
var result = remoteService.doRemoteFunction(10);
5047
assertEquals(100, result);
5148
}
5249

53-
private class StaticRandomProvider implements RandomProvider {
50+
private static class StaticRandomProvider implements RandomProvider {
5451
private double value;
5552

5653
StaticRandomProvider(double value) {

ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
/**
3131
* Test for {@link ServiceAmbassador}
3232
*/
33-
public class ServiceAmbassadorTest {
33+
class ServiceAmbassadorTest {
3434

3535
@Test
36-
public void test() {
36+
void test() {
3737
long result = new ServiceAmbassador().doRemoteFunction(10);
3838
assertTrue(result == 100 || result == RemoteServiceInterface.FAILURE);
3939
}

async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/App.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,24 @@ public class App {
6464
*/
6565
public static void main(String[] args) throws Exception {
6666
// construct a new executor that will run async tasks
67-
AsyncExecutor executor = new ThreadAsyncExecutor();
67+
var executor = new ThreadAsyncExecutor();
6868

6969
// start few async tasks with varying processing times, two last with callback handlers
70-
final AsyncResult<Integer> asyncResult1 = executor.startProcess(lazyval(10, 500));
71-
final AsyncResult<String> asyncResult2 = executor.startProcess(lazyval("test", 300));
72-
final AsyncResult<Long> asyncResult3 = executor.startProcess(lazyval(50L, 700));
73-
final AsyncResult<Integer> asyncResult4 =
74-
executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
75-
final AsyncResult<String> asyncResult5 =
70+
final var asyncResult1 = executor.startProcess(lazyval(10, 500));
71+
final var asyncResult2 = executor.startProcess(lazyval("test", 300));
72+
final var asyncResult3 = executor.startProcess(lazyval(50L, 700));
73+
final var asyncResult4 = executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
74+
final var asyncResult5 =
7675
executor.startProcess(lazyval("callback", 600), callback("Callback result 5"));
7776

7877
// emulate processing in the current thread while async tasks are running in their own threads
7978
Thread.sleep(350); // Oh boy I'm working hard here
8079
log("Some hard work done");
8180

8281
// wait for completion of the tasks
83-
final Integer result1 = executor.endProcess(asyncResult1);
84-
final String result2 = executor.endProcess(asyncResult2);
85-
final Long result3 = executor.endProcess(asyncResult3);
82+
final var result1 = executor.endProcess(asyncResult1);
83+
final var result2 = executor.endProcess(asyncResult2);
84+
final var result3 = executor.endProcess(asyncResult3);
8685
asyncResult4.await();
8786
asyncResult5.await();
8887

async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public <T> AsyncResult<T> startProcess(Callable<T> task) {
4545

4646
@Override
4747
public <T> AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callback) {
48-
CompletableResult<T> result = new CompletableResult<>(callback);
48+
var result = new CompletableResult<>(callback);
4949
new Thread(() -> {
5050
try {
5151
result.setValue(task.call());

async-method-invocation/src/test/java/com/iluwatar/async/method/invocation/AppTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@
2828
/**
2929
* Application test
3030
*/
31-
public class AppTest {
32-
31+
class AppTest {
3332
@Test
34-
public void test() throws Exception {
35-
String[] args = {};
36-
App.main(args);
33+
void test() throws Exception {
34+
App.main(new String[]{});
3735
}
3836
}

0 commit comments

Comments
 (0)