From 6d1c0b1563caa1cc243cbe341864f99d42ee4fcb Mon Sep 17 00:00:00 2001 From: Anurag Agarwal Date: Sun, 10 Nov 2019 00:33:22 +0530 Subject: [PATCH] Resolves checkstyle errors for ambassador, async-method-invocation, balking, bridge, builder (#1058) * Decreases checkstyle errors for ambassador pattern * Reduces checkstyle errors in async-method-invocation * Reduces checkstyle errors in balking * Reduces checkstyle errors in bridge * Reduces checkstyle errors in builder --- .../java/com/iluwatar/ambassador/App.java | 23 +++---- .../java/com/iluwatar/ambassador/Client.java | 5 +- .../iluwatar/ambassador/RemoteService.java | 10 +-- .../ambassador/ServiceAmbassador.java | 9 ++- .../iluwatar/async/method/invocation/App.java | 68 +++++++++---------- .../method/invocation/AsyncCallback.java | 8 +-- .../method/invocation/AsyncExecutor.java | 8 +-- .../async/method/invocation/AsyncResult.java | 5 +- .../invocation/ThreadAsyncExecutor.java | 30 ++++---- .../async/method/invocation/AppTest.java | 2 - .../invocation/ThreadAsyncExecutorTest.java | 64 ++++++++--------- .../main/java/com/iluwatar/balking/App.java | 21 +++--- .../com/iluwatar/balking/WashingMachine.java | 24 +++---- .../iluwatar/balking/WashingMachineState.java | 5 +- .../iluwatar/balking/WashingMachineTest.java | 5 +- .../main/java/com/iluwatar/bridge/App.java | 26 +++---- .../java/com/iluwatar/bridge/Enchantment.java | 4 +- .../iluwatar/bridge/FlyingEnchantment.java | 4 +- .../main/java/com/iluwatar/bridge/Hammer.java | 4 +- .../bridge/SoulEatingEnchantment.java | 4 +- .../main/java/com/iluwatar/bridge/Sword.java | 4 +- .../main/java/com/iluwatar/bridge/Weapon.java | 4 +- .../main/java/com/iluwatar/builder/App.java | 35 +++++----- .../main/java/com/iluwatar/builder/Armor.java | 4 +- .../java/com/iluwatar/builder/HairColor.java | 4 +- .../java/com/iluwatar/builder/HairType.java | 4 +- .../main/java/com/iluwatar/builder/Hero.java | 12 ++-- .../java/com/iluwatar/builder/Profession.java | 4 +- .../java/com/iluwatar/builder/Weapon.java | 4 +- 29 files changed, 179 insertions(+), 225 deletions(-) diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/App.java b/ambassador/src/main/java/com/iluwatar/ambassador/App.java index 6b0b048c8..c61ef4935 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/App.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/App.java @@ -24,27 +24,26 @@ package com.iluwatar.ambassador; /** - * * The ambassador pattern creates a helper service that sends network requests on behalf of a * client. It is often used in cloud-based applications to offload features of a remote service. * - * An ambassador service can be thought of as an out-of-process proxy that is co-located with - * the client. Similar to the proxy design pattern, the ambassador service provides an interface - * for another remote service. In addition to the interface, the ambassador provides extra - * functionality and features, specifically offloaded common connectivity tasks. This usually - * consists of monitoring, logging, routing, security etc. This is extremely useful in - * legacy applications where the codebase is difficult to modify and allows for improvements - * in the application's networking capabilities. + *

An ambassador service can be thought of as an out-of-process proxy that is co-located with + * the client. Similar to the proxy design pattern, the ambassador service provides an interface for + * another remote service. In addition to the interface, the ambassador provides extra functionality + * and features, specifically offloaded common connectivity tasks. This usually consists of + * monitoring, logging, routing, security etc. This is extremely useful in legacy applications where + * the codebase is difficult to modify and allows for improvements in the application's networking + * capabilities. * - * In this example, we will the ({@link ServiceAmbassador}) class represents the ambassador while the + *

In this example, we will the ({@link ServiceAmbassador}) class represents the ambassador while + * the * ({@link RemoteService}) class represents a remote application. - * */ public class App { /** - * Entry point - */ + * Entry point. + */ public static void main(String[] args) { Client host1 = new Client(); Client host2 = new Client(); diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/Client.java b/ambassador/src/main/java/com/iluwatar/ambassador/Client.java index fdadd71d2..60d0e164d 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/Client.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/Client.java @@ -23,12 +23,11 @@ package com.iluwatar.ambassador; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.slf4j.Logger; - /** - * A simple Client + * A simple Client. */ public class Client { diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java index 10da79d0b..349931595 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java @@ -23,12 +23,12 @@ package com.iluwatar.ambassador; +import static java.lang.Thread.sleep; + import com.iluwatar.ambassador.util.RandomProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static java.lang.Thread.sleep; - /** * A remote legacy application represented by a Singleton implementation. */ @@ -55,9 +55,11 @@ public class RemoteService implements RemoteServiceInterface { RemoteService(RandomProvider randomProvider) { this.randomProvider = randomProvider; } + /** - * Remote function takes a value and multiplies it by 10 taking a random amount of time. - * Will sometimes return -1. This imitates connectivity issues a client might have to account for. + * Remote function takes a value and multiplies it by 10 taking a random amount of time. Will + * sometimes return -1. This imitates connectivity issues a client might have to account for. + * * @param value integer value to be multiplied. * @return if waitTime is less than {@link RemoteService#THRESHOLD}, it returns value * 10, * otherwise {@link RemoteServiceInterface#FAILURE}. diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java b/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java index 37b6970b4..4f191977c 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java @@ -23,17 +23,15 @@ package com.iluwatar.ambassador; +import static java.lang.Thread.sleep; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static java.lang.Thread.sleep; - /** - * * ServiceAmbassador provides an interface for a ({@link Client}) to access ({@link RemoteService}). * The interface adds logging, latency testing and usage of the service in a safe way that will not * add stress to the remote service when connectivity issues occur. - * */ public class ServiceAmbassador implements RemoteServiceInterface { @@ -41,7 +39,8 @@ public class ServiceAmbassador implements RemoteServiceInterface { private static final int RETRIES = 3; private static final int DELAY_MS = 3000; - ServiceAmbassador() {} + ServiceAmbassador() { + } @Override public long doRemoteFunction(int value) { diff --git a/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/App.java b/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/App.java index bc33494f7..a85a51ab8 100644 --- a/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/App.java +++ b/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/App.java @@ -23,35 +23,34 @@ package com.iluwatar.async.method.invocation; +import java.util.concurrent.Callable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.Callable; - /** * This application demonstrates the async method invocation pattern. Key parts of the pattern are - * AsyncResult which is an intermediate container for an asynchronously evaluated value, - * AsyncCallback which can be provided to be executed on task completion and AsyncExecutor - * that manages the execution of the async tasks. - *

- * The main method shows example flow of async invocations. The main thread starts multiple tasks with variable - * durations and then continues its own work. When the main thread has done it's job it collects the results of the - * async tasks. Two of the tasks are handled with callbacks, meaning the callbacks are executed immediately when the - * tasks complete. - *

- * Noteworthy difference of thread usage between the async results and callbacks is that the async results are collected - * in the main thread but the callbacks are executed within the worker threads. This should be noted when working with - * thread pools. - *

- * Java provides its own implementations of async method invocation pattern. FutureTask, CompletableFuture and - * ExecutorService are the real world implementations of this pattern. But due to the nature of parallel programming, - * the implementations are not trivial. This example does not take all possible scenarios into account but rather - * provides a simple version that helps to understand the pattern. + * AsyncResult which is an intermediate container for an asynchronously evaluated + * value, AsyncCallback which can be provided to be executed on task completion and + * AsyncExecutor that manages the execution of the async tasks. + * + *

The main method shows example flow of async invocations. The main thread starts multiple + * tasks with variable durations and then continues its own work. When the main thread has done it's + * job it collects the results of the async tasks. Two of the tasks are handled with callbacks, + * meaning the callbacks are executed immediately when the tasks complete. + * + *

Noteworthy difference of thread usage between the async results and callbacks is that the + * async results are collected in the main thread but the callbacks are executed within the worker + * threads. This should be noted when working with thread pools. + * + *

Java provides its own implementations of async method invocation pattern. FutureTask, + * CompletableFuture and ExecutorService are the real world implementations of this pattern. But due + * to the nature of parallel programming, the implementations are not trivial. This example does not + * take all possible scenarios into account but rather provides a simple version that helps to + * understand the pattern. * * @see AsyncResult * @see AsyncCallback * @see AsyncExecutor - * * @see java.util.concurrent.FutureTask * @see java.util.concurrent.CompletableFuture * @see java.util.concurrent.ExecutorService @@ -61,27 +60,29 @@ public class App { private static final Logger LOGGER = LoggerFactory.getLogger(App.class); /** - * Program entry point + * Program entry point. */ public static void main(String[] args) throws Exception { // construct a new executor that will run async tasks AsyncExecutor executor = new ThreadAsyncExecutor(); // start few async tasks with varying processing times, two last with callback handlers - AsyncResult asyncResult1 = executor.startProcess(lazyval(10, 500)); - AsyncResult asyncResult2 = executor.startProcess(lazyval("test", 300)); - AsyncResult asyncResult3 = executor.startProcess(lazyval(50L, 700)); - AsyncResult asyncResult4 = executor.startProcess(lazyval(20, 400), callback("Callback result 4")); - AsyncResult asyncResult5 = executor.startProcess(lazyval("callback", 600), callback("Callback result 5")); + final AsyncResult asyncResult1 = executor.startProcess(lazyval(10, 500)); + final AsyncResult asyncResult2 = executor.startProcess(lazyval("test", 300)); + final AsyncResult asyncResult3 = executor.startProcess(lazyval(50L, 700)); + final AsyncResult asyncResult4 = + executor.startProcess(lazyval(20, 400), callback("Callback result 4")); + final AsyncResult asyncResult5 = + executor.startProcess(lazyval("callback", 600), callback("Callback result 5")); // emulate processing in the current thread while async tasks are running in their own threads Thread.sleep(350); // Oh boy I'm working hard here log("Some hard work done"); // wait for completion of the tasks - Integer result1 = executor.endProcess(asyncResult1); - String result2 = executor.endProcess(asyncResult2); - Long result3 = executor.endProcess(asyncResult3); + final Integer result1 = executor.endProcess(asyncResult1); + final String result2 = executor.endProcess(asyncResult2); + final Long result3 = executor.endProcess(asyncResult3); asyncResult4.await(); asyncResult5.await(); @@ -94,10 +95,8 @@ public class App { /** * Creates a callable that lazily evaluates to given value with artificial delay. * - * @param value - * value to evaluate - * @param delayMillis - * artificial delay in milliseconds + * @param value value to evaluate + * @param delayMillis artificial delay in milliseconds * @return new callable for lazy evaluation */ private static Callable lazyval(T value, long delayMillis) { @@ -111,8 +110,7 @@ public class App { /** * Creates a simple callback that logs the complete status of the async result. * - * @param name - * callback name + * @param name callback name * @return new async callback */ private static AsyncCallback callback(String name) { diff --git a/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/AsyncCallback.java b/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/AsyncCallback.java index 950444fe7..22b36134f 100644 --- a/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/AsyncCallback.java +++ b/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/AsyncCallback.java @@ -26,11 +26,9 @@ package com.iluwatar.async.method.invocation; import java.util.Optional; /** - * - * AsyncCallback interface + * AsyncCallback interface. * - * @param - * + * @param Type of Result */ public interface AsyncCallback { @@ -38,7 +36,7 @@ public interface AsyncCallback { * Complete handler which is executed when async task is completed or fails execution. * * @param value the evaluated value from async task, undefined when execution fails - * @param ex empty value if execution succeeds, some exception if executions fails + * @param ex empty value if execution succeeds, some exception if executions fails */ void onComplete(T value, Optional ex); } diff --git a/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/AsyncExecutor.java b/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/AsyncExecutor.java index 14e30cbeb..819ffd237 100644 --- a/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/AsyncExecutor.java +++ b/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/AsyncExecutor.java @@ -27,9 +27,7 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; /** - * - * AsyncExecutor interface - * + * AsyncExecutor interface. */ public interface AsyncExecutor { @@ -45,7 +43,7 @@ public interface AsyncExecutor { * Starts processing of an async task. Returns immediately with async result. Executes callback * when the task is completed. * - * @param task task to be executed asynchronously + * @param task task to be executed asynchronously * @param callback callback to be executed on task completion * @return async result for the task */ @@ -57,7 +55,7 @@ public interface AsyncExecutor { * * @param asyncResult async result of a task * @return evaluated value of the completed task - * @throws ExecutionException if execution has failed, containing the root cause + * @throws ExecutionException if execution has failed, containing the root cause * @throws InterruptedException if the execution is interrupted */ T endProcess(AsyncResult asyncResult) throws ExecutionException, InterruptedException; diff --git a/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/AsyncResult.java b/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/AsyncResult.java index c3b8467f1..6aaf233b4 100644 --- a/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/AsyncResult.java +++ b/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/AsyncResult.java @@ -26,7 +26,8 @@ package com.iluwatar.async.method.invocation; import java.util.concurrent.ExecutionException; /** - * AsyncResult interface + * AsyncResult interface. + * * @param parameter returned when getValue is invoked */ public interface AsyncResult { @@ -42,7 +43,7 @@ public interface AsyncResult { * Gets the value of completed async task. * * @return evaluated value or throws ExecutionException if execution has failed - * @throws ExecutionException if execution has failed, containing the root cause + * @throws ExecutionException if execution has failed, containing the root cause * @throws IllegalStateException if execution is not completed */ T getValue() throws ExecutionException; diff --git a/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutor.java b/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutor.java index f05b83b4f..ecd28f519 100644 --- a/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutor.java +++ b/async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutor.java @@ -29,13 +29,13 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicInteger; /** - * * Implementation of async executor that creates a new thread for every task. - * */ public class ThreadAsyncExecutor implements AsyncExecutor { - /** Index for thread naming */ + /** + * Index for thread naming. + */ private final AtomicInteger idx = new AtomicInteger(0); @Override @@ -52,12 +52,13 @@ public class ThreadAsyncExecutor implements AsyncExecutor { } catch (Exception ex) { result.setException(ex); } - } , "executor-" + idx.incrementAndGet()).start(); + }, "executor-" + idx.incrementAndGet()).start(); return result; } @Override - public T endProcess(AsyncResult asyncResult) throws ExecutionException, InterruptedException { + public T endProcess(AsyncResult asyncResult) throws ExecutionException, + InterruptedException { if (!asyncResult.isCompleted()) { asyncResult.await(); } @@ -65,8 +66,9 @@ public class ThreadAsyncExecutor implements AsyncExecutor { } /** - * Simple implementation of async result that allows completing it successfully with a value or exceptionally with an - * exception. A really simplified version from its real life cousins FutureTask and CompletableFuture. + * Simple implementation of async result that allows completing it successfully with a value or + * exceptionally with an exception. A really simplified version from its real life cousins + * FutureTask and CompletableFuture. * * @see java.util.concurrent.FutureTask * @see java.util.concurrent.CompletableFuture @@ -90,11 +92,10 @@ public class ThreadAsyncExecutor implements AsyncExecutor { } /** - * Sets the value from successful execution and executes callback if available. Notifies any thread waiting for - * completion. + * Sets the value from successful execution and executes callback if available. Notifies any + * thread waiting for completion. * - * @param value - * value of the evaluated task + * @param value value of the evaluated task */ void setValue(T value) { this.value = value; @@ -106,11 +107,10 @@ public class ThreadAsyncExecutor implements AsyncExecutor { } /** - * Sets the exception from failed execution and executes callback if available. Notifies any thread waiting for - * completion. + * Sets the exception from failed execution and executes callback if available. Notifies any + * thread waiting for completion. * - * @param exception - * exception of the failed task + * @param exception exception of the failed task */ void setException(Exception exception) { this.exception = exception; diff --git a/async-method-invocation/src/test/java/com/iluwatar/async/method/invocation/AppTest.java b/async-method-invocation/src/test/java/com/iluwatar/async/method/invocation/AppTest.java index 769cd66c4..75aebc0df 100644 --- a/async-method-invocation/src/test/java/com/iluwatar/async/method/invocation/AppTest.java +++ b/async-method-invocation/src/test/java/com/iluwatar/async/method/invocation/AppTest.java @@ -26,9 +26,7 @@ package com.iluwatar.async.method.invocation; import org.junit.jupiter.api.Test; /** - * * Application test - * */ public class AppTest { diff --git a/async-method-invocation/src/test/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutorTest.java b/async-method-invocation/src/test/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutorTest.java index d240f3c2e..4d97d60e5 100644 --- a/async-method-invocation/src/test/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutorTest.java +++ b/async-method-invocation/src/test/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutorTest.java @@ -23,30 +23,18 @@ package com.iluwatar.async.method.invocation; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Matchers; +import static java.time.Duration.ofMillis; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.*; +import static org.mockito.internal.verification.VerificationModeFactory.times; import java.util.Optional; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; - -import static java.time.Duration.ofMillis; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTimeout; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.timeout; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.verifyZeroInteractions; -import static org.mockito.Mockito.when; -import static org.mockito.internal.verification.VerificationModeFactory.times; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Matchers; /** * Date: 12/6/15 - 10:49 AM @@ -82,7 +70,8 @@ public class ThreadAsyncExecutorTest { } /** - * Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} + * Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable, + * AsyncCallback)} */ @Test public void testSuccessfulTaskWithCallback() throws Exception { @@ -104,7 +93,8 @@ public class ThreadAsyncExecutorTest { verify(task, times(1)).call(); // ... same for the callback, we expect our object - final ArgumentCaptor> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class); + final ArgumentCaptor> optionalCaptor = + ArgumentCaptor.forClass((Class) Optional.class); verify(callback, times(1)).onComplete(eq(result), optionalCaptor.capture()); final Optional optionalException = optionalCaptor.getValue(); @@ -117,8 +107,8 @@ public class ThreadAsyncExecutorTest { } /** - * Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} when a task takes a while - * to execute + * Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} when a + * task takes a while to execute */ @Test public void testLongRunningTaskWithoutCallback() throws Exception { @@ -158,8 +148,8 @@ public class ThreadAsyncExecutorTest { } /** - * Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when a task - * takes a while to execute + * Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable, + * AsyncCallback)} when a task takes a while to execute */ @Test public void testLongRunningTaskWithCallback() throws Exception { @@ -191,7 +181,8 @@ public class ThreadAsyncExecutorTest { // Our task should only execute once, but it can take a while ... verify(task, timeout(3000).times(1)).call(); - final ArgumentCaptor> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class); + final ArgumentCaptor> optionalCaptor = + ArgumentCaptor.forClass((Class) Optional.class); verify(callback, timeout(3000).times(1)).onComplete(eq(result), optionalCaptor.capture()); final Optional optionalException = optionalCaptor.getValue(); @@ -209,8 +200,9 @@ public class ThreadAsyncExecutorTest { } /** - * Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} when a task takes a while - * to execute, while waiting on the result using {@link ThreadAsyncExecutor#endProcess(AsyncResult)} + * Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} when a + * task takes a while to execute, while waiting on the result using {@link + * ThreadAsyncExecutor#endProcess(AsyncResult)} */ @Test public void testEndProcess() throws Exception { @@ -247,7 +239,8 @@ public class ThreadAsyncExecutorTest { } /** - * Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable)} when the callable is 'null' + * Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable)} when + * the callable is 'null' */ @Test public void testNullTask() throws Exception { @@ -273,8 +266,8 @@ public class ThreadAsyncExecutorTest { } /** - * Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when the - * callable is 'null', but the asynchronous callback is provided + * Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable, + * AsyncCallback)} when the callable is 'null', but the asynchronous callback is provided */ @Test public void testNullTaskWithCallback() throws Exception { @@ -288,7 +281,8 @@ public class ThreadAsyncExecutorTest { asyncResult.await(); // Prevent timing issues, and wait until the result is available assertTrue(asyncResult.isCompleted()); - final ArgumentCaptor> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class); + final ArgumentCaptor> optionalCaptor = + ArgumentCaptor.forClass((Class) Optional.class); verify(callback, times(1)).onComplete(Matchers.isNull(), optionalCaptor.capture()); final Optional optionalException = optionalCaptor.getValue(); @@ -312,8 +306,8 @@ public class ThreadAsyncExecutorTest { } /** - * Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when both - * the callable and the asynchronous callback are 'null' + * Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable, + * AsyncCallback)} when both the callable and the asynchronous callback are 'null' */ @Test public void testNullTaskWithNullCallback() throws Exception { diff --git a/balking/src/main/java/com/iluwatar/balking/App.java b/balking/src/main/java/com/iluwatar/balking/App.java index bb3fb085c..3e64a2990 100644 --- a/balking/src/main/java/com/iluwatar/balking/App.java +++ b/balking/src/main/java/com/iluwatar/balking/App.java @@ -23,23 +23,22 @@ package com.iluwatar.balking; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * In Balking Design Pattern if an object’s method is invoked when it is in an inappropriate state, - * then the method will return without doing anything. Objects that use this pattern are generally only in a - * state that is prone to balking temporarily but for an unknown amount of time + * then the method will return without doing anything. Objects that use this pattern are generally + * only in a state that is prone to balking temporarily but for an unknown amount of time * - * In this example implementation WashingMachine is an object that has two states - * in which it can be: ENABLED and WASHING. If the machine is ENABLED - * the state is changed into WASHING that any other thread can't invoke this action on this and then do the job. - * On the other hand if it have been already washing and any other thread execute wash() - * it can't do that once again and returns doing nothing. + *

In this example implementation WashingMachine is an object that has two states in which it + * can be: ENABLED and WASHING. If the machine is ENABLED the state is changed into WASHING that any + * other thread can't invoke this action on this and then do the job. On the other hand if it have + * been already washing and any other thread execute wash() it can't do that once again and returns + * doing nothing. */ public class App { @@ -47,6 +46,8 @@ public class App { private static final Logger LOGGER = LoggerFactory.getLogger(App.class); /** + * Entry Point. + * * @param args the command line arguments - not used */ public static void main(String... args) { diff --git a/balking/src/main/java/com/iluwatar/balking/WashingMachine.java b/balking/src/main/java/com/iluwatar/balking/WashingMachine.java index 044b1b6e9..c47c96c3a 100644 --- a/balking/src/main/java/com/iluwatar/balking/WashingMachine.java +++ b/balking/src/main/java/com/iluwatar/balking/WashingMachine.java @@ -23,13 +23,12 @@ package com.iluwatar.balking; +import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.TimeUnit; - /** - * Washing machine class + * Washing machine class. */ public class WashingMachine { @@ -38,7 +37,7 @@ public class WashingMachine { private WashingMachineState washingMachineState; /** - * Creates a new instance of WashingMachine + * Creates a new instance of WashingMachine. */ public WashingMachine() { this((interval, timeUnit, task) -> { @@ -52,8 +51,8 @@ public class WashingMachine { } /** - * Creates a new instance of WashingMachine using provided delayProvider. This constructor is used only for - * unit testing purposes. + * Creates a new instance of WashingMachine using provided delayProvider. This constructor is used + * only for unit testing purposes. */ public WashingMachine(DelayProvider delayProvider) { this.delayProvider = delayProvider; @@ -65,17 +64,17 @@ public class WashingMachine { } /** - * Method responsible for washing - * if the object is in appropriate state + * Method responsible for washing if the object is in appropriate state. */ public void wash() { synchronized (this) { - LOGGER.info("{}: Actual machine state: {}", Thread.currentThread().getName(), getWashingMachineState()); - if (washingMachineState == WashingMachineState.WASHING) { + WashingMachineState machineState = getWashingMachineState(); + LOGGER.info("{}: Actual machine state: {}", Thread.currentThread().getName(), machineState); + if (this.washingMachineState == WashingMachineState.WASHING) { LOGGER.error("ERROR: Cannot wash if the machine has been already washing!"); return; } - washingMachineState = WashingMachineState.WASHING; + this.washingMachineState = WashingMachineState.WASHING; } LOGGER.info("{}: Doing the washing", Thread.currentThread().getName()); @@ -83,8 +82,7 @@ public class WashingMachine { } /** - * Method responsible of ending the washing - * by changing machine state + * Method responsible of ending the washing by changing machine state. */ public synchronized void endOfWashing() { washingMachineState = WashingMachineState.ENABLED; diff --git a/balking/src/main/java/com/iluwatar/balking/WashingMachineState.java b/balking/src/main/java/com/iluwatar/balking/WashingMachineState.java index 0799d3fd8..664a4c0c9 100644 --- a/balking/src/main/java/com/iluwatar/balking/WashingMachineState.java +++ b/balking/src/main/java/com/iluwatar/balking/WashingMachineState.java @@ -24,10 +24,9 @@ package com.iluwatar.balking; /** - * WashingMachineState enum describes in which state machine is, - * it can be enabled and ready to work as well as during washing + * WashingMachineState enum describes in which state machine is, it can be enabled and ready to work + * as well as during washing. */ - public enum WashingMachineState { ENABLED, WASHING } diff --git a/balking/src/test/java/com/iluwatar/balking/WashingMachineTest.java b/balking/src/test/java/com/iluwatar/balking/WashingMachineTest.java index 8348f9256..7c6bd73ec 100644 --- a/balking/src/test/java/com/iluwatar/balking/WashingMachineTest.java +++ b/balking/src/test/java/com/iluwatar/balking/WashingMachineTest.java @@ -23,11 +23,10 @@ package com.iluwatar.balking; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.concurrent.TimeUnit; - -import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; /** * Tests for {@link WashingMachine} diff --git a/bridge/src/main/java/com/iluwatar/bridge/App.java b/bridge/src/main/java/com/iluwatar/bridge/App.java index 195f463cf..74104257e 100644 --- a/bridge/src/main/java/com/iluwatar/bridge/App.java +++ b/bridge/src/main/java/com/iluwatar/bridge/App.java @@ -27,25 +27,25 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * - * Composition over inheritance. The Bridge pattern can also be thought of as two layers of abstraction. - * With Bridge, you can decouple an abstraction from its implementation so that the two can vary independently. - *

- * In Bridge pattern both abstraction ({@link Weapon}) and implementation ( - * {@link Enchantment}) have their own class hierarchies. The interface of the implementations - * can be changed without affecting the clients. - *

- * In this example we have two class hierarchies. One of weapons and another one of enchantments. We can easily - * combine any weapon with any enchantment using composition instead of creating deep class hierarchy. - * + * Composition over inheritance. The Bridge pattern can also be thought of as two layers of + * abstraction. With Bridge, you can decouple an abstraction from its implementation so that the two + * can vary independently. + * + *

In Bridge pattern both abstraction ({@link Weapon}) and implementation ( {@link Enchantment}) + * have their own class hierarchies. The interface of the implementations can be changed without + * affecting the clients. + * + *

In this example we have two class hierarchies. One of weapons and another one of + * enchantments. We can easily combine any weapon with any enchantment using composition instead of + * creating deep class hierarchy. */ public class App { private static final Logger LOGGER = LoggerFactory.getLogger(App.class); /** - * Program entry point - * + * Program entry point. + * * @param args command line args */ public static void main(String[] args) { diff --git a/bridge/src/main/java/com/iluwatar/bridge/Enchantment.java b/bridge/src/main/java/com/iluwatar/bridge/Enchantment.java index 22acac8f9..8388fe91e 100644 --- a/bridge/src/main/java/com/iluwatar/bridge/Enchantment.java +++ b/bridge/src/main/java/com/iluwatar/bridge/Enchantment.java @@ -24,9 +24,7 @@ package com.iluwatar.bridge; /** - * - * Enchantment - * + * Enchantment. */ public interface Enchantment { diff --git a/bridge/src/main/java/com/iluwatar/bridge/FlyingEnchantment.java b/bridge/src/main/java/com/iluwatar/bridge/FlyingEnchantment.java index 14abe4c63..772456b88 100644 --- a/bridge/src/main/java/com/iluwatar/bridge/FlyingEnchantment.java +++ b/bridge/src/main/java/com/iluwatar/bridge/FlyingEnchantment.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * - * FlyingEnchantment - * + * FlyingEnchantment. */ public class FlyingEnchantment implements Enchantment { diff --git a/bridge/src/main/java/com/iluwatar/bridge/Hammer.java b/bridge/src/main/java/com/iluwatar/bridge/Hammer.java index 47f60613d..ffab542cb 100644 --- a/bridge/src/main/java/com/iluwatar/bridge/Hammer.java +++ b/bridge/src/main/java/com/iluwatar/bridge/Hammer.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * - * Hammer - * + * Hammer. */ public class Hammer implements Weapon { diff --git a/bridge/src/main/java/com/iluwatar/bridge/SoulEatingEnchantment.java b/bridge/src/main/java/com/iluwatar/bridge/SoulEatingEnchantment.java index 3655c7e97..ede98d2cb 100644 --- a/bridge/src/main/java/com/iluwatar/bridge/SoulEatingEnchantment.java +++ b/bridge/src/main/java/com/iluwatar/bridge/SoulEatingEnchantment.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * - * SoulEatingEnchantment - * + * SoulEatingEnchantment. */ public class SoulEatingEnchantment implements Enchantment { diff --git a/bridge/src/main/java/com/iluwatar/bridge/Sword.java b/bridge/src/main/java/com/iluwatar/bridge/Sword.java index ab6f4ab7f..71f87a55d 100644 --- a/bridge/src/main/java/com/iluwatar/bridge/Sword.java +++ b/bridge/src/main/java/com/iluwatar/bridge/Sword.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * - * Sword - * + * Sword. */ public class Sword implements Weapon { diff --git a/bridge/src/main/java/com/iluwatar/bridge/Weapon.java b/bridge/src/main/java/com/iluwatar/bridge/Weapon.java index b264dddca..76272332e 100644 --- a/bridge/src/main/java/com/iluwatar/bridge/Weapon.java +++ b/bridge/src/main/java/com/iluwatar/bridge/Weapon.java @@ -24,9 +24,7 @@ package com.iluwatar.bridge; /** - * - * Weapon - * + * Weapon. */ public interface Weapon { diff --git a/builder/src/main/java/com/iluwatar/builder/App.java b/builder/src/main/java/com/iluwatar/builder/App.java index f57e4d42f..ae29ee367 100644 --- a/builder/src/main/java/com/iluwatar/builder/App.java +++ b/builder/src/main/java/com/iluwatar/builder/App.java @@ -28,36 +28,33 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * * The intention of the Builder pattern is to find a solution to the telescoping constructor * anti-pattern. The telescoping constructor anti-pattern occurs when the increase of object * constructor parameter combination leads to an exponential list of constructors. Instead of using * numerous constructors, the builder pattern uses another object, a builder, that receives each * initialization parameter step by step and then returns the resulting constructed object at once. - *

- * The Builder pattern has another benefit. It can be used for objects that contain flat data (html - * code, SQL query, X.509 certificate...), that is to say, data that can't be easily edited. This - * type of data cannot be edited step by step and must be edited at once. The best way to construct - * such an object is to use a builder class. - *

- * In this example we have the Builder pattern variation as described by Joshua Bloch in Effective - * Java 2nd Edition. - *

- * We want to build {@link Hero} objects, but its construction is complex because of the many - * parameters needed. To aid the user we introduce {@link Builder} class. {@link Hero.Builder} - * takes the minimum parameters to build {@link Hero} object in its constructor. After that - * additional configuration for the {@link Hero} object can be done using the fluent - * {@link Builder} interface. When configuration is ready the build method is called to receive - * the final {@link Hero} object. - * + * + *

The Builder pattern has another benefit. It can be used for objects that contain flat data + * (html code, SQL query, X.509 certificate...), that is to say, data that can't be easily edited. + * This type of data cannot be edited step by step and must be edited at once. The best way to + * construct such an object is to use a builder class. + * + *

In this example we have the Builder pattern variation as described by Joshua Bloch in + * Effective Java 2nd Edition. + * + *

We want to build {@link Hero} objects, but its construction is complex because of the many + * parameters needed. To aid the user we introduce {@link Builder} class. {@link Hero.Builder} takes + * the minimum parameters to build {@link Hero} object in its constructor. After that additional + * configuration for the {@link Hero} object can be done using the fluent {@link Builder} interface. + * When configuration is ready the build method is called to receive the final {@link Hero} object. */ public class App { private static final Logger LOGGER = LoggerFactory.getLogger(App.class); /** - * Program entry point - * + * Program entry point. + * * @param args command line args */ public static void main(String[] args) { diff --git a/builder/src/main/java/com/iluwatar/builder/Armor.java b/builder/src/main/java/com/iluwatar/builder/Armor.java index 8cf57a361..2ab9bf831 100644 --- a/builder/src/main/java/com/iluwatar/builder/Armor.java +++ b/builder/src/main/java/com/iluwatar/builder/Armor.java @@ -24,9 +24,7 @@ package com.iluwatar.builder; /** - * - * Armor enumeration - * + * Armor enumeration. */ public enum Armor { diff --git a/builder/src/main/java/com/iluwatar/builder/HairColor.java b/builder/src/main/java/com/iluwatar/builder/HairColor.java index f94de3556..1beccff5e 100644 --- a/builder/src/main/java/com/iluwatar/builder/HairColor.java +++ b/builder/src/main/java/com/iluwatar/builder/HairColor.java @@ -24,9 +24,7 @@ package com.iluwatar.builder; /** - * - * HairColor enumeration - * + * HairColor enumeration. */ public enum HairColor { diff --git a/builder/src/main/java/com/iluwatar/builder/HairType.java b/builder/src/main/java/com/iluwatar/builder/HairType.java index 6eece1e37..3d16eb4d7 100644 --- a/builder/src/main/java/com/iluwatar/builder/HairType.java +++ b/builder/src/main/java/com/iluwatar/builder/HairType.java @@ -24,9 +24,7 @@ package com.iluwatar.builder; /** - * - * HairType enumeration - * + * HairType enumeration. */ public enum HairType { diff --git a/builder/src/main/java/com/iluwatar/builder/Hero.java b/builder/src/main/java/com/iluwatar/builder/Hero.java index a8f285b66..b33ce1007 100644 --- a/builder/src/main/java/com/iluwatar/builder/Hero.java +++ b/builder/src/main/java/com/iluwatar/builder/Hero.java @@ -24,9 +24,7 @@ package com.iluwatar.builder; /** - * * Hero, the class with many parameters. - * */ public final class Hero { @@ -75,9 +73,9 @@ public final class Hero { StringBuilder sb = new StringBuilder(); sb.append("This is a ") - .append(profession) - .append(" named ") - .append(name); + .append(profession) + .append(" named ") + .append(name); if (hairColor != null || hairType != null) { sb.append(" with "); if (hairColor != null) { @@ -99,9 +97,7 @@ public final class Hero { } /** - * * The builder class. - * */ public static class Builder { @@ -113,7 +109,7 @@ public final class Hero { private Weapon weapon; /** - * Constructor + * Constructor. */ public Builder(Profession profession, String name) { if (profession == null || name == null) { diff --git a/builder/src/main/java/com/iluwatar/builder/Profession.java b/builder/src/main/java/com/iluwatar/builder/Profession.java index 1e22a1c67..23b1ac220 100644 --- a/builder/src/main/java/com/iluwatar/builder/Profession.java +++ b/builder/src/main/java/com/iluwatar/builder/Profession.java @@ -24,9 +24,7 @@ package com.iluwatar.builder; /** - * - * Profession enumeration - * + * Profession enumeration. */ public enum Profession { diff --git a/builder/src/main/java/com/iluwatar/builder/Weapon.java b/builder/src/main/java/com/iluwatar/builder/Weapon.java index 51ddeafbc..4ca78b95f 100644 --- a/builder/src/main/java/com/iluwatar/builder/Weapon.java +++ b/builder/src/main/java/com/iluwatar/builder/Weapon.java @@ -24,9 +24,7 @@ package com.iluwatar.builder; /** - * - * Weapon enumeration - * + * Weapon enumeration. */ public enum Weapon {