From b9b6777d15a8dff0bc304888bc1c122a74cb89c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilkka=20Sepp=C3=A4l=C3=A4?= Date: Tue, 1 Sep 2020 19:55:22 +0300 Subject: [PATCH] Update README.md --- private-class-data/README.md | 5 ++- promise/README.md | 41 ++++++++++--------- .../main/java/com/iluwatar/promise/App.java | 2 +- .../java/com/iluwatar/promise/Promise.java | 2 +- .../com/iluwatar/promise/PromiseSupport.java | 3 +- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/private-class-data/README.md b/private-class-data/README.md index d4788d2dd..5d0e6eb51 100644 --- a/private-class-data/README.md +++ b/private-class-data/README.md @@ -9,6 +9,7 @@ tags: --- ## Intent + Private Class Data design pattern seeks to reduce exposure of attributes by limiting their visibility. It reduces the number of class attributes by encapsulating them in single Data object. @@ -124,9 +125,11 @@ immutableStew.mix(); // Mixing the immutable stew we find: 2 potatoes, 4 carrot ``` ## Class diagram + ![alt text](./etc/private-class-data.png "Private Class Data") ## Applicability + Use the Private Class Data pattern when -* You want to prevent write access to class data members +* You want to prevent write access to class data members. diff --git a/promise/README.md b/promise/README.md index 993d29a9f..73dce13ec 100644 --- a/promise/README.md +++ b/promise/README.md @@ -14,23 +14,25 @@ CompletableFuture ## Intent -A Promise represents a proxy for a value not necessarily known when the promise is created. It allows you to associate -dependent promises to an asynchronous action's eventual success value or failure reason. Promises are a way to write -async code that still appears as though it is executing in a synchronous way. +A Promise represents a proxy for a value not necessarily known when the promise is created. It +allows you to associate dependent promises to an asynchronous action's eventual success value or +failure reason. Promises are a way to write async code that still appears as though it is executing +in a synchronous way. ## Explanation -The Promise object is used for asynchronous computations. A Promise represents an operation that hasn't completed yet, -but is expected in the future. +The Promise object is used for asynchronous computations. A Promise represents an operation that +hasn't completed yet, but is expected in the future. Promises provide a few advantages over callback objects: - * Functional composition and error handling - * Prevents callback hell and provides callback aggregation + * Functional composition and error handling. + * Prevents callback hell and provides callback aggregation. Real world example -> We are developing a software solution that downloads files and calculates the number of lines and character -frequencies in those files. Promise is an ideal solution to make the code concise and easy to understand. +> We are developing a software solution that downloads files and calculates the number of lines and +> character frequencies in those files. Promise is an ideal solution to make the code concise and +> easy to understand. In plain words @@ -38,14 +40,15 @@ In plain words Wikipedia says -> In computer science, future, promise, delay, and deferred refer to constructs used for synchronizing program -execution in some concurrent programming languages. They describe an object that acts as a proxy for a result that is -initially unknown, usually because the computation of its value is not yet complete. +> In computer science, future, promise, delay, and deferred refer to constructs used for +> synchronizing program execution in some concurrent programming languages. They describe an object +> that acts as a proxy for a result that is initially unknown, usually because the computation of +> its value is not yet complete. **Programmatic Example** -In the example a file is downloaded and its line count is calculated. The calculated line count is then consumed and -printed on console. +In the example a file is downloaded and its line count is calculated. The calculated line count is +then consumed and printed on console. Let's first introduce a support class we need for implementation. Here's `PromiseSupport`. @@ -195,7 +198,7 @@ public class Promise extends PromiseSupport { public Promise thenApply(Function func) { Promise dest = new Promise<>(); - fulfillmentAction = new TransformAction(this, dest, func); + fulfillmentAction = new TransformAction<>(this, dest, func); return dest; } @@ -246,8 +249,8 @@ public class Promise extends PromiseSupport { } ``` -Now we can show the full example in action. Here's how to download and count the number of lines in a file using -`Promise`. +Now we can show the full example in action. Here's how to download and count the number of lines in +a file using `Promise`. ```java countLines().thenAccept( @@ -280,8 +283,8 @@ Now we can show the full example in action. Here's how to download and count the ## Applicability -Promise pattern is applicable in concurrent programming when some work needs to be done asynchronously -and: +Promise pattern is applicable in concurrent programming when some work needs to be done +asynchronously and: * Code maintainability and readability suffers due to callback hell. * You need to compose promises and need better error handling for asynchronous tasks. diff --git a/promise/src/main/java/com/iluwatar/promise/App.java b/promise/src/main/java/com/iluwatar/promise/App.java index fb2f14bd5..6c0921936 100644 --- a/promise/src/main/java/com/iluwatar/promise/App.java +++ b/promise/src/main/java/com/iluwatar/promise/App.java @@ -81,7 +81,7 @@ public class App { * @throws InterruptedException if main thread is interrupted. * @throws ExecutionException if an execution error occurs. */ - public static void main(String[] args) throws InterruptedException, ExecutionException { + public static void main(String[] args) throws InterruptedException { var app = new App(); try { app.promiseUsage(); diff --git a/promise/src/main/java/com/iluwatar/promise/Promise.java b/promise/src/main/java/com/iluwatar/promise/Promise.java index e81cccb58..5b79ebcd6 100644 --- a/promise/src/main/java/com/iluwatar/promise/Promise.java +++ b/promise/src/main/java/com/iluwatar/promise/Promise.java @@ -141,7 +141,7 @@ public class Promise extends PromiseSupport { */ public Promise thenApply(Function func) { Promise dest = new Promise<>(); - fulfillmentAction = new TransformAction(this, dest, func); + fulfillmentAction = new TransformAction<>(this, dest, func); return dest; } diff --git a/promise/src/main/java/com/iluwatar/promise/PromiseSupport.java b/promise/src/main/java/com/iluwatar/promise/PromiseSupport.java index 7a12f0a0f..01e9926a0 100644 --- a/promise/src/main/java/com/iluwatar/promise/PromiseSupport.java +++ b/promise/src/main/java/com/iluwatar/promise/PromiseSupport.java @@ -26,7 +26,6 @@ package com.iluwatar.promise; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -114,4 +113,4 @@ class PromiseSupport implements Future { } throw new ExecutionException(exception); } -} \ No newline at end of file +}