Advertisement
Guest User

Untitled

a guest
Oct 19th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. public class StreamTest {
  2.  
  3. // 46 строк
  4. @RequiredArgsConstructor
  5. static class ExceptionUnion extends Exception {
  6.  
  7. private final VExceptionUnion base;
  8.  
  9. sealed interface VExceptionUnion permits VIOException, VDBException {}
  10. public record VIOException(IOException exception) implements VExceptionUnion {}
  11. public record VDBException(DBException exception) implements VExceptionUnion {}
  12.  
  13. }
  14.  
  15. void withoutTypeUnion() {
  16. var stream = zalupaSource().stream()
  17. .map(var -> IO.something(var))
  18. .filter(var -> {
  19. try {
  20. return DB.also(var);
  21. } catch (DBException exception) {
  22. throw new ExceptionUnion(new ExceptionUnion.VDBException(exception));
  23. }
  24. })
  25. .filter(var -> {
  26. File var1;
  27. try {
  28. var1 = IO.something(var);
  29. } catch (IOException exception) {
  30. throw new ExceptionUnion(new ExceptionUnion.VIOException(exception));
  31. }
  32. Record var2;
  33. try {
  34. var2 = DB.get(var1.lines().get(0));
  35. } catch (DBException exception) {
  36. throw new ExceptionUnion(new ExceptionUnion.VDBException(exception));
  37. }
  38. });
  39.  
  40. User result;
  41. try {
  42. result = stream.findFirst();
  43. } catch (ExceptionUnion exception) {
  44. switch (exception.base) {
  45. case VIOException(var ioException) -> // logic 1
  46. case VDBException(var dbException) -> // logic 2
  47. }
  48. }
  49. }
  50.  
  51. // 18 строк
  52. void withTypeUnion() {
  53. var stream = zalupaSource().stream()
  54. .map(var -> IO.something(var))
  55. .filter(var -> DB.also(var))
  56. .filter(var -> {
  57. File var1 = IO.something(var);
  58. Record var2 = DB.get(var1.lines().get(0));
  59. });
  60.  
  61. User result;
  62. try {
  63. result = stream.findFirst();
  64. } catch (IOException exception) {
  65. // logic 1
  66. } catch (DBException exception) {
  67. // logic 2
  68. }
  69. }
  70.  
  71. // 13 строк
  72. void withTryExpressions() {
  73. var stream = zalupaSource().stream()
  74. .map(var -> IO.something(var))
  75. .filter(var -> DB.also(var))
  76. .filter(var -> {
  77. File var1 = IO.something(var);
  78. Record var2 = DB.get(var1.lines().get(0));
  79. });
  80.  
  81. User result = try (stream.findFirst()) {
  82. catch IOException exception -> // logic 1
  83. catch DBException exception -> // logic 2
  84. }
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement