Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class StreamTest {
- // 46 строк
- @RequiredArgsConstructor
- static class ExceptionUnion extends Exception {
- private final VExceptionUnion base;
- sealed interface VExceptionUnion permits VIOException, VDBException {}
- public record VIOException(IOException exception) implements VExceptionUnion {}
- public record VDBException(DBException exception) implements VExceptionUnion {}
- }
- void withoutTypeUnion() {
- var stream = zalupaSource().stream()
- .map(var -> IO.something(var))
- .filter(var -> {
- try {
- return DB.also(var);
- } catch (DBException exception) {
- throw new ExceptionUnion(new ExceptionUnion.VDBException(exception));
- }
- })
- .filter(var -> {
- File var1;
- try {
- var1 = IO.something(var);
- } catch (IOException exception) {
- throw new ExceptionUnion(new ExceptionUnion.VIOException(exception));
- }
- Record var2;
- try {
- var2 = DB.get(var1.lines().get(0));
- } catch (DBException exception) {
- throw new ExceptionUnion(new ExceptionUnion.VDBException(exception));
- }
- });
- User result;
- try {
- result = stream.findFirst();
- } catch (ExceptionUnion exception) {
- switch (exception.base) {
- case VIOException(var ioException) -> // logic 1
- case VDBException(var dbException) -> // logic 2
- }
- }
- }
- // 18 строк
- void withTypeUnion() {
- var stream = zalupaSource().stream()
- .map(var -> IO.something(var))
- .filter(var -> DB.also(var))
- .filter(var -> {
- File var1 = IO.something(var);
- Record var2 = DB.get(var1.lines().get(0));
- });
- User result;
- try {
- result = stream.findFirst();
- } catch (IOException exception) {
- // logic 1
- } catch (DBException exception) {
- // logic 2
- }
- }
- // 13 строк
- void withTryExpressions() {
- var stream = zalupaSource().stream()
- .map(var -> IO.something(var))
- .filter(var -> DB.also(var))
- .filter(var -> {
- File var1 = IO.something(var);
- Record var2 = DB.get(var1.lines().get(0));
- });
- User result = try (stream.findFirst()) {
- catch IOException exception -> // logic 1
- catch DBException exception -> // logic 2
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement