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
<h2>Operators</h2><p>In general all operators <a href="http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html">supported in Java</a> are identical in Groovy. Groovy goes a step further by allowing you to <a class="confluence-link" href="/display/GROOVY/Operator+Overloading" data-linked-resource-id="2764" data-linked-resource-type="page" data-linked-resource-default-alias="Operator Overloading" data-base-url="http://docs.codehaus.org">customize behavior</a> of operators on Groovy types.</p><p><img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e3RvYzptYXhMZXZlbD04fG1pbkxldmVsPTN9&locale=en_GB&version=2" data-macro-name="toc" data-macro-parameters="maxLevel=8|minLevel=3"></p><h3>Arithmetic and Conditional Operators</h3><p>See <a class="confluence-link" href="/display/GROOVY/Operator+Overloading" data-linked-resource-id="2764" data-linked-resource-type="page" data-linked-resource-default-alias="Operator Overloading" data-base-url="http://docs.codehaus.org">Operator Overloading</a> for a list of the common operators that Groovy supports.</p><p>In addition, Groovy supports the ! (not) operator 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>def expression = false assert !expression </pre></td></tr></table><p>For more details about how expressions are corced to a boolean value, see: <a class="confluence-link" href="/display/GROOVY/Groovy+Truth" data-linked-resource-id="31358992" data-linked-resource-type="page" data-linked-resource-default-alias="Groovy Truth" data-base-url="http://docs.codehaus.org">Groovy Truth</a>.</p><h3>Collection-based Operators</h3><h4>Spread Operator (*.)</h4><p>The Spread Operator is used to invoke an action on all items of an aggregate object. It is equivalent to calling the collect method like so:</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>parent*.action //equivalent to: parent.collect{ child -> child?.action } </pre></td></tr></table><p>The action may either be a method call or property access, and returns a list of the items returned from each child call. As an example:</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>assert ['cat', 'elephant']*.size() == [3, 8] </pre></td></tr></table><p>The Spread operator will work as expected with most of the aggregate-like classes within Groovy. You can also customize your own classes to use it by defining your own <code>iterator()</code> method as this example shows:</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>class Person { String name } class Twin { Person one, two def iterator() { return [one, two].iterator() } } def tw = new Twin(one: new Person(name:'Tom'), two: new Person(name:'Tim')) assert tw*.name == ['Tom', 'Tim'] // expanded equivalent of above: assert tw.collect{ it.name } == ['Tom', 'Tim'] </pre></td></tr></table><h3>Object-Related Operators</h3><ul><li><a class="confluence-link" href="/display/GROOVY/Using+invokeMethod+and+getProperty" data-linked-resource-id="9765915" data-linked-resource-type="page" data-linked-resource-default-alias="Using invokeMethod and getProperty" data-base-url="http://docs.codehaus.org">invokeMethod and get/setProperty </a> (.)</li><li>Java field (.@)</li><li>The spread java field (*.@)</li><li>Method Reference (.&)</li><li>'as' - "manual coercion" - <code>asType(t)</code> method</li><li>Groovy == ( <code>equals()</code>) behavior.<ul><li>"is" for identity</li></ul></li><li>The <em>instanceof</em> operator (as in Java)</li></ul><h5>Java field (.@)</h5><p>Groovy dynamically creates getter method for all your fields that can be referenced as properties:</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>class X { def field } x = new X() x.field = 1 println x.field // 1 </pre></td></tr></table><p>You can override these getters with your own implementations if you like:</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>class X { def field def getField() { field += 1 } } x = new X() x.field = 1 println x.field // 2 </pre></td></tr></table><p>The @ operator allows you to override this behavior and access the field directly, so to extend the previous sample:</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>println x.@field // 1 </pre></td></tr></table><p>It should be mentioned that, while interesting, this is probably not a good thing to do unless you really need to. Overriding a public interface to access the internal state of an object probably means you are about to break something. Not even recommended for use in tests since it increases coupling unnecessarily.</p><h3>Other Operators</h3><ul><li><p><code>getAt(index)</code> and <code>putAt(index, value)</code> for the subscript operator (e.g. foo[1] or foo['bar'], i.e. index is either an int or String)</p></li><li>Range Operator (..) - see <a class="confluence-link" href="/display/GROOVY/Collections#Collections-Collections-Ranges" data-anchor="Collections-Ranges" data-linked-resource-id="2732" data-linked-resource-type="page" data-linked-resource-default-alias="Collections#Collections-Ranges" data-base-url="http://docs.codehaus.org">Collections#Collections-Ranges</a></li><li><code>isCase()</code> for the membership operator (in)</li></ul><h4>Elvis Operator (?: )</h4><p>The "Elvis operator" is a shortening of Java's <a href="http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op2.html">ternary operator</a>. One instance of where this is handy is for returning a 'sensible default' value if an expression resolves to <code>false</code> or <code>null</code>. A simple example might look like this:</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 displayName = user.name ? user.name : "Anonymous" //traditional ternary operator usage def displayName = user.name ?: "Anonymous" // more compact Elvis operator - does same as above</pre></td></tr></table><h4>Safe Navigation Operator (?.)</h4><p>The Safe Navigation operator is used to avoid a NullPointerException. Typically when you have a reference to an object you might need to verify that it is not null before accessing methods or properties of the object. To avoid this, the safe navigation operator will simply return <code>null</code> instead of throwing an exception, like so:</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 user = User.find( "admin" ) //this might be null if 'admin' does not exist def streetName = user?.address?.street //streetName will be null if user or user.address is null - no NPE thrown </pre></td></tr></table><h3>Regular Expression Operators</h3><ul><li>find (=~)</li><li>match (==~)</li></ul><p>For more details, see: <a class="confluence-link" href="/display/GROOVY/Regular+Expressions" data-linked-resource-id="2768" data-linked-resource-type="page" data-linked-resource-default-alias="Regular Expressions" data-base-url="http://docs.codehaus.org">Regular Expressions</a></p><h3>Table of Operators</h3><table class="confluenceTable"><tbody><tr><th class="confluenceTh"><p>Operator Name</p></th><th class="confluenceTh"><p>Symbol</p></th><th class="confluenceTh"><p>Description</p></th></tr><tr><td class="confluenceTd"><p>Spaceship</p></td><td class="confluenceTd"><p><=></p></td><td class="confluenceTd"><p>Useful in comparisons, returns -1 if left is smaller 0 if == to right or 1 if greater than the right</p></td></tr><tr><td class="confluenceTd"><p>Regex find</p></td><td class="confluenceTd"><p>=~</p></td><td class="confluenceTd"><p>Find with a regular expresion? See <a class="confluence-link" href="/display/GROOVY/Regular+Expressions" data-linked-resource-id="2768" data-linked-resource-type="page" data-linked-resource-default-alias="Regular Expressions" data-base-url="http://docs.codehaus.org">Regular Expressions</a></p></td></tr><tr><td class="confluenceTd"><p>Regex match</p></td><td class="confluenceTd"><p>==~</p></td><td class="confluenceTd"><p>Get a match via a regex? See <a class="confluence-link" href="/display/BOO/Regular+Expressions" data-linked-resource-id="17614" data-linked-resource-type="page" data-linked-resource-default-alias="Regular Expressions" data-base-url="http://docs.codehaus.org">Regular Expressions</a></p></td></tr><tr><td class="confluenceTd"><p><a href="http://docs.codehaus.org/display/GROOVY/Operators#Operators-Javafield%28.@%29">Java Field Override </a></p></td><td class="confluenceTd"><p>.@</p></td><td class="confluenceTd"><p>Can be used to override generated properties to provide access to a field</p></td></tr><tr><td class="confluenceTd"><p><a href="http://docs.codehaus.org/display/GROOVY/Operators#Operators-SpreadOperator%28.%29">Spread</a></p></td><td class="confluenceTd"><p>*.</p></td><td class="confluenceTd"><p>Used to invoke an action on all items of an aggregate object</p></td></tr><tr><td class="confluenceTd"><p>Spread Java Field</p></td><td class="confluenceTd"><p>*.@</p></td><td class="confluenceTd"><p>Amalgamation of the above two</p></td></tr><tr><td class="confluenceTd"><p>Method Reference</p></td><td class="confluenceTd"><p>.&</p></td><td class="confluenceTd"><p>Get a reference to a method, can be useful for creating closures from methods</p></td></tr><tr><td class="confluenceTd"><p>asType Operator</p></td><td class="confluenceTd"><p>as</p></td><td class="confluenceTd"><p>Used for groovy casting, coercing one type to another.</p></td></tr><tr><td class="confluenceTd"><p>Membership Operator</p></td><td class="confluenceTd"><p>in</p></td><td class="confluenceTd"><p>Can be used as replacement for collection.contains()</p></td></tr><tr><td class="confluenceTd"><p>Identity Operator</p></td><td class="confluenceTd"><p>is</p></td><td class="confluenceTd"><p>Identity check. Since == is overridden in Groovy with the meaning of equality we need some fallback to check for object identity.</p></td></tr><tr><td class="confluenceTd"><p><a href="http://docs.codehaus.org/display/GROOVY/Operators#Operators-SafeNavigationOperator%28%3F.%29">Safe Navigation</a></p></td><td class="confluenceTd"><p>?.</p></td><td class="confluenceTd"><p>returns nulls instead of throwing NullPointerExceptions</p></td></tr><tr><td class="confluenceTd"><p><a href="http://docs.codehaus.org/display/GROOVY/Operators#Operators-ElvisOperator%28%3F%3A%29">Elvis Operator</a></p></td><td class="confluenceTd"><p>?:</p></td><td class="confluenceTd"><p>Shorter ternary operator</p></td></tr></tbody></table>
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