Finding if value exists in any column recursively Our company uses an old version of JAXB so it does not allow generics. Other than that, I am using recursive calls because Rows can have subrows and I want to find out if any of the rows for the given column id i has any values. 

I am interested to know if I got the recursion right.   

private static boolean anyCellHasValue(CommonRowType row, int i) {

    CustomCellType cell = (CustomCellType)row.getCustomRow().getCell().get(i);

    if(!CELL_EMPTY.equals(cell.getType())) {
        return true ;
    }

    if (row.getChildren() != null &amp;&amp; row.getChildren().getRowData() != null &amp;&amp; 
        !row.getChildren().getRowData().isEmpty()) {
        for (int k = 0; k &lt; row.getChildren().getRowData().size(); k++) {
            if(anyCellHasValue((CommonRowType) row.getChildren().getRowData().get(k), i)) {
                return true;
            }
        }
    }

    return false;
}
This site is more about the readability and structure of code rather than actual correctness. I will comment on that and maybe a more readable program will help you find any potential errors?

I would recommend that you:


Extract complex if statements to their own methods describing the test
Use the new style java iteration instead of the old for loop
Reduce the block nesting level by returning early when you know you have a negative match
Use good naming for all variables. In this case the variable i desperately needs a better name!


This should produce (sans renaming of i) code that look something like this: (not compiled or tested in any way :))

private static boolean anyCellHasValue(CommonRowType row, int i) {
    if (isNonEmptyCellType(row, i)) {
        return true;
    }

    if (!hasChildren(row)) {
        return false;
    }

    for (CommonRowType childRow : row.getChildren().getRowData()) {
        if (anyCellHasValue(childRow, i)) {
            return true;
        }
    }

    return false;
}

private static boolean isNonEmptyCellType(CommonRowType row, int i) {
    CustomCellType cell = (CustomCellType)row.getCustomRow().getCell().get(i);
    return !CELL_EMPTY.equals(cell.getType()));
}

private static boolean hasChildren(CommonRowType row) {
    return row.getChildren() != null 
            &amp;&amp; row.getChildren().getRowData() != null 
            &amp;&amp; !row.getChildren().getRowData().isEmpty();
}