RyanLynch
RyanLynch

Reputation: 3007

Less than in Groovy case/switch statement

I have the following switch statement

    switch (points) {
       case 0: name = "new"; break;
       case 1..14: badgeName = "bronze-coin"; break;
       case 15..29: badgeName = "silver-coin"; break;
       default: badgeName = "ruby";
    }

I'd like the first case (case 0) to include points less than or equal to 0. How can I do this in Groovy?

Upvotes: 6

Views: 6677

Answers (2)

J. Skeen
J. Skeen

Reputation: 322

switch(points)
{
    case Integer.MIN_VALUE..0: badgeName = "new"; break;
    case 1..14: badgeName = "bronze-coin"; break;
    case 15..29: badgeName = "silver-coin"; break;
    default: badgeName = "ruby";
}

Upvotes: 6

RyanLynch
RyanLynch

Reputation: 3007

case { it instanceof Integer && it < 0 }:

Upvotes: 3

Related Questions