Skip to content

Commit dda0953

Browse files
anuragagarwal561994iluwatar
authored andcommitted
Resolves checkstyle errors for guarded-suspension, half-sync-half-async, hexagonal (iluwatar#1064)
* Reduces checkstyle errors in guarded-suspension * Reduces checkstyle errors in half-sync-half-async * Reduces checkstyle errors in hexagonal
1 parent 4f9ee01 commit dda0953

34 files changed

+382
-359
lines changed

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@
2121
* THE SOFTWARE.
2222
*/
2323

24-
/**
25-
* Guarded-suspension is a concurrent design pattern for handling situation when to execute some action we need
26-
* condition to be satisfied.
27-
* <p>
28-
* Implementation is based on GuardedQueue, which has two methods: get and put,
29-
* the condition is that we cannot get from empty queue so when thread attempt
30-
* to break the condition we invoke Object's wait method on him and when other thread put an element
31-
* to the queue he notify the waiting one that now he can get from queue.
32-
*/
3324
package com.iluwatar.guarded.suspension;
3425

3526
import java.util.concurrent.ExecutorService;
@@ -38,10 +29,18 @@
3829

3930
/**
4031
* Created by robertt240 on 1/26/17.
32+
*
33+
* <p>Guarded-suspension is a concurrent design pattern for handling situation when to execute some
34+
* action we need condition to be satisfied.
35+
*
36+
* <p>Implementation is based on GuardedQueue, which has two methods: get and put, the condition is
37+
* that we cannot get from empty queue so when thread attempt to break the condition we invoke
38+
* Object's wait method on him and when other thread put an element to the queue he notify the
39+
* waiting one that now he can get from queue.
4140
*/
4241
public class App {
4342
/**
44-
* Example pattern execution
43+
* Example pattern execution.
4544
*
4645
* @param args - command line args
4746
*/
@@ -55,13 +54,15 @@ public static void main(String[] args) {
5554
}
5655
);
5756

58-
//here we wait two seconds to show that the thread which is trying to get from guardedQueue will be waiting
57+
// here we wait two seconds to show that the thread which is trying
58+
// to get from guardedQueue will be waiting
5959
try {
6060
Thread.sleep(2000);
6161
} catch (InterruptedException e) {
6262
e.printStackTrace();
6363
}
64-
//now we execute second thread which will put number to guardedQueue and notify first thread that it could get
64+
// now we execute second thread which will put number to guardedQueue
65+
// and notify first thread that it could get
6566
executorService.execute(() -> {
6667
guardedQueue.put(20);
6768
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323

2424
package com.iluwatar.guarded.suspension;
2525

26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
28-
2926
import java.util.LinkedList;
3027
import java.util.Queue;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
3130

3231
/**
33-
* Guarded Queue is an implementation for Guarded Suspension Pattern
34-
* Guarded suspension pattern is used to handle a situation when you want to execute a method
35-
* on an object which is not in a proper state.
32+
* Guarded Queue is an implementation for Guarded Suspension Pattern Guarded suspension pattern is
33+
* used to handle a situation when you want to execute a method on an object which is not in a
34+
* proper state.
35+
*
3636
* @see <a href="http://java-design-patterns.com/patterns/guarded-suspension/">http://java-design-patterns.com/patterns/guarded-suspension/</a>
3737
*/
3838
public class GuardedQueue {
@@ -44,6 +44,8 @@ public GuardedQueue() {
4444
}
4545

4646
/**
47+
* Get the last element of the queue is exists.
48+
*
4749
* @return last element of a queue if queue is not empty
4850
*/
4951
public synchronized Integer get() {
@@ -60,6 +62,8 @@ public synchronized Integer get() {
6062
}
6163

6264
/**
65+
* Put a value in the queue.
66+
*
6367
* @param e number which we want to put to our queue
6468
*/
6569
public synchronized void put(Integer e) {

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

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,58 +23,50 @@
2323

2424
package com.iluwatar.halfsynchalfasync;
2525

26+
import java.util.concurrent.LinkedBlockingQueue;
2627
import org.slf4j.Logger;
2728
import org.slf4j.LoggerFactory;
2829

29-
import java.util.concurrent.LinkedBlockingQueue;
30-
3130
/**
32-
*
33-
* This application demonstrates Half-Sync/Half-Async pattern. Key parts of the pattern are
34-
* {@link AsyncTask} and {@link AsynchronousService}.
35-
*
36-
* <p>
37-
* <i>PROBLEM</i> <br>
31+
* This application demonstrates Half-Sync/Half-Async pattern. Key parts of the pattern are {@link
32+
* AsyncTask} and {@link AsynchronousService}.
33+
*
34+
* <p><i>PROBLEM</i> <br>
3835
* A concurrent system have a mixture of short duration, mid duration and long duration tasks. Mid
3936
* or long duration tasks should be performed asynchronously to meet quality of service
4037
* requirements.
41-
*
42-
* <p>
43-
* <i>INTENT</i> <br>
38+
*
39+
* <p><i>INTENT</i> <br>
4440
* The intent of this pattern is to separate the the synchronous and asynchronous processing in the
4541
* concurrent application by introducing two intercommunicating layers - one for sync and one for
4642
* async. This simplifies the programming without unduly affecting the performance.
47-
*
48-
* <p>
49-
* <i>APPLICABILITY</i> <br>
50-
* UNIX network subsystems - In operating systems network operations are carried out
51-
* asynchronously with help of hardware level interrupts.<br>
52-
* CORBA - At the asynchronous layer one thread is associated with each socket that is connected
53-
* to the client. Thread blocks waiting for CORBA requests from the client. On receiving request it
54-
* is inserted in the queuing layer which is then picked up by synchronous layer which processes the
55-
* request and sends response back to the client.<br>
56-
* Android AsyncTask framework - Framework provides a way to execute long running blocking
57-
* calls, such as downloading a file, in background threads so that the UI thread remains free to
58-
* respond to user inputs.<br>
59-
*
60-
* <p>
61-
* <i>IMPLEMENTATION</i> <br>
43+
*
44+
* <p><i>APPLICABILITY</i> <br>
45+
* UNIX network subsystems - In operating systems network operations are carried out asynchronously
46+
* with help of hardware level interrupts.<br> CORBA - At the asynchronous layer one thread is
47+
* associated with each socket that is connected to the client. Thread blocks waiting for CORBA
48+
* requests from the client. On receiving request it is inserted in the queuing layer which is then
49+
* picked up by synchronous layer which processes the request and sends response back to the
50+
* client.<br> Android AsyncTask framework - Framework provides a way to execute long running
51+
* blocking calls, such as downloading a file, in background threads so that the UI thread remains
52+
* free to respond to user inputs.<br>
53+
*
54+
* <p><i>IMPLEMENTATION</i> <br>
6255
* The main method creates an asynchronous service which does not block the main thread while the
6356
* task is being performed. The main thread continues its work which is similar to Async Method
6457
* Invocation pattern. The difference between them is that there is a queuing layer between
6558
* Asynchronous layer and synchronous layer, which allows for different communication patterns
6659
* between both layers. Such as Priority Queue can be used as queuing layer to prioritize the way
6760
* tasks are executed. Our implementation is just one simple way of implementing this pattern, there
6861
* are many variants possible as described in its applications.
69-
*
7062
*/
7163
public class App {
7264

7365
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
7466

7567
/**
76-
* Program entry point
77-
*
68+
* Program entry point.
69+
*
7870
* @param args command line args
7971
*/
8072
public static void main(String[] args) {
@@ -100,15 +92,13 @@ public static void main(String[] args) {
10092
}
10193

10294
/**
103-
*
104-
* ArithmeticSumTask
105-
*
95+
* ArithmeticSumTask.
10696
*/
10797
static class ArithmeticSumTask implements AsyncTask<Long> {
108-
private long n;
98+
private long numberOfElements;
10999

110-
public ArithmeticSumTask(long n) {
111-
this.n = n;
100+
public ArithmeticSumTask(long numberOfElements) {
101+
this.numberOfElements = numberOfElements;
112102
}
113103

114104
/*
@@ -117,7 +107,7 @@ public ArithmeticSumTask(long n) {
117107
*/
118108
@Override
119109
public Long call() throws Exception {
120-
return ap(n);
110+
return ap(numberOfElements);
121111
}
122112

123113
/*
@@ -128,7 +118,7 @@ public Long call() throws Exception {
128118
*/
129119
@Override
130120
public void onPreCall() {
131-
if (n < 0) {
121+
if (numberOfElements < 0) {
132122
throw new IllegalArgumentException("n is less than 0");
133123
}
134124
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* typically done is background threads and the result is posted back in form of callback. The
3131
* callback does not implement {@code isComplete}, {@code cancel} as it is out of scope of this
3232
* pattern.
33-
*
33+
*
3434
* @param <O> type of result
3535
*/
3636
public interface AsyncTask<O> extends Callable<O> {
@@ -53,7 +53,7 @@ public interface AsyncTask<O> extends Callable<O> {
5353
/**
5454
* A callback called if computing the task resulted in some exception. This method is called when
5555
* either of {@link #call()} or {@link #onPreCall()} throw any exception.
56-
*
56+
*
5757
* @param throwable error cause
5858
*/
5959
void onError(Throwable throwable);

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@
2323

2424
package com.iluwatar.halfsynchalfasync;
2525

26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
28-
2926
import java.util.concurrent.BlockingQueue;
3027
import java.util.concurrent.ExecutionException;
3128
import java.util.concurrent.ExecutorService;
3229
import java.util.concurrent.FutureTask;
3330
import java.util.concurrent.ThreadPoolExecutor;
3431
import java.util.concurrent.TimeUnit;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
3534

3635
/**
3736
* This is the asynchronous layer which does not block when a new request arrives. It just passes
@@ -63,13 +62,14 @@ public AsynchronousService(BlockingQueue<Runnable> workQueue) {
6362

6463
/**
6564
* A non-blocking method which performs the task provided in background and returns immediately.
66-
* <p>
67-
* On successful completion of task the result is posted back using callback method
68-
* {@link AsyncTask#onPostCall(Object)}, if task execution is unable to complete normally due to
69-
* some exception then the reason for error is posted back using callback method
70-
* {@link AsyncTask#onError(Throwable)}.
71-
* <p>
72-
* NOTE: The results are posted back in the context of background thread in this implementation.
65+
*
66+
* <p>On successful completion of task the result is posted back using callback method {@link
67+
* AsyncTask#onPostCall(Object)}, if task execution is unable to complete normally due to some
68+
* exception then the reason for error is posted back using callback method {@link
69+
* AsyncTask#onError(Throwable)}.
70+
*
71+
* <p>NOTE: The results are posted back in the context of background thread in this
72+
* implementation.
7373
*/
7474
public <T> void execute(final AsyncTask<T> task) {
7575
try {

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

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,35 @@
3131
import com.iluwatar.hexagonal.sampledata.SampleData;
3232

3333
/**
34-
*
35-
* Hexagonal Architecture pattern decouples the application core from the
36-
* services it uses. This allows the services to be plugged in and the
37-
* application will run with or without the services.<p>
38-
*
39-
* The core logic, or business logic, of an application consists of the
40-
* algorithms that are essential to its purpose. They implement the use
41-
* cases that are the heart of the application. When you change them, you
42-
* change the essence of the application.<p>
43-
*
44-
* The services are not essential. They can be replaced without changing
45-
* the purpose of the application. Examples: database access and other
46-
* types of storage, user interface components, e-mail and other
47-
* communication components, hardware devices.<p>
48-
*
49-
* This example demonstrates Hexagonal Architecture with a lottery system.
50-
* The application core is separate from the services that drive it and
51-
* from the services it uses.<p>
52-
*
53-
* The primary ports for the application are console interfaces
54-
* {@link com.iluwatar.hexagonal.administration.ConsoleAdministration} through which the lottery round is
55-
* initiated and run and {@link com.iluwatar.hexagonal.service.ConsoleLottery} that allows players to
56-
* submit lottery tickets for the draw.<p>
57-
*
58-
* The secondary ports that application core uses are {@link com.iluwatar.hexagonal.banking.WireTransfers}
59-
* which is a banking service, {@link com.iluwatar.hexagonal.eventlog.LotteryEventLog} that delivers
60-
* eventlog as lottery events occur and {@link com.iluwatar.hexagonal.database.LotteryTicketRepository}
61-
* that is the storage for the lottery tickets.
34+
* Hexagonal Architecture pattern decouples the application core from the services it uses. This
35+
* allows the services to be plugged in and the application will run with or without the services.
6236
*
37+
* <p>The core logic, or business logic, of an application consists of the algorithms that are
38+
* essential to its purpose. They implement the use cases that are the heart of the application.
39+
* When you change them, you change the essence of the application.
40+
*
41+
* <p>The services are not essential. They can be replaced without changing the purpose of the
42+
* application. Examples: database access and other types of storage, user interface components,
43+
* e-mail and other communication components, hardware devices.
44+
*
45+
* <p>This example demonstrates Hexagonal Architecture with a lottery system. The application core
46+
* is separate from the services that drive it and from the services it uses.
47+
*
48+
* <p>The primary ports for the application are console interfaces {@link
49+
* com.iluwatar.hexagonal.administration.ConsoleAdministration} through which the lottery round is
50+
* initiated and run and {@link com.iluwatar.hexagonal.service.ConsoleLottery} that allows players
51+
* to submit lottery tickets for the draw.
52+
*
53+
* <p>The secondary ports that application core uses are{@link
54+
* com.iluwatar.hexagonal.banking.WireTransfers} which is a banking service, {@link
55+
* com.iluwatar.hexagonal.eventlog.LotteryEventLog} that delivers eventlog as lottery events occur
56+
* and {@link com.iluwatar.hexagonal.database.LotteryTicketRepository} that is the storage for the
57+
* lottery tickets.
6358
*/
6459
public class App {
6560

6661
/**
67-
* Program entry point
62+
* Program entry point.
6863
*/
6964
public static void main(String[] args) {
7065

@@ -73,11 +68,11 @@ public static void main(String[] args) {
7368
// start new lottery round
7469
LotteryAdministration administration = injector.getInstance(LotteryAdministration.class);
7570
administration.resetLottery();
76-
71+
7772
// submit some lottery tickets
7873
LotteryService service = injector.getInstance(LotteryService.class);
7974
SampleData.submitTickets(service, 20);
80-
75+
8176
// perform lottery
8277
administration.performLottery();
8378
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,28 @@
3030
import com.iluwatar.hexagonal.module.LotteryModule;
3131
import com.iluwatar.hexagonal.mongo.MongoConnectionPropertiesLoader;
3232
import com.iluwatar.hexagonal.sampledata.SampleData;
33+
import java.util.Scanner;
3334
import org.slf4j.Logger;
3435
import org.slf4j.LoggerFactory;
3536

36-
import java.util.Scanner;
37-
3837
/**
39-
* Console interface for lottery administration
38+
* Console interface for lottery administration.
4039
*/
4140
public class ConsoleAdministration {
4241

4342
private static final Logger LOGGER = LoggerFactory.getLogger(ConsoleAdministration.class);
4443

4544
/**
46-
* Program entry point
45+
* Program entry point.
4746
*/
4847
public static void main(String[] args) {
4948
MongoConnectionPropertiesLoader.load();
5049
Injector injector = Guice.createInjector(new LotteryModule());
5150
LotteryAdministration administration = injector.getInstance(LotteryAdministration.class);
5251
LotteryService service = injector.getInstance(LotteryService.class);
5352
SampleData.submitTickets(service, 20);
54-
ConsoleAdministrationSrv consoleAdministration = new ConsoleAdministrationSrvImpl(administration, LOGGER);
53+
ConsoleAdministrationSrv consoleAdministration =
54+
new ConsoleAdministrationSrvImpl(administration, LOGGER);
5555
try (Scanner scanner = new Scanner(System.in)) {
5656
boolean exit = false;
5757
while (!exit) {

0 commit comments

Comments
 (0)