2121 * THE SOFTWARE.
2222 */
2323package com .iluwatar .promise ;
24-
25- import java .io .BufferedReader ;
26- import java .io .File ;
27- import java .io .FileReader ;
28- import java .io .FileWriter ;
29- import java .io .IOException ;
30- import java .io .InputStreamReader ;
31- import java .io .Reader ;
32- import java .net .URL ;
3324import java .util .Map ;
3425import java .util .concurrent .CompletableFuture ;
26+ import java .util .concurrent .CountDownLatch ;
3527import java .util .concurrent .ExecutionException ;
36- import java .util .concurrent .Executor ;
3728import java .util .concurrent .ExecutorService ;
3829import java .util .concurrent .Executors ;
3930
5950 */
6051public class App {
6152
53+ private static final String URL = "https://raw.githubusercontent.com/iluwatar/java-design-patterns/Promise/promise/README.md" ;
54+ private ExecutorService executor ;
55+ private CountDownLatch canStop = new CountDownLatch (2 );
56+
6257 private App () {
58+ executor = Executors .newFixedThreadPool (2 );
6359 }
6460
6561 /**
@@ -69,67 +65,80 @@ private App() {
6965 * @throws ExecutionException if an execution error occurs.
7066 */
7167 public static void main (String [] args ) throws InterruptedException , ExecutionException {
72- ExecutorService executor = Executors . newSingleThreadExecutor ();
68+ App app = new App ();
7369 try {
74- promiseUsage ( executor );
70+ app . run ( );
7571 } finally {
76- executor . shutdownNow ();
72+ app . stop ();
7773 }
7874 }
7975
80- private static void promiseUsage (Executor executor )
81- throws InterruptedException , ExecutionException {
82- String urlString = "https://raw.githubusercontent.com/iluwatar/java-design-patterns/Promise/promise/README.md" ;
83- Promise <Integer > lineCountPromise = new Promise <String >().fulfillInAsync (() -> {
84- return downloadFile (urlString );
85- }, executor ).then (fileLocation -> {
86- return countLines (fileLocation );
87- });
76+ private void run () throws InterruptedException , ExecutionException {
77+ promiseUsage ();
78+ }
79+
80+ private void promiseUsage () {
8881
89- Promise <Map <Character , Integer >> charFrequencyPromise = new Promise <String >().fulfillInAsync (() -> {
90- return String .valueOf (downloadFile (urlString ));
91- }, executor ).then (fileLocation -> {
92- return characterFrequency (fileLocation );
93- });
82+ countLines ()
83+ .then (
84+ count -> {
85+ System .out .println ("Line count is: " + count );
86+ taskCompleted ();
87+ }
88+ );
9489
95- lineCountPromise .get ();
96- System .out .println ("Line count is: " + lineCountPromise .get ());
97- charFrequencyPromise .get ();
98- System .out .println ("Char frequency is: " + charFrequencyPromise .get ());
90+ lowestCharFrequency ()
91+ .then (
92+ charFrequency -> {
93+ System .out .println ("Char with lowest frequency is: " + charFrequency );
94+ taskCompleted ();
95+ }
96+ );
9997 }
10098
101- private static Map <Character , Integer > characterFrequency (String fileLocation ) {
102- // TODO Auto-generated method stub
103- return null ;
99+ private Promise <Character > lowestCharFrequency () {
100+ return characterFrequency ()
101+ .then (
102+ charFrequency -> {
103+ return Utility .lowestFrequencyChar (charFrequency ).orElse (null );
104+ }
105+ );
104106 }
105107
106- private static Integer countLines (String fileLocation ) {
107- int lineCount = 0 ;
108- try (Reader reader = new FileReader (fileLocation );
109- BufferedReader bufferedReader = new BufferedReader (reader );) {
110- for (String line ; (line = bufferedReader .readLine ()) != null ; ) {
111- lineCount ++;
112- }
113- } catch (IOException ex ) {
114- ex .printStackTrace ();
115- }
116- return lineCount ;
108+ private Promise <Map <Character , Integer >> characterFrequency () {
109+ return download (URL )
110+ .then (
111+ fileLocation -> {
112+ return Utility .characterFrequency (fileLocation );
113+ }
114+ );
117115 }
118116
119- private static String downloadFile (String urlString ) throws InterruptedException , IOException {
120- URL url = new URL (urlString );
121- File file = File .createTempFile ("promise_pattern" , null );
122- try (Reader reader = new InputStreamReader (url .openStream ());
123- BufferedReader bufferedReader = new BufferedReader (reader );
124- FileWriter writer = new FileWriter (file )) {
125- for (String line ; (line = bufferedReader .readLine ()) != null ; ) {
126- writer .write (line );
127- writer .write ("\n " );
128- }
129- } catch (IOException ex ) {
130- ex .printStackTrace ();
131- }
132- System .out .println ("File downloaded at: " + file .getAbsolutePath ());
133- return file .getAbsolutePath ();
117+ private Promise <Integer > countLines () {
118+ return download (URL )
119+ .then (
120+ fileLocation -> {
121+ return Utility .countLines (fileLocation );
122+ }
123+ );
124+ }
125+
126+ private Promise <String > download (String urlString ) {
127+ Promise <String > downloadPromise = new Promise <String >()
128+ .fulfillInAsync (
129+ () -> {
130+ return Utility .downloadFile (urlString );
131+ }, executor );
132+
133+ return downloadPromise ;
134+ }
135+
136+ private void stop () throws InterruptedException {
137+ canStop .await ();
138+ executor .shutdownNow ();
139+ }
140+
141+ private void taskCompleted () {
142+ canStop .countDown ();
134143 }
135144}
0 commit comments