Skip to content

Sum with Integer object if not null

I do not know if it is possible in Java but I need a method to do a special “add function”. Let me explain,

SHORT VERSION: Is it possible to add an int with an Integer object? I need a method to check if that integer object exists, and add 0 if the object is null, add the correct value otherwise I tried

public static Integer isNull(Integer bonus){
    if (bonus == null)
        return Integer.valueOf(0);
    else
        return bonus;
}

but I can’t enter the “null” branch.

FULL VERSION: The Integer Bonus value is the result of a fired Drools rule. This rule return this Integer Bonus with the insertLogical(new object) statement. According the drools manual,

insertLogical(new Something()); is similar to insert, but the object will be automatically retracted when there are no more facts to support the truth of the currently firing rule. In other words, as soon as the condition of that rule is not true any more, the Engine retracts the logically inserted fact. This is commonly referred to as “truth maintenance”

So as long as the rule is valid the Bonus object exists and the normal sum is valid; but when the rules is retracted and the Bonus destroyed I need that sum to continue to be valid

In addiction, I can say I’m trying to implement the Magic The Gathering card game with Drools; so I’m trying to code cards like “when something, so something”. With insertLogical I guessed I only have to care about the activation of the rule and not of its removal.

I hope my question is quite clear, and sorry for my bad English

BETTER EXPLANATION (maybe): It is not so simple, the card has an Integer value, and sometimes, varying on the rules which are fired, this value can change. I don’t want to manually change the value, because there can be many and many ways this value can change, so I need the insertLogical() to at lest don’t care about changing again those values when the rule does not exists anymore.

EXAMPLE: I have a card in play which as the value 3. Then I play another card which increase that value to 4, but only as long as this second card remains in play. Normally, with the Drools insert statement I have to write some code which increase the value field of that card (which is mapped into a Java Class) and then some code which decrease back that value when the second card is not in play anymore. With the insertLogical (if I understood it correctly) I have the decrease code for free. The problem is inserteLogical only apply to new java Objects (not existings ones or most of all not to objects fields); so I declare a new Bonus object (a positive or negative Integer) I can add to card value, but it can be destroyed at any time and the “add” function must be still valid.

when the second card is played (or it is already and I play the first) I have to modify the first card value like this

card.value=card.value + isNull(bonus)

so it can automatically change if a bonus (the second card) exist or not

Answer

If I have correctly understood what you want, here is an example of what you could do

/**
 * Take first value of a String [] as an Integer, and return null if not Ok
 * @param args a String[] that could come from main
 * @return integer value of args[0] or null
 */
private static Integer getArgs0(String[] args) {
    Integer bonus = null;
    if (args.length > 0) { // special case no args
        try {
            bonus = Integer.parseInt(args[0]);
        }
        catch(NumberFormatException e) {}
    }
    return bonus;
}

/**
 * Convert a null Integer to 0 - else leave it untouched
 * @param bonus input Integer
 * @return 0 if bonus is null else bonus
 */
private static int zeroIfNull(Integer bonus) {
    return (bonus == null) ? 0 : bonus;
}