Improved JavaJsonLineCounter a bit
[Mograsim.git] / tests / net.mograsim.logic.model.am2900.tests / src / net / mograsim / logic / model / JavaJsonLineCounter.java
1 package net.mograsim.logic.model;
2
3 import java.io.IOException;
4 import java.io.UncheckedIOException;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.nio.file.Paths;
8 import java.util.Arrays;
9 import java.util.Set;
10 import java.util.concurrent.atomic.AtomicLong;
11 import java.util.stream.Collectors;
12 import java.util.stream.StreamSupport;
13
14 public class JavaJsonLineCounter
15 {
16         public static void main(String[] args) throws IOException
17         {
18                 printLineCount("../..", "java", "bin", "classes", "SWTHelper");
19                 printLineCount("../..", "json", "bin", "classes", "SWTHelper");
20         }
21
22         private static void printLineCount(String path, String filetype, String... excludedDirectoryNames) throws IOException
23         {
24                 Set<Path> excludedDirectoryPaths = Arrays.stream(excludedDirectoryNames).map(Paths::get).collect(Collectors.toSet());
25                 AtomicLong lineCount = new AtomicLong();
26                 AtomicLong byteSize = new AtomicLong();
27                 AtomicLong fileCount = new AtomicLong();
28                 Files.walk(Paths.get(path)).filter(Files::isRegularFile).filter(p -> p.toString().endsWith('.' + filetype))
29                                 .filter(p -> !StreamSupport.stream(p.spliterator(), false).anyMatch(excludedDirectoryPaths::contains)).forEach(p ->
30                                 {
31                                         try
32                                         {
33                                                 lineCount.addAndGet(Files.lines(p).count());
34                                                 byteSize.addAndGet(Files.size(p));
35                                                 fileCount.incrementAndGet();
36                                         }
37                                         catch (IOException e)
38                                         {
39                                                 throw new UncheckedIOException(e);
40                                         }
41                                 });
42                 System.out.println(filetype + ": " + fileCount + " files; " + lineCount + " lines; " + byteSize + " bytes");
43         }
44 }