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.
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.
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.
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.
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
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With