Human above all, with pathos, weaknesses and grumpy at times. Speak for myself; think out loud. Direct, seemingly hard faced. Urged to fix things. Am fortunate.
How To Think Of Jdbc (part 1) Heterogenous Containers
On the preamble we identified a list that allows us to add any type of object and retrieve it back to its actual type. Let’s see how we can benefit from a similar typesafe heterogeneous container in conjuction with JDBC which has been around well before generics.
First thing is to define a Column class which merely describes a table column. All it takes is its name and its type. In JDBC you use a ResultSet to retrieve the value of a column as you iterate through it.
So the Column class not only specifies the column name you need to get the value from but will also give you the right class to cast it to the correct type!
Second is a Row class which is the actual heterogeneous container holding each column and its value. We’ll use that as we iterate through the ResultSet to put the value for each column. This Row instance is what we’ll also use to query for a column and get its value in the actual type.
In general when thinking of such containers there are two things to have in mind.
a Map that’s used as a storage for your heterogeneous type objects
a Class instance to allow you to cast to the actual type