J
jrobinss
Hi all,
this is a simple question, so it may be silly...
I am processing structures that contain integers, structures such as matrixes used in statistical analysis. As I implement these as Maps of indexes, Iuse the class Integer, as in
// Map<matrix index, DB index> <- I'd like to remove this comment!
public static Map<Integer, Integer> myMap = ...;
Now what I'd like is to write
public static Map<MatrixIndex, DbIndex> myMap = ...;
The advantage is that the code is auto-documented, but even better that thecompiler will check that I never get mixed up in different indexes, by relying on strong typing.
Usually, I would do this by extending the relevant class. But here it's Integer, which is final.
Questions:
1. is this a bad good idea, and I should proceed with Integer?
2. if not, how would you implement this?
3. is there any performance issue in defining my own classes instead of Integer?
My current solution is to define my own classes for replacing Integer, suchas:
public final class MatrixIndex {
public final int value;
public MyIndex(int i) {value = i;}
}
I won't benefit from autoboxing, then... :-(
(I'm just hoping it doesn't break too much of the code, because even thoughit's mine to break, I don't have infinite time)
Note that (exceptionnally for me) performance *is* an issue here. I haven'tyet narrowed it down, but the code executes very slowly and eats up much too much memory at the moment. I'm starting to optimize it, that's why I'm starting with strongly typing it to prevent errors.
thanks for any tips!
JRobinss
this is a simple question, so it may be silly...
I am processing structures that contain integers, structures such as matrixes used in statistical analysis. As I implement these as Maps of indexes, Iuse the class Integer, as in
// Map<matrix index, DB index> <- I'd like to remove this comment!
public static Map<Integer, Integer> myMap = ...;
Now what I'd like is to write
public static Map<MatrixIndex, DbIndex> myMap = ...;
The advantage is that the code is auto-documented, but even better that thecompiler will check that I never get mixed up in different indexes, by relying on strong typing.
Usually, I would do this by extending the relevant class. But here it's Integer, which is final.
Questions:
1. is this a bad good idea, and I should proceed with Integer?
2. if not, how would you implement this?
3. is there any performance issue in defining my own classes instead of Integer?
My current solution is to define my own classes for replacing Integer, suchas:
public final class MatrixIndex {
public final int value;
public MyIndex(int i) {value = i;}
}
I won't benefit from autoboxing, then... :-(
(I'm just hoping it doesn't break too much of the code, because even thoughit's mine to break, I don't have infinite time)
Note that (exceptionnally for me) performance *is* an issue here. I haven'tyet narrowed it down, but the code executes very slowly and eats up much too much memory at the moment. I'm starting to optimize it, that's why I'm starting with strongly typing it to prevent errors.
thanks for any tips!
JRobinss