0

I have this function in my GWT project:

private InputElement getInputElement(int rowIndex, int columnIndex, 
CellTable<MyClassA> cellTable) {
        InputElement input = null;
        if (isColumnEditable(columnIndex)) {
            input = (InputElement) cellTable.getRowElement(rowIndex).getCells().getItem(columnIndex).getFirstChild().getFirstChild();
        }  
        return input;
    }

If would like to re-use this function when the last parameter is CellTable<MyClassB>, because the rest of the code is exactly the same. How can I do that?

2
  • are MyClassA and MyClassB are related?? Commented Dec 13, 2012 at 11:05
  • Nope. But for this function should be the same. As you can see, we are not working with MyClass, just with the cellTable functions. Commented Dec 13, 2012 at 11:08

2 Answers 2

1

You can write the following code -

public interface MyClassInterface { ... }

public class MyClassA implements MyClassInterface { ... }

public class MyClassB implements MyClassInterface { ... }

private <T extends MyClassInterface> InputElement getInputElement(int rowIndex, int columnIndex, CellTable<T> cellTable)                    
{             
    InputElement input = null;              
    if (isColumnEditable(columnIndex))  
    {  
        input = (InputElement)  cellTable.getRowElement(rowIndex).getCells().getItem(columnIndex).getFirstChild().getFirstChild();  
    }  
    return input;

}

Sign up to request clarification or add additional context in comments.

Comments

1

You need to make a interface MyClassInterface and implement it in both classes.

public interface MyClassInterface {

}

public class MyClassA implements MyClassInterface {
   ...
}
public class MyClassB implements MyClassInterface {
   ...
}
private InputElement getInputElement(int rowIndex, int columnIndex, 
CellTable<? extends MyClassInterface> cellTable) {
        InputElement input = null;
        if (isColumnEditable(columnIndex)) {
            input = (InputElement) cellTable.getRowElement(rowIndex).getCells().getItem(columnIndex).getFirstChild().getFirstChild();
        }  
        return input;
    }

4 Comments

The method getInputElement(int, int, CellTable<CellTablePresenter.MyClassInterface>) in the type CellTablePresenter is not applicable for the arguments (int, int, CellTable<MyClassA>)
Thanks. Just did what you said. Empty interface and add the "implements MyClassInterface" on both MyClassA and MyClassB. However I get the error above. And a similar one for MyClassB.
You have to make the interface and the classes in separate files.
In the method sig, cellTable probably needs to be of type CellTable<? extends MyClassInterface> or the like.