Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Table of Contents
maxLevel1

Ternary Operator ? x : y

This operator is used to simplify the following syntax:

...

Code Block
titleCase 3: simplified syntax
return product.label ? product.label : 'N/A'

Elvis Operator ?:

This operator is used to provide a default fallback value if certain variable is empty or null. If the expression in the middle of the ternary operator is identical to its first expression, you should simplify the return statement from the previous example above using ?: operator:

...

Info

In case you wonder why this is called Elvis operator: (smile)

?: →  rotate 90° right → → 

Safe Navigation Operator ?.

This operator provides a not-null check before accessing a value from object. Whenever you access object's attribute or method you should check for a non-null reference like this:

...

Code Block
languagegroovy
titleCase 4: multiple occurences for safe navigation operator
return product?.attribute21?.toBigDecimal()

Spread Operator *

Imagine following api.find with many filters:

...

Code Block
languagegroovy
No signature of method: net.pricefx.formulaengine.scripting.SandboxAPI.find() is applicable for argument types: (java.lang.String, java.util.ArrayList) values: [P, [`attribute1` = "P", (`attribute2` = "Red" or `attribute2` = "Green" or `attribute2` = "Blue"), ...]]
Possible solutions: find(), find(java.lang.String, [Lcom.googlecode.genericdao.search.Filter;), find(java.lang.String, int, [Lcom.googlecode.genericdao.search.Filter;), find(groovy.lang.Closure), find(java.lang.String, int, java.lang.String, [Lcom.googlecode.genericdao.search.Filter;), uuid()

Spaceship Operator <=>

This operator delegates to the compareTo method of the left-side operand and passes it the right-side operand.  

...