I took over someone's project and I came across with a function where it takes a condition and checks if that condition has children. If yes it will check if the children are objects or other conditions. If it is an object it will go ahead and do the logic other wise if it is condition it will continue checking what is under it and so on.... I know this is too complicated and I can't explain it more than this but I wrote a similar logic for scenario.
In my example CondtionList
extends the Condition
class and the tree structure looks like this
condition
/ \
object condition
/ | \
object condition object
/ \
object condition
What I am trying to do here is to make a recursive call for multilayered tree like structure Condition
. It works fine until it gets to the return value which calls back my recursive repeatedly before it finishes executing this function.
Here is my code:
private boolean[] isNumberEvenOrOdd(Condition condition){
boolean isEven = false;
boolean isOdd = false;
boolean[] isNumberEvenOrOdd = new boolean[2];
if(condition instanceOf ConditionList) {
for(Condition child : ConditionList.getConditions()){
if (child instanceOf ConditionList){
isNumberEvenOrOdd(child);
}
else{
if((Integer)child % 2 == 0) {
isEven = true;
}
else {
isOdd = true;
}
}
}
}
isNumberEvenOrOdd[0] = isEven;
isNumberEvenOrOdd[1] = isOdd;
return isNumberEvenOrOdd;
}
Thanks in advance for your help.