Skip to content

Commit

Permalink
Adjust checkstyle rules. Make checkstyle fail the build when violatio…
Browse files Browse the repository at this point in the history
…ns are found. Correct all current checkstyle violations.
  • Loading branch information
iluwatar committed Dec 25, 2015
1 parent 9fbb085 commit cec9a99
Show file tree
Hide file tree
Showing 167 changed files with 1,210 additions and 937 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package com.iluwatar.abstractfactory;


/**
*
* The Abstract Factory pattern provides a way to encapsulate a group of individual factories that
* have a common theme without specifying their concrete classes. In normal usage, the client
* software creates a concrete implementation of the abstract factory and then uses the generic
* interface of the factory to create the concrete objects that are part of the theme. The client
* does not know (or care) which concrete objects it gets from each of these internal factories,
* since it uses only the generic interfaces of their products. This pattern separates the details
* of implementation of a set of objects from their general usage and relies on object composition,
* as object creation is implemented in methods exposed in the factory interface.
* The Abstract Factory pattern provides a way to encapsulate a group of individual factories that have a common theme
* without specifying their concrete classes. In normal usage, the client software creates a concrete implementation of
* the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part
* of the theme. The client does not know (or care) which concrete objects it gets from each of these internal
* factories, since it uses only the generic interfaces of their products. This pattern separates the details of
* implementation of a set of objects from their general usage and relies on object composition, as object creation is
* implemented in methods exposed in the factory interface.
* <p>
* The essence of the Abstract Factory pattern is a factory interface ({@link KingdomFactory}) and
* its implementations ({@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses both
* concrete implementations to create a king, a castle and an army.
* The essence of the Abstract Factory pattern is a factory interface ({@link KingdomFactory}) and its implementations (
* {@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses both concrete implementations to create a
* king, a castle and an army.
*
*/
public class App {
Expand All @@ -23,11 +21,8 @@ public class App {
private Castle castle;
private Army army;


/**
* Creates kingdom
*
* @param factory
*/
public void createKingdom(final KingdomFactory factory) {
setKing(factory.createKing());
Expand All @@ -47,21 +42,17 @@ King getKing(final KingdomFactory factory) {
return factory.createKing();
}

Castle getCastle(final KingdomFactory factory) {
return factory.createCastle();
}

Army getArmy(final KingdomFactory factory) {
return factory.createArmy();
}

public King getKing() {
return king;
}

private void setKing(final King king) {
this.king = king;
}

Castle getCastle(final KingdomFactory factory) {
return factory.createCastle();
}

public Castle getCastle() {
return castle;
Expand All @@ -70,6 +61,10 @@ public Castle getCastle() {
private void setCastle(final Castle castle) {
this.castle = castle;
}

Army getArmy(final KingdomFactory factory) {
return factory.createArmy();
}

public Army getArmy() {
return army;
Expand All @@ -79,32 +74,32 @@ private void setArmy(final Army army) {
this.army = army;
}


/**
* Program entry point
*
* @param args command line args
* @param args
* command line args
*/
public static void main(String[] args) {
App app = new App();
System.out.println("Elf Kingdom");
KingdomFactory elfKingdomFactory;
elfKingdomFactory = app.getElfKingdomFactory();
app.createKingdom(elfKingdomFactory);
System.out.println(app.getArmy().getDescription());
System.out.println(app.getCastle().getDescription());
System.out.println(app.getKing().getDescription());
System.out.println("\nOrc Kingdom");
KingdomFactory orcKingdomFactory;
orcKingdomFactory = app.getOrcKingdomFactory();
app.createKingdom(orcKingdomFactory);
System.out.println(app.getArmy().getDescription());
System.out.println(app.getCastle().getDescription());
System.out.println(app.getKing().getDescription());

App app = new App();

System.out.println("Elf Kingdom");
KingdomFactory elfKingdomFactory;
elfKingdomFactory = app.getElfKingdomFactory();
app.createKingdom(elfKingdomFactory);
System.out.println(app.getArmy().getDescription());
System.out.println(app.getCastle().getDescription());
System.out.println(app.getKing().getDescription());

System.out.println("\nOrc Kingdom");
KingdomFactory orcKingdomFactory;
orcKingdomFactory = app.getOrcKingdomFactory();
app.createKingdom(orcKingdomFactory);
System.out.println(app.getArmy().getDescription());
System.out.println(app.getCastle().getDescription());
System.out.println(app.getKing().getDescription());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@

/**
* This application demonstrates the async method invocation pattern. Key parts of the pattern are
* <code>AsyncResult</code> which is an intermediate container for an asynchronously evaluated
* value, <code>AsyncCallback</code> which can be provided to be executed on task completion and
* <code>AsyncExecutor</code> that manages the execution of the async tasks.
* <code>AsyncResult</code> which is an intermediate container for an asynchronously evaluated value,
* <code>AsyncCallback</code> which can be provided to be executed on task completion and <code>AsyncExecutor</code>
* that manages the execution of the async tasks.
* <p>
* 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.
* 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.
* <p>
* 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.
* 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.
* <p>
* 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.
* 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
Expand All @@ -33,6 +32,9 @@
*/
public class App {

/**
* Program entry point
*/
public static void main(String[] args) throws Exception {
// construct a new executor that will run async tasks
AsyncExecutor executor = new ThreadAsyncExecutor();
Expand All @@ -41,10 +43,8 @@ public static void main(String[] args) throws Exception {
AsyncResult<Integer> asyncResult1 = executor.startProcess(lazyval(10, 500));
AsyncResult<String> asyncResult2 = executor.startProcess(lazyval("test", 300));
AsyncResult<Long> asyncResult3 = executor.startProcess(lazyval(50L, 700));
AsyncResult<Integer> asyncResult4 =
executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
AsyncResult<String> asyncResult5 =
executor.startProcess(lazyval("callback", 600), callback("Callback result 5"));
AsyncResult<Integer> asyncResult4 = executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
AsyncResult<String> 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
Expand All @@ -66,8 +66,10 @@ public static void main(String[] args) throws Exception {
/**
* 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 <T> Callable<T> lazyval(T value, long delayMillis) {
Expand All @@ -81,7 +83,8 @@ private static <T> Callable<T> lazyval(T value, long delayMillis) {
/**
* 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 <T> AsyncCallback<T> callback(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
/**
*
* AsyncResult interface
*
* @param <T>
*/
public interface AsyncResult<T> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ public <T> AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callba
} catch (Exception ex) {
result.setException(ex);
}
}, "executor-" + idx.incrementAndGet()).start();
} , "executor-" + idx.incrementAndGet()).start();
return result;
}

@Override
public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException,
InterruptedException {
public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException {
if (asyncResult.isCompleted()) {
return asyncResult.getValue();
} else {
Expand All @@ -45,9 +44,8 @@ public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException,
}

/**
* 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
Expand All @@ -71,10 +69,11 @@ private static class CompletableResult<T> implements AsyncResult<T> {
}

/**
* 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;
Expand All @@ -86,10 +85,11 @@ void setValue(T value) {
}

/**
* 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;
Expand Down
Loading

0 comments on commit cec9a99

Please sign in to comment.