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"/>
|
<position height="-1" width="-1" x="524" y="313"/>
|
||||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
sort-features="false" accessors="true" visibility="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"/>
|
<operations public="true" package="false" protected="true" private="true" static="true"/>
|
||||||
</display>
|
</display>
|
||||||
</class>
|
</class>
|
||||||
@ -67,42 +67,38 @@
|
|||||||
</interface>
|
</interface>
|
||||||
<class id="8" language="java" name="com.iluwatar.promise.App" project="promise"
|
<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">
|
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"
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
sort-features="false" accessors="true" visibility="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"/>
|
<operations public="true" package="true" protected="true" private="false" static="true"/>
|
||||||
</display>
|
</display>
|
||||||
</class>
|
</class>
|
||||||
<generalization id="9">
|
<realization id="9">
|
||||||
<end type="SOURCE" refId="1"/>
|
|
||||||
<end type="TARGET" refId="3"/>
|
|
||||||
</generalization>
|
|
||||||
<realization id="10">
|
|
||||||
<end type="SOURCE" refId="3"/>
|
<end type="SOURCE" refId="3"/>
|
||||||
<end type="TARGET" refId="2"/>
|
<end type="TARGET" refId="2"/>
|
||||||
</realization>
|
</realization>
|
||||||
<dependency id="11">
|
<dependency id="10">
|
||||||
<end type="SOURCE" refId="1"/>
|
<end type="SOURCE" refId="1"/>
|
||||||
<end type="TARGET" refId="6"/>
|
<end type="TARGET" refId="6"/>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency id="11">
|
||||||
|
<end type="SOURCE" refId="1"/>
|
||||||
|
<end type="TARGET" refId="5"/>
|
||||||
|
</dependency>
|
||||||
<dependency id="12">
|
<dependency id="12">
|
||||||
<end type="SOURCE" refId="8"/>
|
<end type="SOURCE" refId="8"/>
|
||||||
<end type="TARGET" refId="1"/>
|
<end type="TARGET" refId="1"/>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency id="13">
|
<dependency id="13">
|
||||||
<end type="SOURCE" refId="8"/>
|
|
||||||
<end type="TARGET" refId="4"/>
|
|
||||||
</dependency>
|
|
||||||
<dependency id="14">
|
|
||||||
<end type="SOURCE" refId="1"/>
|
<end type="SOURCE" refId="1"/>
|
||||||
<end type="TARGET" refId="7"/>
|
<end type="TARGET" refId="7"/>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency id="15">
|
<generalization id="14">
|
||||||
<end type="SOURCE" refId="1"/>
|
<end type="SOURCE" refId="1"/>
|
||||||
<end type="TARGET" refId="5"/>
|
<end type="TARGET" refId="3"/>
|
||||||
</dependency>
|
</generalization>
|
||||||
<dependency id="16">
|
<dependency id="15">
|
||||||
<end type="SOURCE" refId="1"/>
|
<end type="SOURCE" refId="1"/>
|
||||||
<end type="TARGET" refId="4"/>
|
<end type="TARGET" refId="4"/>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
@ -21,19 +21,10 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
package com.iluwatar.promise;
|
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.Map;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
@ -59,7 +50,12 @@ import java.util.concurrent.Executors;
|
|||||||
*/
|
*/
|
||||||
public class App {
|
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() {
|
private App() {
|
||||||
|
executor = Executors.newFixedThreadPool(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -69,67 +65,80 @@ public class App {
|
|||||||
* @throws ExecutionException if an execution error occurs.
|
* @throws ExecutionException if an execution error occurs.
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) throws InterruptedException, ExecutionException {
|
public static void main(String[] args) throws InterruptedException, ExecutionException {
|
||||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
App app = new App();
|
||||||
try {
|
try {
|
||||||
promiseUsage(executor);
|
app.run();
|
||||||
} finally {
|
} finally {
|
||||||
executor.shutdownNow();
|
app.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void promiseUsage(Executor executor)
|
private void run() throws InterruptedException, ExecutionException {
|
||||||
throws InterruptedException, ExecutionException {
|
promiseUsage();
|
||||||
String urlString = "https://raw.githubusercontent.com/iluwatar/java-design-patterns/Promise/promise/README.md";
|
}
|
||||||
Promise<Integer> lineCountPromise = new Promise<String>().fulfillInAsync(() -> {
|
|
||||||
return downloadFile(urlString);
|
private void promiseUsage() {
|
||||||
}, executor).then(fileLocation -> {
|
|
||||||
return countLines(fileLocation);
|
|
||||||
});
|
|
||||||
|
|
||||||
Promise<Map<Character, Integer>> charFrequencyPromise = new Promise<String>().fulfillInAsync(() -> {
|
countLines()
|
||||||
return String.valueOf(downloadFile(urlString));
|
.then(
|
||||||
}, executor).then(fileLocation -> {
|
count -> {
|
||||||
return characterFrequency(fileLocation);
|
System.out.println("Line count is: " + count);
|
||||||
});
|
taskCompleted();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
lineCountPromise.get();
|
lowestCharFrequency()
|
||||||
System.out.println("Line count is: " + lineCountPromise.get());
|
.then(
|
||||||
charFrequencyPromise.get();
|
charFrequency -> {
|
||||||
System.out.println("Char frequency is: " + charFrequencyPromise.get());
|
System.out.println("Char with lowest frequency is: " + charFrequency);
|
||||||
|
taskCompleted();
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<Character, Integer> characterFrequency(String fileLocation) {
|
private Promise<Character> lowestCharFrequency() {
|
||||||
// TODO Auto-generated method stub
|
return characterFrequency()
|
||||||
return null;
|
.then(
|
||||||
|
charFrequency -> {
|
||||||
|
return Utility.lowestFrequencyChar(charFrequency).orElse(null);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Integer countLines(String fileLocation) {
|
private Promise<Map<Character, Integer>> characterFrequency() {
|
||||||
int lineCount = 0;
|
return download(URL)
|
||||||
try (Reader reader = new FileReader(fileLocation);
|
.then(
|
||||||
BufferedReader bufferedReader = new BufferedReader(reader);) {
|
fileLocation -> {
|
||||||
for (String line; (line = bufferedReader.readLine()) != null; ) {
|
return Utility.characterFrequency(fileLocation);
|
||||||
lineCount++;
|
}
|
||||||
}
|
);
|
||||||
} catch (IOException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
return lineCount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String downloadFile(String urlString) throws InterruptedException, IOException {
|
private Promise<Integer> countLines() {
|
||||||
URL url = new URL(urlString);
|
return download(URL)
|
||||||
File file = File.createTempFile("promise_pattern", null);
|
.then(
|
||||||
try (Reader reader = new InputStreamReader(url.openStream());
|
fileLocation -> {
|
||||||
BufferedReader bufferedReader = new BufferedReader(reader);
|
return Utility.countLines(fileLocation);
|
||||||
FileWriter writer = new FileWriter(file)) {
|
}
|
||||||
for (String line; (line = bufferedReader.readLine()) != null; ) {
|
);
|
||||||
writer.write(line);
|
}
|
||||||
writer.write("\n");
|
|
||||||
}
|
private Promise<String> download(String urlString) {
|
||||||
} catch (IOException ex) {
|
Promise<String> downloadPromise = new Promise<String>()
|
||||||
ex.printStackTrace();
|
.fulfillInAsync(
|
||||||
}
|
() -> {
|
||||||
System.out.println("File downloaded at: " + file.getAbsolutePath());
|
return Utility.downloadFile(urlString);
|
||||||
return file.getAbsolutePath();
|
}, 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