# Loan my Resource Pattern

version 2.5.22

The Loan my Resource pattern ensures that a resource is deterministically disposed of once it goes out of scope.

This pattern is built in to many Groovy helper methods. You should consider using it yourself if you need to work with resources in ways beyond what Groovy supports.

## 1. Example

Consider the following code which works with a file. First we might write some line to the file and then print its size:

    def f = new File('junk.txt')
    f.withPrintWriter { pw ->
        pw.println(new Date())
        pw.println(this.class.name)
    }
    println f.size()
    // => 42

We could also read back the contents of the file a line at a time and print each line out:

    f.eachLine { line ->
        println line
    }
    // =>
    // Mon Jun 18 22:38:17 EST 2007
    // RunPattern

Note that normal Java `Reader` and `PrintWriter` objects were used under the covers by Groovy but the code writer did not have to worry about explicitly creating or closing those resources. The built-in Groovy methods loan the respective reader or writer to the closure code and then tidy up after themselves. So, you are using this pattern without having to do any work.

Sometimes however, you wish to do things slightly differently to what you can get for free using Groovy’s built-in mechanisms. You should consider utilising this pattern within your own resource-handling operations.

Consider how you might process the list of words on each line within the file. We could actually do this one too using Groovy’s built-in functions, but bear with us and assume we have to do some resource handling ourselves. Here is how we might write the code without using this pattern:

    def reader = f.newReader()
    reader.splitEachLine(' ') { wordList ->
        println wordList
    }
    reader.close()
    // =>
    // [ "Mon", "Jun", "18", "22:38:17", "EST", "2007" ]
    // [ "RunPattern" ]

Notice that we now have an explicit call to `close()` in our code. If we didn’t code it just right (here we didn’t surround the code in a `try …​ finally` block, we run the risk of leaving the file handle open.

Let’s now apply the loan pattern. First, we’ll write a helper method:

    def withListOfWordsForEachLine(File f, Closure c) {
        def r = f.newReader()
        try {
            r.splitEachLine(' ', c)
        } finally {
            r?.close()
        }
    }

Now, we can re-write our code as follows:

    withListOfWordsForEachLine(f) { wordList ->
        println wordList
    }
    // =>
    // [ "Mon", "Jun", "18", "22:38:17", "EST", "2007" ]
    // [ "RunPattern" ]

This is much simpler and has removed the explicit `close()`. This is now catered for in one spot so we can apply the appropriate level of testing or reviewing in just one spot to be sure we have no problems.