From 76970633b8a654e05d4341e96ee639bc3bcde969 Mon Sep 17 00:00:00 2001 From: Narendra Pathai Date: Thu, 4 Aug 2016 18:10:50 +0530 Subject: [PATCH] Work on #403, incorporate review changes --- promise/README.md | 5 +- promise/etc/promise.ucls | 46 ++++++------ .../main/java/com/iluwatar/promise/App.java | 73 +++++++++++++++---- .../java/com/iluwatar/promise/Promise.java | 16 ++-- .../com/iluwatar/promise/PromiseSupport.java | 14 ++-- 5 files changed, 100 insertions(+), 54 deletions(-) diff --git a/promise/README.md b/promise/README.md index 069a8dbfe..638bb3ef5 100644 --- a/promise/README.md +++ b/promise/README.md @@ -3,10 +3,11 @@ layout: pattern title: Promise folder: promise permalink: /patterns/promise/ -categories: Structural +categories: Concurrency tags: - Java - - Concurrency + - Functional + - Reactive - Difficulty-Intermediate --- diff --git a/promise/etc/promise.ucls b/promise/etc/promise.ucls index fe3c6c2e0..cdfb6ed7f 100644 --- a/promise/etc/promise.ucls +++ b/promise/etc/promise.ucls @@ -74,38 +74,38 @@ - - - - - - - - - + - - - - - - - - - - - - - + + + + + - + + + + + + + + + + + + + + + + + diff --git a/promise/src/main/java/com/iluwatar/promise/App.java b/promise/src/main/java/com/iluwatar/promise/App.java index 6ce7de994..3a1ecfa01 100644 --- a/promise/src/main/java/com/iluwatar/promise/App.java +++ b/promise/src/main/java/com/iluwatar/promise/App.java @@ -22,6 +22,15 @@ */ package com.iluwatar.promise; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.URL; +import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; @@ -44,6 +53,8 @@ import java.util.concurrent.Executors; *
  • Prevents callback hell and provides callback aggregation * * + *

    + * * @see CompletableFuture */ public class App { @@ -68,23 +79,57 @@ public class App { private static void promiseUsage(Executor executor) throws InterruptedException, ExecutionException { - Promise consumedPromise = new Promise<>(); - consumedPromise.fulfillInAsync(() -> { - Thread.sleep(1000); - return 10; - }, executor).then(value -> { - System.out.println("Consumed int value: " + value); + String urlString = "https://raw.githubusercontent.com/iluwatar/java-design-patterns/Promise/promise/README.md"; + Promise lineCountPromise = new Promise().fulfillInAsync(() -> { + return downloadFile(urlString); + }, executor).then(fileLocation -> { + return countLines(fileLocation); }); - Promise transformedPromise = new Promise<>(); - transformedPromise.fulfillInAsync(() -> { - Thread.sleep(1000); - return "10"; - }, executor).then(value -> { return Integer.parseInt(value); }).then(value -> { - System.out.println("Consumed transformed int value: " + value); + Promise> charFrequencyPromise = new Promise().fulfillInAsync(() -> { + return String.valueOf(downloadFile(urlString)); + }, executor).then(fileLocation -> { + return characterFrequency(fileLocation); }); - consumedPromise.get(); - transformedPromise.get(); + lineCountPromise.get(); + System.out.println("Line count is: " + lineCountPromise.get()); + charFrequencyPromise.get(); + System.out.println("Char frequency is: " + charFrequencyPromise.get()); + } + + private static Map characterFrequency(String fileLocation) { + // TODO Auto-generated method stub + return null; + } + + private static Integer countLines(String fileLocation) { + int lineCount = 0; + try (Reader reader = new FileReader(fileLocation); + BufferedReader bufferedReader = new BufferedReader(reader);) { + for (String line; (line = bufferedReader.readLine()) != null; ) { + lineCount++; + } + } catch (IOException ex) { + ex.printStackTrace(); + } + return lineCount; + } + + private static String downloadFile(String urlString) throws InterruptedException, IOException { + URL url = new URL(urlString); + File file = File.createTempFile("promise_pattern", null); + try (Reader reader = new InputStreamReader(url.openStream()); + BufferedReader bufferedReader = new BufferedReader(reader); + FileWriter writer = new FileWriter(file)) { + for (String line; (line = bufferedReader.readLine()) != null; ) { + writer.write(line); + writer.write("\n"); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + System.out.println("File downloaded at: " + file.getAbsolutePath()); + return file.getAbsolutePath(); } } diff --git a/promise/src/main/java/com/iluwatar/promise/Promise.java b/promise/src/main/java/com/iluwatar/promise/Promise.java index fe7dc6f9f..7d8a97e84 100644 --- a/promise/src/main/java/com/iluwatar/promise/Promise.java +++ b/promise/src/main/java/com/iluwatar/promise/Promise.java @@ -120,11 +120,11 @@ public class Promise extends PromiseSupport { */ private class ConsumeAction implements Runnable { - private Promise src; - private Promise dest; - private Consumer action; + private final Promise src; + private final Promise dest; + private final Consumer action; - ConsumeAction(Promise src, Promise dest, Consumer action) { + private ConsumeAction(Promise src, Promise dest, Consumer action) { this.src = src; this.dest = dest; this.action = action; @@ -147,11 +147,11 @@ public class Promise extends PromiseSupport { */ private class TransformAction implements Runnable { - private Promise src; - private Promise dest; - private Function func; + private final Promise src; + private final Promise dest; + private final Function func; - TransformAction(Promise src, Promise dest, Function func) { + private TransformAction(Promise src, Promise dest, Function func) { this.src = src; this.dest = dest; this.func = func; diff --git a/promise/src/main/java/com/iluwatar/promise/PromiseSupport.java b/promise/src/main/java/com/iluwatar/promise/PromiseSupport.java index dde2ca452..048586e23 100644 --- a/promise/src/main/java/com/iluwatar/promise/PromiseSupport.java +++ b/promise/src/main/java/com/iluwatar/promise/PromiseSupport.java @@ -33,15 +33,15 @@ import java.util.concurrent.TimeoutException; */ class PromiseSupport implements Future { - static final int RUNNING = 1; - static final int FAILED = 2; - static final int COMPLETED = 3; + private static final int RUNNING = 1; + private static final int FAILED = 2; + private static final int COMPLETED = 3; - final Object lock; + private final Object lock; - volatile int state = RUNNING; - T value; - Exception exception; + private volatile int state = RUNNING; + private T value; + private Exception exception; PromiseSupport() { this.lock = new Object();