I am trying to separate return types from database function calls with subclasses of an abstract Output class. I am running into an issue where my abstract method (getResults) return type needs to be different for a ResultSet from a Date, or char, or string but they should all fall under an Output class or interface. The subclass that returns the ResultSet needs to return a List<Map<String,Object>>
, Date should return List<Date>
, String List<String>
, etc...
Client Code:
LoadableStatement loadedStatementObject= new LoadableStatement.Builder()
.addParam(new In(2, 111111))
.addParam(new Out(1, OracleTypes.CURSOR))
.addStatement(DBPackage.LOOKUP_RECORD.getStatement())
.load();
Output cursor = new Cursor(loadedStatementObject);
abstract method in Output.java and subclasses of.
public class Output{
//Constructor, fields etc...
public abstract Collection getResults();
}
public class Cursor extends Output {
private List<Map<String, Object>> results;
public Cursor(LoadableStatement statement) {
super(statement);
}
@Override
public Collection getResults() {
//Logic to convert some type of list from DB into a
//List<Map<String,Object>>
return new ArrayList<Map<String, Object>>();
}
}
public class StringResult extends Output {
public StringResult(LoadableStatement statement) {
super(statement);
}
@Override
public Collection getResults() {
//Logic that converts a list from DB into a List<String>
}
}
How can I accomplish this?