-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordCounterScript2.java
71 lines (60 loc) · 2.65 KB
/
WordCounterScript2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.7.6
import picocli.CommandLine;
import picocli.CommandLine.Command;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.concurrent.Callable;
@Command(name = "WordCounterScript2", mixinStandardHelpOptions = true, version = "WordCounterScript2 0.1",
description = "WordCounterScript2")
class WordCounterScript2 implements Callable<Integer> {
@CommandLine.Option(names = {"-f", "--file"}, description = "Target file name", defaultValue = "data-file.txt")
private String targetFile;
@CommandLine.Option(names = {"-w", "--word"}, description = "Target word", defaultValue = "PEACE")
private String targetWord;
@CommandLine.Option(names = {"-b", "--buffer"}, description = "Characters buffer in MB", defaultValue = "1")
private int charBuffersSizeMB;
public static void main(String... args) {
int exitCode = new CommandLine(new WordCounterScript2()).execute(args);
System.exit(exitCode);
}
@Override
public Integer call() {
var startTime = System.nanoTime();
System.out.println("Target file " + targetFile + " target word " + targetWord + " buffer size " + charBuffersSizeMB);
try {
var bufferSize = charBuffersSizeMB * 1024 * 1024 / 2;
var reader = new BufferedWordCounter();
var count = reader.countWordOnFile(targetWord, targetFile, bufferSize);
System.out.println("The word '" + targetWord + "' occurs " + count + " times.");
return 0;
} catch (Exception e) {
return 1;
} finally {
var endTime = System.nanoTime();
var elapsed = endTime - startTime;
System.out.println("Elapsed time in milliseconds: " + elapsed / 1000000);
}
}
private static class BufferedWordCounter {
public Long countWordOnFile(String targetWord, String targetFile, int bufferSize) {
long wordCount = 0;
try (BufferedReader br = new BufferedReader(new FileReader(targetFile), bufferSize)) {
String line;
while ((line = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
if (targetWord.equals(st.nextToken())) {
wordCount++;
}
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return wordCount;
}
}
}