Reputation: 2061
Just wonder groovy way to do value matching with default like this?
if(params.max != 10 && params.max != 20 && params.max != 30){
params.max = 10
}
Upvotes: 6
Views: 234
Reputation: 2272
params.max = [10, 20, 30].contains(params.max)) ? params.max : 10;
Upvotes: 7
Reputation: 26801
You can also use the Elvis operator (?:) which is useful in this kind of situation. It returns the 2nd value if the first value is null:
params.max = [10, 20, 30].find{ it == params.max } ?: 10
Upvotes: 1