...
Table of Contents | ||
---|---|---|
|
Ternary Operator ? x : y
This operator is used to simplify the following syntax:
...
Code Block | ||
---|---|---|
| ||
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: ?: → 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 | ||||
---|---|---|---|---|
| ||||
return product?.attribute21?.toBigDecimal() |
Spread Operator *
Imagine following api.find with many filters:
...
Code Block | ||
---|---|---|
| ||
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.
...