jabrena / exceptions-in-java

Apache License 2.0
0 stars 0 forks source link

Upgrade #2

Closed jabrena closed 1 month ago

jabrena commented 1 month ago
.filter(p -> p.getFileName().toString().contains("Exception.java"))
jabrena commented 1 month ago
| JDK   | Checked Exceptions | Unchecked Exceptions |
|-------|--------------------|----------------------|
| JDK6  | 166                | 102                  |
| JDK7  | 131                | 80                   |
| JDK8  | 174                | 114                  |
| JDK9  | 166                | 141                  |
| JDK10 | 161                | 141                  |
| JDK11 | 137                | 128                  |
| JDK12 | 141                | 133                  |
| JDK13 | 139                | 134                  |
| JDK14 | 139                | 137                  |
| JDK15 | 136                | 132                  |
| JDK16 | 136                | 134                  |
| JDK17 | 137                | 139                  |
| JDK18 | 138                | 138                  |
| JDK19 | 143                | 143                  |
| JDK20 | 142                | 141                  |
| JDK21 | 146                | 140                  |
| JDK22 | 146                | 143                  |
jabrena commented 1 month ago
package info.jab.jdk;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExceptionCounter {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionCounter.class);

    record Counter(String jdk, Long checked, Long unchecked) {}

    public Counter countExceptions(List<String> paths) {
        Function<Path, String> loadFileFromGitModule = param -> {
            try {
                return new String(Files.readAllBytes(param), StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException(e.getLocalizedMessage(), e);
            }
        };

        Function<String, Stream<Path>> getFilesFromPath = param -> {
            try {
                return Files.walk(Paths.get(param));
            } catch (IOException e) {
                throw new RuntimeException(e.getLocalizedMessage(), e);
            }
        };

        Predicate<Path> containsCheckedExceptionPattern = param -> {
            final String pattern = " extends Exception";
            return loadFileFromGitModule.apply(param).contains(pattern);
        };

        Predicate<Path> containsUncheckedExceptionPattern = param -> {
            final String pattern = " extends RuntimeException";
            return loadFileFromGitModule.apply(param).contains(pattern);
        };

        Function<List<String>, Long> countCheckedExceptions = param -> {
            logger.info("Counting Checked Exceptions");
            return param
                .stream()
                .flatMap(getFilesFromPath)
                .filter(Files::isRegularFile)
                .filter(p -> p.getFileName().toString().contains("Exception.java"))
                .filter(containsCheckedExceptionPattern)
                //.peek(System.out::println)
                .count();
        };

        Function<List<String>, Long> countUncheckedExceptions = param -> {
            logger.info("Counting Unchecked Exceptions");
            return param
                .stream()
                .flatMap(getFilesFromPath)
                .filter(Files::isRegularFile)
                .filter(p -> p.getFileName().toString().contains("Exception.java"))
                .filter(containsUncheckedExceptionPattern)
                //.peek(System.out::println)
                .count();
        };

        long startTime = System.currentTimeMillis();

        var counter1 = countCheckedExceptions.apply(paths);
        var counter2 = countUncheckedExceptions.apply(paths);

        long stopTime = System.currentTimeMillis();
        float elapsedTime = (stopTime - startTime) / 1000;

        logger.info("Computation time in {}: {} seconds", paths.get(0), elapsedTime);

        return new Counter(paths.get(0), counter1, counter2);
    }
}