Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object AutoCloseableUtils {
- /**
- * Class provides try-with-resource for scala
- * Basic tool for resource management.
- * This class is similar to try-with-resources in java.
- *
- * @param resource Produces the resource
- * @tparam R Resource type
- */
- implicit class AutoCloseableResource[R <: AutoCloseable](resource: R){
- /**
- * Method performs a block on resource and returns the value.
- * Resource will be closed automatically.
- *
- * @param block An action to be performed on resource
- * @tparam A Return type
- */
- @inline
- def use[A](block: (R => A)): A = {
- try {
- block(resource)
- } finally {
- if (resource != null) try {
- resource.close()
- } catch {
- case e: Exception => e.printStackTrace()
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment