|
16 | 16 |
|
17 | 17 | package za.co.absa.cobrix.cobol.utils |
18 | 18 |
|
19 | | -import scala.util.control.NonFatal |
20 | | - |
21 | 19 | object UsingUtils { |
22 | 20 | /** |
23 | 21 | * Executes the given action with a resource that implements the AutoCloseable interface, ensuring |
24 | 22 | * proper closure of the resource. Any exception that occurs during the action or resource closure |
25 | | - * is handled appropriately, with suppressed exceptions added where relevant. Null resources are not supported. |
| 23 | + * is handled appropriately. |
| 24 | + * |
| 25 | + * This is for use in Scala before Scala 2.13 introduced `Using` in `scala.util.Using` and works |
| 26 | + * similarly to Java try-with-resources with the exception that it does not support multiple resources. |
| 27 | + * But `using()` can be nested. |
26 | 28 | * |
27 | | - * @param resource a lazily evaluated resource that implements AutoCloseable |
28 | | - * @param action a function to be executed using the provided resource |
29 | | - * @tparam T the type of the resource, which must extend AutoCloseable |
| 29 | + * Example usage: |
| 30 | + * {{{ |
| 31 | + * val result = UsingUtils.using(new AutoCloseableResource()) { resource => |
| 32 | + * // Perform operations with the resource |
| 33 | + * // Return value of any type, including Unit, and null, or throw an exception. |
| 34 | + * } |
| 35 | + * }}} |
| 36 | + * |
| 37 | + * @param resource a resource that implements AutoCloseable. |
| 38 | + * @param action an action to be executed using the provided resource. |
| 39 | + * @tparam T the type of the resource, which must extend AutoCloseable. |
30 | 40 | * @throws Throwable if either the action or resource closure fails. If both fail, the action's exception |
31 | | - * is thrown with the closure's exception added as suppressed |
| 41 | + * is thrown with the closure's exception added as suppressed. |
32 | 42 | */ |
33 | | - def using[T <: AutoCloseable,U](resource: => T)(action: T => U): U = { |
34 | | - var thrownException: Option[Throwable] = None |
35 | | - var suppressedException: Option[Throwable] = None |
| 43 | + def using[T <: AutoCloseable, U](resource: => T)(action: T => U): U = { |
| 44 | + var actionException: Throwable = null |
36 | 45 | val openedResource = resource |
37 | 46 |
|
38 | | - val result = try { |
39 | | - Option(action(openedResource)) |
| 47 | + try { |
| 48 | + action(openedResource) |
40 | 49 | } catch { |
41 | | - case NonFatal(ex) => |
42 | | - thrownException = Option(ex) |
43 | | - None |
| 50 | + case t: Throwable => |
| 51 | + actionException = t |
| 52 | + throw t |
44 | 53 | } finally |
45 | 54 | if (openedResource != null) { |
46 | 55 | try |
47 | 56 | openedResource.close() |
48 | 57 | catch { |
49 | | - case NonFatal(ex) => suppressedException = Option(ex) |
| 58 | + case closeException: Throwable => |
| 59 | + if (actionException != null) { |
| 60 | + actionException.addSuppressed(closeException) |
| 61 | + } else { |
| 62 | + throw closeException |
| 63 | + } |
50 | 64 | } |
51 | 65 | } |
52 | | - |
53 | | - (thrownException, suppressedException) match { |
54 | | - case (Some(thrown), Some(suppressed)) => |
55 | | - thrown.addSuppressed(suppressed) |
56 | | - throw thrown |
57 | | - case (Some(thrown), None) => throw thrown |
58 | | - case (None, Some(suppressed)) => throw suppressed |
59 | | - case (None, None) => result.getOrElse(throw new IllegalArgumentException("Action returned null")) |
60 | | - } |
61 | 66 | } |
62 | 67 | } |
0 commit comments