Reputation: 187
I'm cutting my teeth on Java (long time C++ guy here), and I'm running into a bit of a problem converting the class Integer to the primitive type int. I've seen several solutions for this, but none appear to work.
What makes my case unique is the conversion takes place inside a generic class (Integer being the generic parameter). The class extends JProgressBar and inherits a custom (templated) interface, TaskObject.
Code for ProgressBar class:
private class ProgressBar<Integer> extends JProgressBar
implements TaskObject<Integer> {
@Override
public void update(Integer val) {
System.out.println("pbar updating to " + val);
setValue(val);
}
}
Code for TaskObject interface:
interface TaskObject<T> {
void update(T value);
}
The ProgressBar instacne is passed as a type of TaskObject to a SwingWorker thread and updated from that thread (hence the update() function).
Unfortunately, setValue won't take a type Integer and converting using (int) fails, along with the Integer-class conversion methods. The specific error notes at the end that "Integer extends object declared in class ...ProgressBar".
It seems obvious that the fact that I'm using Integer as a generic parameter is confusing the compiler in the call to JProgressBar.setValue()... I have half an idea on how to go about resolving this, but it seems like much work for little gain, so I thought I'd throw it out there and see if anyone else had an opinion...
Upvotes: 0
Views: 1384
Reputation: 23268
private class ProgressBar<Integer> extends JProgressBar
implements TaskObject<Integer>
The Integer
used here is actually defining a new type parameter named 'Integer' which has nothing to do with java.lang.Integer
class.
What you currently have is equivalent to:
private class ProgressBar<MyTypeVariable> extends JProgressBar
implements TaskObject<MyTypeVariable> {
@Override
public void update(MyTypeVariable val) {
System.out.println("pbar updating to " + val);
setValue(val);
}
}
Since all you are trying to do is implement the TaskObject
interface with java.lang.Integer
for its type parameter T
, define it as:
private class ProgressBar extends JProgressBar
implements TaskObject<Integer> {
@Override
public void update(Integer val) {
System.out.println("pbar updating to " + val);
setValue(val);
}
}
Now you can call JProgressBar's setValue(int)
method with val
because, thanks to autoboxing the compiler will convert the value automatically.
Upvotes: 1
Reputation: 2488
Your code is syntactically correct, but you have one serious flaw: Why would you chose "Integer" to be your type parameter?
private class ProgressBar<Integer> // <-- here
extends JProgressBar
implements TaskObject<Integer> {
You're using "Integer" in your class methods, when you think you are using Integer
. Get rid of the genericity of ProgressBar
(it is not needed, as JProgressBar
is in fact only accepting numeric values). In other words, in Java you put the template parameter to the class you are deriving from, not the new class. Which make sense, as ProgressBar
is a special kind of a TaskObject
for Integer
s.
tl;dr: ProgressBar
shouldn't be generic, TaskObject
should be.
Upvotes: 0