Work on #403, made example readable and moved methods into utility
This commit is contained in:
parent
76970633b8
commit
95cf9fe367
Binary file not shown.
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 54 KiB |
@ -25,7 +25,7 @@
|
||||
<position height="-1" width="-1" x="524" y="313"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="false" protected="true" private="true" static="true"/>
|
||||
<attributes public="true" package="false" protected="true" private="false" static="true"/>
|
||||
<operations public="true" package="false" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
@ -67,42 +67,38 @@
|
||||
</interface>
|
||||
<class id="8" language="java" name="com.iluwatar.promise.App" project="promise"
|
||||
file="/promise/src/main/java/com/iluwatar/promise/App.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="992" y="206"/>
|
||||
<position height="-1" width="-1" x="822" y="251"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<attributes public="true" package="true" protected="true" private="false" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="false" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<generalization id="9">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="3"/>
|
||||
</generalization>
|
||||
<realization id="10">
|
||||
<realization id="9">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</realization>
|
||||
<dependency id="11">
|
||||
<dependency id="10">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="6"/>
|
||||
</dependency>
|
||||
<dependency id="11">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="5"/>
|
||||
</dependency>
|
||||
<dependency id="12">
|
||||
<end type="SOURCE" refId="8"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</dependency>
|
||||
<dependency id="13">
|
||||
<end type="SOURCE" refId="8"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</dependency>
|
||||
<dependency id="14">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="7"/>
|
||||
</dependency>
|
||||
<dependency id="15">
|
||||
<generalization id="14">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="5"/>
|
||||
</dependency>
|
||||
<dependency id="16">
|
||||
<end type="TARGET" refId="3"/>
|
||||
</generalization>
|
||||
<dependency id="15">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</dependency>
|
||||
|
@ -21,19 +21,10 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
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.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@ -59,7 +50,12 @@ import java.util.concurrent.Executors;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
private static final String URL = "https://raw.githubusercontent.com/iluwatar/java-design-patterns/Promise/promise/README.md";
|
||||
private ExecutorService executor;
|
||||
private CountDownLatch canStop = new CountDownLatch(2);
|
||||
|
||||
private App() {
|
||||
executor = Executors.newFixedThreadPool(2);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,67 +65,80 @@ public class App {
|
||||
* @throws ExecutionException if an execution error occurs.
|
||||
*/
|
||||
public static void main(String[] args) throws InterruptedException, ExecutionException {
|
||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
App app = new App();
|
||||
try {
|
||||
promiseUsage(executor);
|
||||
app.run();
|
||||
} finally {
|
||||
executor.shutdownNow();
|
||||
app.stop();
|
||||
}
|
||||
}
|
||||
|
||||
private static void promiseUsage(Executor executor)
|
||||
throws InterruptedException, ExecutionException {
|
||||
String urlString = "https://raw.githubusercontent.com/iluwatar/java-design-patterns/Promise/promise/README.md";
|
||||
Promise<Integer> lineCountPromise = new Promise<String>().fulfillInAsync(() -> {
|
||||
return downloadFile(urlString);
|
||||
}, executor).then(fileLocation -> {
|
||||
return countLines(fileLocation);
|
||||
});
|
||||
private void run() throws InterruptedException, ExecutionException {
|
||||
promiseUsage();
|
||||
}
|
||||
|
||||
private void promiseUsage() {
|
||||
|
||||
Promise<Map<Character, Integer>> charFrequencyPromise = new Promise<String>().fulfillInAsync(() -> {
|
||||
return String.valueOf(downloadFile(urlString));
|
||||
}, executor).then(fileLocation -> {
|
||||
return characterFrequency(fileLocation);
|
||||
});
|
||||
countLines()
|
||||
.then(
|
||||
count -> {
|
||||
System.out.println("Line count is: " + count);
|
||||
taskCompleted();
|
||||
}
|
||||
);
|
||||
|
||||
lineCountPromise.get();
|
||||
System.out.println("Line count is: " + lineCountPromise.get());
|
||||
charFrequencyPromise.get();
|
||||
System.out.println("Char frequency is: " + charFrequencyPromise.get());
|
||||
lowestCharFrequency()
|
||||
.then(
|
||||
charFrequency -> {
|
||||
System.out.println("Char with lowest frequency is: " + charFrequency);
|
||||
taskCompleted();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private static Map<Character, Integer> characterFrequency(String fileLocation) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
private Promise<Character> lowestCharFrequency() {
|
||||
return characterFrequency()
|
||||
.then(
|
||||
charFrequency -> {
|
||||
return Utility.lowestFrequencyChar(charFrequency).orElse(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 Promise<Map<Character, Integer>> characterFrequency() {
|
||||
return download(URL)
|
||||
.then(
|
||||
fileLocation -> {
|
||||
return Utility.characterFrequency(fileLocation);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
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();
|
||||
private Promise<Integer> countLines() {
|
||||
return download(URL)
|
||||
.then(
|
||||
fileLocation -> {
|
||||
return Utility.countLines(fileLocation);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private Promise<String> download(String urlString) {
|
||||
Promise<String> downloadPromise = new Promise<String>()
|
||||
.fulfillInAsync(
|
||||
() -> {
|
||||
return Utility.downloadFile(urlString);
|
||||
}, executor);
|
||||
|
||||
return downloadPromise;
|
||||
}
|
||||
|
||||
private void stop() throws InterruptedException {
|
||||
canStop.await();
|
||||
executor.shutdownNow();
|
||||
}
|
||||
|
||||
private void taskCompleted() {
|
||||
canStop.countDown();
|
||||
}
|
||||
}
|
||||
|
91
promise/src/main/java/com/iluwatar/promise/Utility.java
Normal file
91
promise/src/main/java/com/iluwatar/promise/Utility.java
Normal file
@ -0,0 +1,91 @@
|
||||
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.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class Utility {
|
||||
|
||||
public static Map<Character, Integer> characterFrequency(String fileLocation) {
|
||||
Map<Character, Integer> characterToFrequency = new HashMap<>();
|
||||
try (Reader reader = new FileReader(fileLocation);
|
||||
BufferedReader bufferedReader = new BufferedReader(reader);) {
|
||||
for (String line; (line = bufferedReader.readLine()) != null;) {
|
||||
for (char c : line.toCharArray()) {
|
||||
if (!characterToFrequency.containsKey(c)) {
|
||||
characterToFrequency.put(c, 1);
|
||||
} else {
|
||||
characterToFrequency.put(c, characterToFrequency.get(c) + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return characterToFrequency;
|
||||
}
|
||||
|
||||
public static Optional<Character> lowestFrequencyChar(Map<Character, Integer> charFrequency) {
|
||||
Optional<Character> lowestFrequencyChar = Optional.empty();
|
||||
if (charFrequency.isEmpty()) {
|
||||
return lowestFrequencyChar;
|
||||
}
|
||||
|
||||
Iterator<Entry<Character, Integer>> iterator = charFrequency.entrySet().iterator();
|
||||
Entry<Character, Integer> entry = iterator.next();
|
||||
int minFrequency = entry.getValue();
|
||||
lowestFrequencyChar = Optional.of(entry.getKey());
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
entry = iterator.next();
|
||||
if (entry.getValue() < minFrequency) {
|
||||
minFrequency = entry.getValue();
|
||||
lowestFrequencyChar = Optional.of(entry.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
return lowestFrequencyChar;
|
||||
}
|
||||
|
||||
public static Integer countLines(String fileLocation) {
|
||||
int lineCount = 0;
|
||||
try (Reader reader = new FileReader(fileLocation);
|
||||
BufferedReader bufferedReader = new BufferedReader(reader);) {
|
||||
while (bufferedReader.readLine() != null) {
|
||||
lineCount++;
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return lineCount;
|
||||
}
|
||||
|
||||
public static String downloadFile(String urlString) throws MalformedURLException, IOException {
|
||||
System.out.println("Downloading contents from url: " + urlString);
|
||||
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");
|
||||
}
|
||||
System.out.println("File downloaded at: " + file.getAbsolutePath());
|
||||
return file.getAbsolutePath();
|
||||
} catch (IOException ex) {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user