Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a more groovy way to implement if..else if..else when checking a multiple instance of an object?

Tags:

groovy

I am currently working on something, and I'm thinking if there's a more groovy way to implement this:

if ( reqObj instanceof Order ) {
    params.body = reqObj
}
else if ( reqObj instanceof PaymentRequest ) {
    params.requestContentType = ContentType.JSON
    params.body = reqObj
}
else if ( reqObj instanceof ShipmentRequest ) {
    params.body = reqObj
}
else if ( reqObj instanceof StockLocationRequest ) {
    params.body = reqObj
}
else if ( reqObj instanceof StockItemRequest ) {
    params.body = reqObj
}
else if ( reqObj instanceof StockMovementRequest ) {
    params.body = reqObj
}
else if ( reqObj instanceof ZoneRequest ) {
    params.body = reqObj
}
else{
    params.query = reqObj
}

as you can see, I am checking multiple instances of an object that does the same thing, But they need to be checked if they are an instance of that class so they don't do the params.query and do params.body if it returns true. Is there a groovier way to do this?

P.S. I would normally search in google but I'm clueless on what keywords to search.

like image 463
nap011 Avatar asked Aug 26 '15 05:08

nap011


People also ask

Does Groovy have else if?

In groovy, if else statement is used when only one condition. In if else statement we have a true as well as false or we can say else block. If the condition is true then the true block is executed otherwise else block is executed.

Is it better to use if or else if?

Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.

Why use else if instead of another if?

Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.


2 Answers

Seems like the work of a switch statement, or at least that is usually what is implemented to avoid forever long if...else if..else if...else type structures. And seeing as groovy can handle objects in the switch statement, you shouldn't have to use in between variables to make it work. So ultimately probably something like this:

switch (reqObj) {

    case {it instanceof Order}:
        result: params.requestContentType = ContentType.JSON
                params.body = reqObj
        break
    ...

    default:
        result: params.query = reqObj
        break
}

this article talks some on the ability to use custom objects in a switch statement with groovy, although I think in their example they are using the toString() method and using the string value for the comparison in the case statements. http://www.javaworld.com/article/2073225/groovy--switch-on-steroids.html

This site on the other hand shows the switch using various object properties including an instanceof statement http://mrhaki.blogspot.com/2009/08/groovy-goodness-switch-statement.html

like image 76
Thacious Avatar answered Nov 16 '22 03:11

Thacious


You can do:

def cls = reqObj.getClass()

if (cls in [Order, PaymentRequest, ]) { //other classess
   params.body = reqObj
} else {
   params.query = reqObj
}

if (cls in [PaymentRequest,]) { // may be instanceof as well
   params.requestContentType = ContentType.JSON
}

A ternary operator can be used as well (however this may be not readable):

(cls in [Order, PaymentRequest,] ? {params.body = reqObj} : {params.query = reqObj})()

if (cls in [PaymentRequest,]) { // may be instanceof as well
   params.requestContentType = ContentType.JSON
}
like image 45
Opal Avatar answered Nov 16 '22 03:11

Opal