tags:

views:

89

answers:

5

What is the best (most compact) way to hand this situation: One or more arguments to a method call depends on some condition, while the rest of the arguments are identical?

For example-- you want to

DeathRay mynewWpn = new DeathRay(particle.proton, chassisColor.BLACK, oem.ACME)

if

enemy_composition == nature.ANTIMATTER

but

DeathRay mynewWpn = new DeathRay(particle.anti-proton, chassisColor.BLACK, oem.ACME)

if

enemy_composition == nature.MATTER

Obviously you can if-else but it looks unnecessarily long when there are a lot of arguments or more than one conditional argument. I have also done this creating an argument with an if-else beforehand and then calling the method. Again, that seems kind of clunky. Is there some sort of inline syntax similar to an Excel if-statement?

+1  A: 

You can use a MAP and the command pattern to avoid the if-else.

For eexample

Map<EnemyCompositionEnum,DeathRay> weapons = new HashMap<EnemyCompositionEnum,DeathRay>();

weapons.put(EnemyCompositionEnum.ANTIMATTER, new DeathRay(particle.proton,BLACK,oem.ACME));
weapons.put(EnemyCompositionEnum.MATTER, new DeathRay(particle.anti-proton,BLACK,oem.ACME));

And then use it

DeathRay weapon = weapons.get(enemy.composition);

update

Ok , I just realized whats an Excel ternary operator by reading other answers.

Tom
I love it, and although it might be overkill compared to a ?: for this simple case, I can see many situations where this would come in handy. I had no idea you could put things like that in the value fields of a map. thanks
Pete
@Pete careful, ?: is the "Elvis" operator, that is available in Groovy and Spring EL. You are probably talking about the ternary operator a ? b : c. http://groovy.codehaus.org/Operators
seanizer
In a Java context, ?: is a perfectly reasonable way of referring to the ternary operator.
Ricky Clarkson
+2  A: 

Yes, it's called the ternary operator ?:

DeathRay mynewWpn = new DeathRay(
    enemy_composition == nature.ANTIMATTER ? particle.proton : particle.anti_proton,
    chassisColor.BLACK, oem.ACME);

The syntax is condition ? value_if_true : value_if_false, and it has the lowest operator precedence, although parentheses are often added to avoid confusion.

Adam Rosenfield
I find that parentheses are often added to show confusion, rather than avoid it.
Ricky Clarkson
+2  A: 

If enemy_composition can only be nature.MATTER or nature.ANTIMATTER then you could use a ternery operator:

DeathRay mynewWpn = new DeathRay(enemy_composition == nature.MATTER ? particle.anti-proton : particle.proton, chassisColor.BLACK, oem.ACME)
wrumsby
+3  A: 
erickson
Might be easier to just pass parameter into enum constructor instead of defining same method 3 times. But goog answer anyway, +1
Nikita Rybak
Yes, in this case it would be. Just wanted to show how to write totally different method for each enum instance.
erickson
+1  A: 

How about redesigning some class?

In Nature class, compose some method like getDestroyer().

abstract class Enemy{
   abstract Weapon getDestroyer();
}

Then in concrete class like :

class MatterEnemy extends Enemy{
   Weapon getDestroyer(){return new DeathRay(antiproton, blabla);}
}

You implement such method. So your main class will be :

public static void main(String... args){
  Enemy enemy = new MatterEnemy();
  Weapon weapon = enemy.getDestroyer();
}

This way you can avoid conditional 'ifs'. Instead, the Enemy itself 'tells' you what weapon should be used to destroy them.

jancrot