I’m still not very much used to Java. I have a class which should contain a List. One could imagine this list representing a row in a table (column name, value of certain type):
Name - "Oskar",
Age - 28,
weight - 75.5,
weightUnit - "kilogram",
...
As the content of the list should be flexible, I cannot use class member variables.
Basically, the programmer is blind to the content. But then, at some point – I have to extract all entries with double values. How would you implement something like this?
You could create a class that represents a column, and then represent a row as a list of columns:
Your data would then be:
However, it looks like you’re trying to create a database, so you might want to have a
Tableclass which would have aList<ColumnDescriptor>, whereColumnDescriptorwould be like the aboveColumnclass, only without the value — just the name and type. And then perhaps have aTableRowclass that would have aList<Object>of all the values. When you add a row to a table, you would want to validate that the values are compatible with the types of the columns of that table.You might want to look at the way
JDBCis designed for inspiration. Supporting all the needed generality, functionality, and making it all type-safe is a lot of work. Unless you really need this for anORMtool, custom reporting solution, or an actual database, you might want to rethink the design and ask if this much generality is necessary. (“Do the simplest thing that could possibly work.”)