Created
January 16, 2025 04:25
-
-
Save ko-sasaki/3b5cc69c824fdbcb77f070d318b61641 to your computer and use it in GitHub Desktop.
Scratch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.function.Function; | |
import java.util.stream.Stream; | |
class Scratch { | |
public static void main(String[] args) { | |
Function<String, String> first = Files.lines(Paths.get("test.txt")).map(e -> wrapFunction(Scratch::processLine)).toList(); | |
Stream<String> stringStream = Files.lines(Paths.get("test.txt")).map(wrapFunction(Scratch::processLine)); | |
} | |
public static <T, R, E extends Exception> Function<T, R> wrapFunction(CheckedExceptionFunction<T, R, E> checkedExceptionFunction) { | |
return t -> { | |
try { | |
return checkedExceptionFunction.apply(t); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
}; | |
} | |
private static String processLine(String line) throws IOException { | |
if (line.equals("error")) { | |
throw new IOException("エラーが発生しました"); | |
} | |
return line.toUpperCase(); | |
} | |
@FunctionalInterface | |
interface CheckedExceptionFunction<T, R, E extends Exception> { | |
R apply(T t) throws E; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment