jackysee
jackysee

Reputation: 2061

The Groovy way to do value matching?

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

Answers (2)

M. Utku ALTINKAYA
M. Utku ALTINKAYA

Reputation: 2272

params.max = [10, 20, 30].contains(params.max)) ? params.max : 10;

Upvotes: 7

Ted Naleid
Ted Naleid

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

Related Questions