I'm having a prefuse-table structure, i.e., each row is represented by a tuple and you can access values via table.get(rowNumber, columnName) or via tuple.get(columnName) - whereas get will return an Object there are more specific methods if you know the return type, such as getDouble() or getInt().
Besides numbers the table also stores times, which are available as String, i.e., 08:15 or 20:10. In order to calculate the mean appropriate I have to use the distance between two times (e.g., 11:55 or to be more exact 715 in minutes).
So each number is assigned to an interval length, which is used as a weighting factor, i.e., tuple.getDouble("sales") * 60 / Double.valueOf(interval). Furthermore, it is added up in a variable called mean of the type Number (I used it because of its flexibility to use several number types). So overall, the line looks like mean = mean.doubleValue + tuple.getDouble("sales") * 60 / Double.valueOf(interval);
But not all number columns are of type double and thus, I have the same line for int, float and long. They just look like mean = mean.intValue + tuple.getInt("aColumn") * 60 / Integer.valueOf(interval) and so on. Of course in my case I check which type a column is beforehand and I end up with four ifs and four lines of calculation.
Now I was wondering is there an efficient way to typecast all parameters I need. I can evaluate the type of the column by simply using Class type = column.getColumnType();
I cannot think of a way to design it more efficiently (and better looking), because if I try to use type.cast() within a calculation I'm being told that
the operator * is undefined for the argument type(s) Object, int
Is there any way to typecast such calculations dynamically (probably with different data structures, i.e., not using Number)? It isn't terrible what I have now as it is working, it's just that I'm not really satisfied and it's terrible to look at if another person would continue with this code.