Hi MartinHilpert
copying old structures into new generified structures as lord_pixel proposed is one
solution but you have to be aware that you are paying price in terms of performance. If
you had many libraries using this style your generified program would definitely
behave worse than ungenerified one.
I would like to propose another solution. First, I would suggest that instead of using
the old style as it is presented in the following code
/** Old style */
public void addNewTicks1() {
List ticks = Library.getTicks();
Object o = new Tick(new Double(0.), "x", 0, 0);
ticks.add(o);
}
you begin to adopt new style:
- as far as your types are concerned try to be as specific as you can;
- try to remove all references to Objects from your programs.
The result could be the following:
/** New style */
public void addNewTicks2() {
List<Tick> l = Library.getTicks();
Tick t = new Tick(new Double(0.), "abc", 0, 0);
l.add(t);
}
This program will still produce warning but this time it will complain that the function
getTicks() produces unchecked conversion. Since you are "absolutely" sure that there
is nothing but Ticks in that list you can say to yourself that you could live with that
warning. Actuallly, there is no reason why not to trust your library provider. If you would
still want to eliminate that warning there are two possibilities:
1. You could modify your program and add @SuppressWarnings("unchecked") as it
is shown below.
/** New style, wait for Java 5.1, 6.0, ...*/
@SuppressWarnings("unchecked")
public void addNewTicks3() {
List<Tick> l = Library.getTicks();
Tick t = new Tick(new Double(0.), "abc", 0, 0);
l.add(t);
}
Unfortunately this does not work yet.You might kindly ask Peter von der Ahe to
implement that feature as soon as possible. I would warn against using this tactic
beacuse you have to be a master of generics and be 100% sure what are you
doing. You know, don't shoot the messenger...
2. I think that better solution would be to ask your library provider to offer you a generified library:
/** Generified library */
public static List<Tick> getTicks() {
List<Tick> l = new ArrayList<Tick>();
l.add(new Tick(new Double(0.), "def", 10, 10));
return l;
}
If there are many developers who are still using previous versions of java the library
provider might be reluctant to do so - to offer generifed vesion of library -
because it will have to maintain two versions of library. And now we came back to java
developers. We should learn generics, see their benefits and start to use them as
soon as possible The conclusion is that we might benefit more if we ALL move to
new version of java as soon as possible...
Best regards,
Andrei