Refactor Tokenizer (#1605)

This commit is contained in:
Grzegorz Piwowarek 2017-04-07 18:15:19 +02:00 committed by maibin
parent e7d81e6e98
commit 196ea6b5f7

View File

@ -7,6 +7,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.stream.Collectors;
public class Application { public class Application {
@ -25,17 +26,17 @@ public class Application {
public List<String> getTokensWithCollection(String str) { public List<String> getTokensWithCollection(String str) {
StringTokenizer tokenizer = new StringTokenizer(str, ","); StringTokenizer tokenizer = new StringTokenizer(str, ",");
List<String> tokens = new ArrayList<String>();
Collections.list(tokenizer).forEach(token -> tokens.add((String) token)); return Collections.list(tokenizer).stream()
return tokens; .map(token -> (String) token)
.collect(Collectors.toList());
} }
public List<String> getTokensFromFile(String path, String delim) { public List<String> getTokensFromFile(String path, String delim) {
List<String> tokens = new ArrayList<String>(); List<String> tokens = new ArrayList<>();
String currLine = ""; String currLine = "";
StringTokenizer tokenizer; StringTokenizer tokenizer;
try (BufferedReader br = new BufferedReader( try (BufferedReader br = new BufferedReader(new InputStreamReader(Application.class.getResourceAsStream("/" + path)))) {
new InputStreamReader(Application.class.getResourceAsStream("/" + path)))) {
while ((currLine = br.readLine()) != null) { while ((currLine = br.readLine()) != null) {
tokenizer = new StringTokenizer(currLine, delim); tokenizer = new StringTokenizer(currLine, delim);
while (tokenizer.hasMoreElements()) { while (tokenizer.hasMoreElements()) {
@ -48,5 +49,4 @@ public class Application {
return tokens; return tokens;
} }
} }