Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>The <a href="http://scala.sygneca.com/patterns/loan">Loan my Resource</a> pattern ensures that a resource is deterministically disposed of once it goes out of scope.</p> <p>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.</p> <h3>Example</h3> <p>Consider the following code which works with a file. First we might write some line to the file and then print its size:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def f = new File('junk.txt') f.withPrintWriter { pw -> pw.println(new Date()) pw.println(this.class.name) } println f.size() // => 42 </pre></td></tr></table> <p>We could also read back the contents of the file a line at a time and print each line out:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> f.eachLine { line -> println line } // => // Mon Jun 18 22:38:17 EST 2007 // RunPattern </pre></td></tr></table> <p>Note that normal Java <code>Reader</code> and <code>PrintWriter</code> 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 <em>loan</em> 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.</p> <p>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.</p> <p>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:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def reader = f.newReader() reader.splitEachLine(' ') { wordList -> println wordList } reader.close() // => // [ "Mon", "Jun", "18", "22:38:17", "EST", "2007" ] // [ "RunPattern" ] </pre></td></tr></table> <p>Notice that we now have an explicit call to <code>close()</code> in our code. If we didn't code it just right (here we didn't surround the code in a <code>try ... finally</code> block, we run the risk of leaving the file handle open.</p> <p>Let's now apply the loan pattern. First, we'll write a helper method:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def withListOfWordsForEachLine(File f, Closure c) { def r = f.newReader() try { r.splitEachLine(' ', c) } finally { r?.close() } } </pre></td></tr></table> <p>Now, we can re-write our code as follows:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> withListOfWordsForEachLine(f) { wordList -> println wordList } // => // [ "Mon", "Jun", "18", "22:38:17", "EST", "2007" ] // [ "RunPattern" ] </pre></td></tr></table> <p>This is much simpler and has removed the explicit <code>close()</code>. 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.</p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced