I am very familiar with functional langauges such as Scheme and Haskell. I am trying to solve a problem in Java and struggle, perhaps because I am still in functional thinking.
I want to write:
public void doQueryAndStoreData(String query, <? extends Collection> storeIn) { ResultSet rset = ...; ProcessResultSet proc = new ProcessResultSet(); proc.process(rset, storeIn); }
with an interface like:
private interface IProcessResultSet<C> { public void process(ResultSet rset, C storeIn); }
and a class that implements the interface, for example:
private class ProcessResultSet implements IProcessResultSet<? extends Collection> { public void process(ResultSet rset, Map storeIn) { } public void process(ResultSet rset, List storeIn) { } }
so that the first method can call the appropriate process depending on what type of storeIn it gave.
In Haskell, I could write
class Storeable ca where store :: a -> ca -> ca doQueryAndStoreData :: Storeable ca => ResultSet a -> ca -> ca doQueryAndStoreData (ResultSet rs) coll = foldr store coll rs
and provide Storeable instances for any type of collection in which I want to save my ResultSet .
Is this the right approach in Java? Because I feel that I am somewhat struggling with langauge to accomplish this.
cdk
source share