Repository navigation
Expand file tree
/
Copy pathGrepTool.java
More file actions
65 lines (53 loc) · 2.62 KB
/
Copy pathGrepTool.java
File metadata and controls
65 lines (53 loc) · 2.62 KB
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
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
class GrepTool {
String grep(String pattern, List<String> flags, List<String> files) {
boolean shouldAddFilenameToEachLine = files.size() > 1;
return files
.stream()
.map(file -> grepFile(pattern, flags, file, shouldAddFilenameToEachLine))
.filter(line -> !line.isEmpty())
.collect(Collectors.joining("\n"));
}
private String grepFile(String pattern, List<String> flags, String filename, boolean shouldAddFilenameToEachLine) {
List<String> lines = readFile(filename);
String matchingLines = IntStream.range(0, lines.size())
.filter(lineIndex -> lineMatchesPattern(lines.get(lineIndex), pattern, flags))
.mapToObj(lineIndex -> {
String lineWithFlagsApplied = applyFlagsToLine(lines.get(lineIndex), flags, lineIndex + 1);
return shouldAddFilenameToEachLine ? filename + ":" + lineWithFlagsApplied : lineWithFlagsApplied;
})
.collect(Collectors.joining("\n"));
boolean shouldPrintFilenameInsteadOfMatchingLines = flags.contains("-l");
return shouldPrintFilenameInsteadOfMatchingLines && !matchingLines.isEmpty() ? filename : matchingLines;
}
private boolean lineMatchesPattern(String line, String pattern, List<String> flags) {
boolean isCaseInsensitive = flags.contains("-i");
String lineToCompare = isCaseInsensitive ? line.toLowerCase() : line;
String patternToCompare = isCaseInsensitive ? pattern.toLowerCase() : pattern;
boolean shouldMatchEntireLine = flags.contains("-x");
boolean matchesPattern = shouldMatchEntireLine
? lineToCompare.equals(patternToCompare)
: lineToCompare.contains(patternToCompare);
boolean shouldInvertMatch = flags.contains("-v");
//noinspection SimplifiableConditionalExpression
return shouldInvertMatch ? !matchesPattern : matchesPattern;
}
private String applyFlagsToLine(String line, List<String> flags, int lineNumber) {
boolean shouldAddLineNumber = flags.contains("-n");
return shouldAddLineNumber ? lineNumber + ":" + line : line;
}
private List<String> readFile(String filename) {
try {
return Files.readAllLines(Paths.get(filename), Charset.forName("UTF-8"));
} catch (IOException e) {
return Collections.emptyList();
}
}
}