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
Sign Up
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
<h3>if - else statement</h3> <p>Groovy supports the usual if - else syntax from Java</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 x = false def y = false if ( !x ) { x = true } assert x == true if ( x ) { x = false } else { y = true } assert x == y </pre></td></tr></table> <p>Groovy also supports the normal Java "nested" if then else if syntax:</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> if ( ... ) { ... } else if (...) { ... } else { ... } </pre></td></tr></table> <h3>ternary operator</h3> <p>Groovy also supports the ternary operator:</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 y = 5 def x = (y > 1) ? "worked" : "failed" assert x == "worked" </pre></td></tr></table> <p>See also: the <a class="confluence-link" href="/display/GROOVY/Operators" data-linked-resource-id="12190097" data-linked-resource-type="page" data-linked-resource-default-alias="Operators" data-base-url="http://docs.codehaus.org">elvis operator</a></p> <h3>switch statement</h3> <p>The switch statement in Groovy is backwards compatible with Java code; so you can fall through cases sharing the same code for multiple matches.</p> <p>One difference though is that the Groovy switch statement can handle any kind of switch value and different kinds of matching can be performed.</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 x = 1.23 def result = "" switch ( x ) { case "foo": result = "found foo" // lets fall through case "bar": result += "bar" case [4, 5, 6, 'inList']: result = "list" break case 12..30: result = "range" break case Integer: result = "integer" break case Number: result = "number" break default: result = "default" } assert result == "number" </pre></td></tr></table> <p>Switch supports the following kinds of comparisons</p> <ul> <li>Class case values matches if the switchValue is an instanceof the class</li> <li>Regular expression case value matches if the string of the switchValue matches the regex</li> <li>Collection case value matches if the switchValue is contained in the collection. This also includes ranges too (since they are Lists)</li> <li>if none of the above are used then the case value matches if the case value equals the switch value</li> </ul> <p><strong>default</strong>: must go at the end of the switch/case as Jochen outlined in <a href="http://groovy.markmail.org/message/6w6i647cbqnagrx5?q=Acceptable+Placement+of+default:+Within+Switch%3F&page=1">this thread </a> from the groovy-user mailing list which Jochen states: </p> <p>"because a Java switch/Case does not work like a Groovy switch/case. In Java a case can take only int compatible constants, in Groovy it can take expressions. In Java all cases share a scope, in Groovy each case has its own scope. In Groovy we call the isCase method, in Java it has to be a number we switch with. If we for example use a closure as case, then this might cause side effects. There are cases where we can let them behave the same and usually when using the java version you won't see a difference in Groovy besides the placement and logic of default."</p> <p>So, while in Java the <em>default</em> can be placed anywhere in the <em>switch/case</em>, the <em>default</em> in Groovy is used more as an <em>else</em> than assigning a <em>default</em> case.</p> <h2>How switch works</h2> <p>The case statement performs a <em>match</em> on the case value using the <em>isCase(switchValue)</em> method, which defaults to <em>call equals(switchValue)</em> but has been overloaded for various types like Class or regex etc.</p> <p>So you could create your own kind of matcher class and add an <em>isCase(switchValue)</em> method to provide your own kind of matching.</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