Some refactoring, added javadocs

This commit is contained in:
Narendra Pathai 2016-08-29 00:16:36 +05:30
parent 95cf9fe367
commit 483c61a82a
4 changed files with 175 additions and 94 deletions

View File

@ -29,35 +29,45 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
/** /**
* *
* <p>The Promise object is used for asynchronous computations. A Promise represents an operation that * The Promise object is used for asynchronous computations. A Promise represents an operation
* hasn't completed yet, but is expected in the future. * that hasn't completed yet, but is expected in the future.
* *
* <p>A Promise represents a proxy for a value not necessarily known when the promise is created. It * <p>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 * allows you to associate dependent promises to an asynchronous action's eventual success value or
* failure reason. This lets asynchronous methods return values like synchronous methods: instead of the final * failure reason. This lets asynchronous methods return values like synchronous methods: instead
* value, the asynchronous method returns a promise of having a value at some point in the future. * of the final value, the asynchronous method returns a promise of having a value at some point
* * in the future.
*
* <p>Promises provide a few advantages over callback objects: * <p>Promises provide a few advantages over callback objects:
* <ul> * <ul>
* <li> Functional composition and error handling * <li> Functional composition and error handling
* <li> Prevents callback hell and provides callback aggregation * <li> Prevents callback hell and provides callback aggregation
* </ul> * </ul>
* *
* <p> * <p>
* In this application the usage of promise is demonstrated with two examples:
* <ul>
* <li>Count Lines: In this example a file is downloaded and its line count is calculated.
* The calculated line count is then consumed and printed on console.
* <li>Lowest Character Frequency: In this example a file is downloaded and its lowest frequency
* character is found and printed on console. This happens via a chain of promises, we start with
* a file download promise, then a promise of character frequency, then a promise of lowest frequency
* character which is finally consumed and result is printed on console.
* </ul>
* *
* @see CompletableFuture * @see CompletableFuture
*/ */
public class App { public class App {
private static final String URL = "https://raw.githubusercontent.com/iluwatar/java-design-patterns/Promise/promise/README.md"; private static final String DEFAULT_URL = "https://raw.githubusercontent.com/iluwatar/java-design-patterns/Promise/promise/README.md";
private ExecutorService executor; private ExecutorService executor;
private CountDownLatch canStop = new CountDownLatch(2); private CountDownLatch stopLatch = new CountDownLatch(2);
private App() { private App() {
executor = Executors.newFixedThreadPool(2); executor = Executors.newFixedThreadPool(2);
} }
/** /**
* Program entry point * Program entry point
* @param args arguments * @param args arguments
@ -67,28 +77,25 @@ public class App {
public static void main(String[] args) throws InterruptedException, ExecutionException { public static void main(String[] args) throws InterruptedException, ExecutionException {
App app = new App(); App app = new App();
try { try {
app.run(); app.promiseUsage();
} finally { } finally {
app.stop(); app.stop();
} }
} }
private void run() throws InterruptedException, ExecutionException { private void promiseUsage() {
promiseUsage(); calculateLineCount();
calculateLowestFrequencyChar();
} }
private void promiseUsage() { /*
* Calculate the lowest frequency character and when that promise is fulfilled,
countLines() * consume the result in a Consumer<Character>
.then( */
count -> { private void calculateLowestFrequencyChar() {
System.out.println("Line count is: " + count); lowestFrequencyChar()
taskCompleted(); .thenAccept(
}
);
lowestCharFrequency()
.then(
charFrequency -> { charFrequency -> {
System.out.println("Char with lowest frequency is: " + charFrequency); System.out.println("Char with lowest frequency is: " + charFrequency);
taskCompleted(); taskCompleted();
@ -96,49 +103,73 @@ public class App {
); );
} }
private Promise<Character> lowestCharFrequency() { /*
return characterFrequency() * Calculate the line count and when that promise is fulfilled, consume the result
.then( * in a Consumer<Integer>
charFrequency -> { */
return Utility.lowestFrequencyChar(charFrequency).orElse(null); private void calculateLineCount() {
} countLines()
); .thenAccept(
} count -> {
System.out.println("Line count is: " + count);
private Promise<Map<Character, Integer>> characterFrequency() { taskCompleted();
return download(URL)
.then(
fileLocation -> {
return Utility.characterFrequency(fileLocation);
} }
); );
} }
private Promise<Integer> countLines() { /*
return download(URL) * Calculate the character frequency of a file and when that promise is fulfilled,
.then( * then promise to apply function to calculate lowest character frequency.
fileLocation -> { */
return Utility.countLines(fileLocation); private Promise<Character> lowestFrequencyChar() {
} return characterFrequency()
); .thenApply(Utility::lowestFrequencyChar);
} }
/*
* Download the file at DEFAULT_URL and when that promise is fulfilled,
* then promise to apply function to calculate character frequency.
*/
private Promise<Map<Character, Integer>> characterFrequency() {
return download(DEFAULT_URL)
.thenApply(Utility::characterFrequency);
}
/*
* Download the file at DEFAULT_URL and when that promise is fulfilled,
* then promise to apply function to count lines in that file.
*/
private Promise<Integer> countLines() {
return download(DEFAULT_URL)
.thenApply(Utility::countLines);
}
/*
* Return a promise to provide the local absolute path of the file downloaded in background.
* This is an async method and does not wait until the file is downloaded.
*/
private Promise<String> download(String urlString) { private Promise<String> download(String urlString) {
Promise<String> downloadPromise = new Promise<String>() Promise<String> downloadPromise = new Promise<String>()
.fulfillInAsync( .fulfillInAsync(
() -> { () -> {
return Utility.downloadFile(urlString); return Utility.downloadFile(urlString);
}, executor); }, executor)
.onError(
throwable -> {
throwable.printStackTrace();
taskCompleted();
}
);
return downloadPromise; return downloadPromise;
} }
private void stop() throws InterruptedException { private void stop() throws InterruptedException {
canStop.await(); stopLatch.await();
executor.shutdownNow(); executor.shutdownNow();
} }
private void taskCompleted() { private void taskCompleted() {
canStop.countDown(); stopLatch.countDown();
} }
} }

View File

@ -36,6 +36,7 @@ import java.util.function.Function;
public class Promise<T> extends PromiseSupport<T> { public class Promise<T> extends PromiseSupport<T> {
private Runnable fulfillmentAction; private Runnable fulfillmentAction;
private Consumer<? super Throwable> exceptionHandler;
/** /**
* Creates a promise that will be fulfilled in future. * Creates a promise that will be fulfilled in future.
@ -61,9 +62,17 @@ public class Promise<T> extends PromiseSupport<T> {
@Override @Override
public void fulfillExceptionally(Exception exception) { public void fulfillExceptionally(Exception exception) {
super.fulfillExceptionally(exception); super.fulfillExceptionally(exception);
handleException(exception);
postFulfillment(); postFulfillment();
} }
private void handleException(Exception exception) {
if (exceptionHandler == null) {
return;
}
exceptionHandler.accept(exception);
}
private void postFulfillment() { private void postFulfillment() {
if (fulfillmentAction == null) { if (fulfillmentAction == null) {
return; return;
@ -83,8 +92,8 @@ public class Promise<T> extends PromiseSupport<T> {
executor.execute(() -> { executor.execute(() -> {
try { try {
fulfill(task.call()); fulfill(task.call());
} catch (Exception e) { } catch (Exception ex) {
fulfillExceptionally(e); fulfillExceptionally(ex);
} }
}); });
return this; return this;
@ -96,11 +105,22 @@ public class Promise<T> extends PromiseSupport<T> {
* @param action action to be executed. * @param action action to be executed.
* @return a new promise. * @return a new promise.
*/ */
public Promise<Void> then(Consumer<? super T> action) { public Promise<Void> thenAccept(Consumer<? super T> action) {
Promise<Void> dest = new Promise<>(); Promise<Void> dest = new Promise<>();
fulfillmentAction = new ConsumeAction(this, dest, action); fulfillmentAction = new ConsumeAction(this, dest, action);
return dest; return dest;
} }
/**
* Set the exception handler on this promise.
* @param exceptionHandler a consumer that will handle the exception occurred while fulfilling
* the promise.
* @return this
*/
public Promise<T> onError(Consumer<? super Throwable> exceptionHandler) {
this.exceptionHandler = exceptionHandler;
return this;
}
/** /**
* Returns a new promise that, when this promise is fulfilled normally, is fulfilled with * Returns a new promise that, when this promise is fulfilled normally, is fulfilled with
@ -108,7 +128,7 @@ public class Promise<T> extends PromiseSupport<T> {
* @param func function to be executed. * @param func function to be executed.
* @return a new promise. * @return a new promise.
*/ */
public <V> Promise<V> then(Function<? super T, V> func) { public <V> Promise<V> thenApply(Function<? super T, V> func) {
Promise<V> dest = new Promise<>(); Promise<V> dest = new Promise<>();
fulfillmentAction = new TransformAction<V>(this, dest, func); fulfillmentAction = new TransformAction<V>(this, dest, func);
return dest; return dest;
@ -135,8 +155,8 @@ public class Promise<T> extends PromiseSupport<T> {
try { try {
action.accept(src.get()); action.accept(src.get());
dest.fulfill(null); dest.fulfill(null);
} catch (Throwable e) { } catch (Throwable throwable) {
dest.fulfillExceptionally((Exception) e.getCause()); dest.fulfillExceptionally((Exception) throwable.getCause());
} }
} }
} }
@ -162,8 +182,8 @@ public class Promise<T> extends PromiseSupport<T> {
try { try {
V result = func.apply(src.get()); V result = func.apply(src.get());
dest.fulfill(result); dest.fulfill(result);
} catch (Throwable e) { } catch (Throwable throwable) {
dest.fulfillExceptionally((Exception) e.getCause()); dest.fulfillExceptionally((Exception) throwable.getCause());
} }
} }
} }

View File

@ -12,15 +12,19 @@ import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Map.Entry; import java.util.Map.Entry;
public class Utility { public class Utility {
/**
* Calculates character frequency of the file provided.
* @param fileLocation location of the file.
* @return a map of character to its frequency, an empty map if file does not exist.
*/
public static Map<Character, Integer> characterFrequency(String fileLocation) { public static Map<Character, Integer> characterFrequency(String fileLocation) {
Map<Character, Integer> characterToFrequency = new HashMap<>(); Map<Character, Integer> characterToFrequency = new HashMap<>();
try (Reader reader = new FileReader(fileLocation); try (Reader reader = new FileReader(fileLocation);
BufferedReader bufferedReader = new BufferedReader(reader);) { BufferedReader bufferedReader = new BufferedReader(reader)) {
for (String line; (line = bufferedReader.readLine()) != null;) { for (String line; (line = bufferedReader.readLine()) != null;) {
for (char c : line.toCharArray()) { for (char c : line.toCharArray()) {
if (!characterToFrequency.containsKey(c)) { if (!characterToFrequency.containsKey(c)) {
@ -35,33 +39,35 @@ public class Utility {
} }
return characterToFrequency; return characterToFrequency;
} }
public static Optional<Character> lowestFrequencyChar(Map<Character, Integer> charFrequency) { /**
Optional<Character> lowestFrequencyChar = Optional.empty(); * @return the character with lowest frequency if it exists, {@code Optional.empty()} otherwise.
if (charFrequency.isEmpty()) { */
return lowestFrequencyChar; public static Character lowestFrequencyChar(Map<Character, Integer> charFrequency) {
} Character lowestFrequencyChar = null;
Iterator<Entry<Character, Integer>> iterator = charFrequency.entrySet().iterator(); Iterator<Entry<Character, Integer>> iterator = charFrequency.entrySet().iterator();
Entry<Character, Integer> entry = iterator.next(); Entry<Character, Integer> entry = iterator.next();
int minFrequency = entry.getValue(); int minFrequency = entry.getValue();
lowestFrequencyChar = Optional.of(entry.getKey()); lowestFrequencyChar = entry.getKey();
while (iterator.hasNext()) { while (iterator.hasNext()) {
entry = iterator.next(); entry = iterator.next();
if (entry.getValue() < minFrequency) { if (entry.getValue() < minFrequency) {
minFrequency = entry.getValue(); minFrequency = entry.getValue();
lowestFrequencyChar = Optional.of(entry.getKey()); lowestFrequencyChar = entry.getKey();
} }
} }
return lowestFrequencyChar; return lowestFrequencyChar;
} }
/**
* @return number of lines in the file at provided location. 0 if file does not exist.
*/
public static Integer countLines(String fileLocation) { public static Integer countLines(String fileLocation) {
int lineCount = 0; int lineCount = 0;
try (Reader reader = new FileReader(fileLocation); try (Reader reader = new FileReader(fileLocation);
BufferedReader bufferedReader = new BufferedReader(reader);) { BufferedReader bufferedReader = new BufferedReader(reader)) {
while (bufferedReader.readLine() != null) { while (bufferedReader.readLine() != null) {
lineCount++; lineCount++;
} }
@ -71,11 +77,15 @@ public class Utility {
return lineCount; return lineCount;
} }
/**
* Downloads the contents from the given urlString, and stores it in a temporary directory.
* @return the absolute path of the file downloaded.
*/
public static String downloadFile(String urlString) throws MalformedURLException, IOException { public static String downloadFile(String urlString) throws MalformedURLException, IOException {
System.out.println("Downloading contents from url: " + urlString); System.out.println("Downloading contents from url: " + urlString);
URL url = new URL(urlString); URL url = new URL(urlString);
File file = File.createTempFile("promise_pattern", null); File file = File.createTempFile("promise_pattern", null);
try (Reader reader = new InputStreamReader(url.openStream()); try (Reader reader = new InputStreamReader(url.openStream());
BufferedReader bufferedReader = new BufferedReader(reader); BufferedReader bufferedReader = new BufferedReader(reader);
FileWriter writer = new FileWriter(file)) { FileWriter writer = new FileWriter(file)) {
for (String line; (line = bufferedReader.readLine()) != null; ) { for (String line; (line = bufferedReader.readLine()) != null; ) {

View File

@ -26,6 +26,9 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -40,7 +43,6 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
/** /**
* Tests Promise class. * Tests Promise class.
*/ */
@ -73,7 +75,8 @@ public class PromiseTest {
testWaitingSomeTimeForPromiseToBeFulfilled(); testWaitingSomeTimeForPromiseToBeFulfilled();
} }
private void testWaitingForeverForPromiseToBeFulfilled() throws InterruptedException, TimeoutException { private void testWaitingForeverForPromiseToBeFulfilled()
throws InterruptedException, TimeoutException {
Promise<Integer> promise = new Promise<>(); Promise<Integer> promise = new Promise<>();
promise.fulfillInAsync(new Callable<Integer>() { promise.fulfillInAsync(new Callable<Integer>() {
@ -134,7 +137,7 @@ public class PromiseTest {
throws InterruptedException, ExecutionException { throws InterruptedException, ExecutionException {
Promise<Void> dependentPromise = promise Promise<Void> dependentPromise = promise
.fulfillInAsync(new NumberCrunchingTask(), executor) .fulfillInAsync(new NumberCrunchingTask(), executor)
.then(value -> { .thenAccept(value -> {
assertEquals(NumberCrunchingTask.CRUNCHED_NUMBER, value); assertEquals(NumberCrunchingTask.CRUNCHED_NUMBER, value);
}); });
@ -149,17 +152,18 @@ public class PromiseTest {
throws InterruptedException, ExecutionException, TimeoutException { throws InterruptedException, ExecutionException, TimeoutException {
Promise<Void> dependentPromise = promise Promise<Void> dependentPromise = promise
.fulfillInAsync(new NumberCrunchingTask(), executor) .fulfillInAsync(new NumberCrunchingTask(), executor)
.then(new Consumer<Integer>() { .thenAccept(new Consumer<Integer>() {
@Override @Override
public void accept(Integer t) { public void accept(Integer value) {
throw new RuntimeException("Barf!"); throw new RuntimeException("Barf!");
} }
}); });
try { try {
dependentPromise.get(); dependentPromise.get();
fail("Fetching dependent promise should result in exception if the action threw an exception"); fail("Fetching dependent promise should result in exception "
+ "if the action threw an exception");
} catch (ExecutionException ex) { } catch (ExecutionException ex) {
assertTrue(promise.isDone()); assertTrue(promise.isDone());
assertFalse(promise.isCancelled()); assertFalse(promise.isCancelled());
@ -167,7 +171,8 @@ public class PromiseTest {
try { try {
dependentPromise.get(1000, TimeUnit.SECONDS); dependentPromise.get(1000, TimeUnit.SECONDS);
fail("Fetching dependent promise should result in exception if the action threw an exception"); fail("Fetching dependent promise should result in exception "
+ "if the action threw an exception");
} catch (ExecutionException ex) { } catch (ExecutionException ex) {
assertTrue(promise.isDone()); assertTrue(promise.isDone());
assertFalse(promise.isCancelled()); assertFalse(promise.isCancelled());
@ -179,7 +184,7 @@ public class PromiseTest {
throws InterruptedException, ExecutionException { throws InterruptedException, ExecutionException {
Promise<String> dependentPromise = promise Promise<String> dependentPromise = promise
.fulfillInAsync(new NumberCrunchingTask(), executor) .fulfillInAsync(new NumberCrunchingTask(), executor)
.then(value -> { .thenApply(value -> {
assertEquals(NumberCrunchingTask.CRUNCHED_NUMBER, value); assertEquals(NumberCrunchingTask.CRUNCHED_NUMBER, value);
return String.valueOf(value); return String.valueOf(value);
}); });
@ -195,17 +200,18 @@ public class PromiseTest {
throws InterruptedException, ExecutionException, TimeoutException { throws InterruptedException, ExecutionException, TimeoutException {
Promise<String> dependentPromise = promise Promise<String> dependentPromise = promise
.fulfillInAsync(new NumberCrunchingTask(), executor) .fulfillInAsync(new NumberCrunchingTask(), executor)
.then(new Function<Integer, String>() { .thenApply(new Function<Integer, String>() {
@Override @Override
public String apply(Integer t) { public String apply(Integer value) {
throw new RuntimeException("Barf!"); throw new RuntimeException("Barf!");
} }
}); });
try { try {
dependentPromise.get(); dependentPromise.get();
fail("Fetching dependent promise should result in exception if the function threw an exception"); fail("Fetching dependent promise should result in exception "
+ "if the function threw an exception");
} catch (ExecutionException ex) { } catch (ExecutionException ex) {
assertTrue(promise.isDone()); assertTrue(promise.isDone());
assertFalse(promise.isCancelled()); assertFalse(promise.isCancelled());
@ -213,7 +219,8 @@ public class PromiseTest {
try { try {
dependentPromise.get(1000, TimeUnit.SECONDS); dependentPromise.get(1000, TimeUnit.SECONDS);
fail("Fetching dependent promise should result in exception if the function threw an exception"); fail("Fetching dependent promise should result in exception "
+ "if the function threw an exception");
} catch (ExecutionException ex) { } catch (ExecutionException ex) {
assertTrue(promise.isDone()); assertTrue(promise.isDone());
assertFalse(promise.isCancelled()); assertFalse(promise.isCancelled());
@ -228,6 +235,19 @@ public class PromiseTest {
promise.get(1000, TimeUnit.SECONDS); promise.get(1000, TimeUnit.SECONDS);
} }
@SuppressWarnings("unchecked")
@Test
public void exceptionHandlerIsCalledWhenPromiseIsFulfilledExceptionally() {
Promise<Object> promise = new Promise<>();
Consumer<Throwable> exceptionHandler = mock(Consumer.class);
promise.onError(exceptionHandler);
Exception exception = new Exception("barf!");
promise.fulfillExceptionally(exception);
verify(exceptionHandler).accept(eq(exception));
}
private static class NumberCrunchingTask implements Callable<Integer> { private static class NumberCrunchingTask implements Callable<Integer> {
@ -236,7 +256,7 @@ public class PromiseTest {
@Override @Override
public Integer call() throws Exception { public Integer call() throws Exception {
// Do number crunching // Do number crunching
Thread.sleep(1000); Thread.sleep(100);
return CRUNCHED_NUMBER; return CRUNCHED_NUMBER;
} }
} }