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
1 changed files with 38 additions and 38 deletions

View File

@ -7,46 +7,46 @@ 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 {
public List<String> getTokens(String str) { public List<String> getTokens(String str) {
List<String> tokens = new ArrayList<String>(); List<String> tokens = new ArrayList<String>();
// StringTokenizer tokenizer = new StringTokenizer( str ); // StringTokenizer tokenizer = new StringTokenizer( str );
StringTokenizer tokenizer = new StringTokenizer(str, ","); StringTokenizer tokenizer = new StringTokenizer(str, ",");
// StringTokenizer tokenizer = new StringTokenizer( str , "," , true ); // StringTokenizer tokenizer = new StringTokenizer( str , "," , true );
while (tokenizer.hasMoreElements()) { while (tokenizer.hasMoreElements()) {
tokens.add(tokenizer.nextToken()); tokens.add(tokenizer.nextToken());
// tokens.add( tokenizer.nextToken("e") ); // tokens.add( tokenizer.nextToken("e") );
} }
int tokenLength = tokens.size(); int tokenLength = tokens.size();
return tokens; return tokens;
} }
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) {
List<String> tokens = new ArrayList<String>(); public List<String> getTokensFromFile(String path, String delim) {
String currLine = ""; List<String> tokens = new ArrayList<>();
StringTokenizer tokenizer; String currLine = "";
try (BufferedReader br = new BufferedReader( StringTokenizer tokenizer;
new InputStreamReader(Application.class.getResourceAsStream("/" + path)))) { try (BufferedReader br = new BufferedReader(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()) {
tokens.add(tokenizer.nextToken()); tokens.add(tokenizer.nextToken());
} }
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
return tokens; return tokens;
} }
} }