Skip to content

Commit

Permalink
remove try keywords by using Try of vavr.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Sep 13, 2020
1 parent 8075f52 commit 13eae1d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/github/ninerules/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.ninerules;

import static io.vavr.control.Try.withResources;

import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -44,8 +46,7 @@ private Stream<Path> listupTargets(Stream<Argument> stream, Traverser traverser)
}

public static void main(String[] args) throws UnsupportedEncodingException {
try(PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out, "utf-8"))){
new Main(args, out);
}
withResources(() -> new PrintWriter(new OutputStreamWriter(System.out, "utf-8")))
.of(out -> new Main(args, out));
}
}
17 changes: 10 additions & 7 deletions src/main/java/com/github/ninerules/cli/HelpPrinter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.github.ninerules.cli;

import static io.vavr.control.Try.withResources;

import io.vavr.control.Try;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Expand All @@ -25,18 +29,17 @@ private boolean printIfSpecified(boolean flag, Runnable runnable){
}

public void printHelp(){
try{
openAndPrintThem(getClass().getResource("/resources/help.txt"));
} catch(IOException e){ throw new InternalError(e); }
URL url = getClass().getResource("/resources/help.txt");
Try.run(() -> openAndPrintThem(url));
}

private void openAndPrintThem(URL location) throws IOException{
try(BufferedReader in = new BufferedReader(new InputStreamReader(location.openStream(), "utf-8"))){
printLines(in.lines());
}
withResources(() -> new BufferedReader(new InputStreamReader(location.openStream(), "utf-8")))
.of(in -> printLines(in.lines()));
}

private void printLines(Stream<String> stream){
private boolean printLines(Stream<String> stream){
stream.forEach(out::println);
return true;
}
}

0 comments on commit 13eae1d

Please sign in to comment.