-
Notifications
You must be signed in to change notification settings - Fork 0
Operators
Groovy uses the same operators as in Java, but adds some others.
The use of .@ forces usage of the field instead of the getter for that field.
Example:
class User {
public final String name
User(String name) { this.name = name}
String getName() { "Name: $name" }
}
def user = new User('Bob')
assert user.name == 'Name: Bob' Here the user.name call triggers a call to the getter for name. If you want to retrieve the field instead of calling the getter, you can use the direct field access operator:
assert user.@name == 'Bob' A more concise ternary operator which says, "if the expression is null, do this."
def sampleText
// Normal ternary operator.
def ternaryOutput = (sampleText != null) ? sampleText : 'Hello Groovy!'
// The Elvis operator in action. We must read: 'If sampleText is not null assign
// sampleText to elvisOuput, otherwise assign 'Viva Las Vegas!' to elvisOutput.
def elvisOutput = sampleText ?: 'Viva Las Vegas!'The method pointer operator (.&) call be used to store a reference to a method in a variable, in order to call it later:
def str = 'example of method reference'
def fun = str.&toUpperCase
def upper = fun()
assert upper == str.toUpperCase()In this example:
- the
strvariable contains a String - we store a reference to the
toUpperCasemethod on thestrinstance inside a variable namedfun -
funcan be called like a regular method
There are multiple advantages in using method pointers. First of all, the type of such a method pointer is a groovy.lang.Closure, so it can be used in any place a closure would be used. In particular, it is suitable to convert an existing method for the needs of the strategy pattern:
def transform(List elements, Closure action) {
def result = []
elements.each {
result << action(it)
}
result
}
String describe(Person p) {
"$p.name is $p.age"
}
def action = this.&describe
def list = [
new Person(name: 'Bob', age: 42),
new Person(name: 'Julia', age: 35)]
assert transform(list, action) == ['Bob is 42', 'Julia is 35'] TBD
TBD
TBD
TBD
While Groovy can use the same operators as Java, operators are all implemented by methods in Groovy. This means we can do operator overriding in our own classes. This is very useful and can make more concise code.
| Operator | Method |
|---|---|
| a + b | a.plus(b) |
| a - b | a.minus(b) |
| a * b | a.multiply(b) |
| a ** b | a.power(b) |
| a / b | a.div(b) |
| a % b | a.mod(b) |
| a | b | a.or(b) |
| a & b | a.and(b) |
| a ^ b | a.xor(b) |
| a++ or ++a | a.next() |
| a-- or --a | a.previous() |
| a[b] | a.getAt(b) |
| a[b] = c | a.putAt(b, c) |
| a << b | a.leftShift(b) |
| a >> b | a.rightShift(b) |
| a >>> b | a.rightShiftUnsigned(b) |
| switch(a) { case(b) : } | b.isCase(a) |
| ~a | a.negate() |
| -a | a.negative() |
| +a | a.positive() |
| a == b | a.equals(b) |
| a != b | ! a.equals(b) |
| a <⇒ b | a.compareTo(b) |
| a > b | a.compareTo(b) > 0 |
| a >= b | a.compareTo(b) >= 0 |
| a < b | a.compareTo(b) < 0 |
| a ⇐ b | a.compareTo(b) ⇐ 0 |
class Money {
def amount
Money plus(Money other) {
new Money(amount: this.amount + other.amount)
}
boolean equals(Object other) {
amount == other.amount
}
int hashCode() {
amount.hashCode()
}
String toString() {
amount
}
}
def m1 = new Money(amount: 100)
def m2 = new Money(amount: 1)
assert (m1 + m2).amount == 101 // plus()
assert m1 + m2 == new Money(amount: 101) // equals() and plus()