Need help with generics based error

T

Todd

Hello,

I have a set of classes that I am in the process of making generic. I
don't know how much detail to put into this message, so please ask if
you need more information.

I have two classes at the same level: Trajectory (generic) and
ParkTrajectory. Trajectory takes a ValueType generic argument, i.e.,

public class Trajectory<ValueType> ...

Trajectory has an inner class called TrajectoryJTabbedPane with a
method setInitializationJPanel which takes an InitializationJPanel
argument. The InitializationJPanel class is inner to
TrajectoryJTabbedPane.

public class Trajectory<ValueType>
{
public class TrajectoryJTabbedPane
{
public void setInitializaionJPanel( final InitializationJPanel
ijp )
{
this.ijp = ijp;
}

public class InitializationJPanel{}
}
}

My ParkTrajectory class extends the Trajectory class and worked well
prior to my attempts at making Trajectory generic.

public class ParkTrajectory extends Trajectory<double[]>
{
public class ParkOrbitJTabbedPane extends TrajectoryJTabbedPane
{
protected ParkOrbitJTabbedPane()
{
setInitializationJPanel( new
ParkOrbitVehicleConfigurationJPanel() ); // <-----
}

private final ParkOrbitVehicleConfigurationJPanel extends
InitializationJPanel{}
}
}

Now, the call to setInitializationJPanel within the
ParkOrbitJTabbedPane constructor results in the following error:

setInitializationJPanel(Trajectory<double[]>.InitializationJPanel) in
Trajectory<double[]>.TrajectoryJTabbedPane cannot be applied to
(ParkTrajectory.PparkOrbitJTabbedPane.ParkOrbitVehicleConfigurationJPanel)

Why is this so? The inheritance from InitializationJPanel has not
changed (at least directly). How do I get the method to work with the
generic model?

Any help is appreciated,
Todd
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Todd schreef:
Hello,

I have a set of classes that I am in the process of making generic. I
don't know how much detail to put into this message, so please ask if
you need more information.

We obviously need more information. Google for SSCCE.
I have two classes at the same level: Trajectory (generic) and
ParkTrajectory. Trajectory takes a ValueType generic argument, i.e.,

public class Trajectory<ValueType> ...

Trajectory has an inner class called TrajectoryJTabbedPane with a
method setInitializationJPanel which takes an InitializationJPanel
argument. The InitializationJPanel class is inner to
TrajectoryJTabbedPane.

public class Trajectory<ValueType>
{
public class TrajectoryJTabbedPane
{
public void setInitializaionJPanel( final InitializationJPanel
ijp )
{
this.ijp = ijp;
}

public class InitializationJPanel{}
}
}

You never use ValueType. What is it for?

Is InitializationJPanel generic as well?

You have a typo in this code. NEVER type code into newsgroup posts, but
rather, copy it from your IDE.
My ParkTrajectory class extends the Trajectory class and worked well
prior to my attempts at making Trajectory generic.

public class ParkTrajectory extends Trajectory<double[]>
{
public class ParkOrbitJTabbedPane extends TrajectoryJTabbedPane
{
protected ParkOrbitJTabbedPane()
{
setInitializationJPanel( new
ParkOrbitVehicleConfigurationJPanel() ); // <-----
}

private final ParkOrbitVehicleConfigurationJPanel extends
InitializationJPanel{}
}
}

Now, the call to setInitializationJPanel within the
ParkOrbitJTabbedPane constructor results in the following error:

setInitializationJPanel(Trajectory<double[]>.InitializationJPanel) in
Trajectory<double[]>.TrajectoryJTabbedPane cannot be applied to
(ParkTrajectory.PparkOrbitJTabbedPane.ParkOrbitVehicleConfigurationJPanel)

Similar here: never type over error messages, but copy them over (I
assume your class is not called PparkOrbitJTabbedPane).

I’ll have another look if you give me an SSCCE which I can put into my
IDE, so that it helps me to solve the problem.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iEYEARECAAYFAkjuIW8ACgkQBGFP0CTku6MjcwCeMfPiH7QFNi6i0Vc+xM69AN6i
jwwAn32q7cot9ukp2HL0/UXO1cQZRAyz
=0JFy
-----END PGP SIGNATURE-----
 
T

Todd

Hendrik,

Thanks for responding. I am sorry about typing in the code, however,
the system on which the code resides and the system with which I can
access the internet are not connected - nor can they be.

I will try to find a non-generic way to solve this problem.

Thanks,
Todd
 
L

Lew

Hendrik,

Thanks for responding.  I am sorry about typing in the code, however,
the system on which the code resides and the system with which I can
access the internet are not connected - nor can they be.

I will try to find a non-generic way to solve this problem.

Deciding ahead of time that you are going to be "generic" or "non-
generic" is not good engineering. Naturally, one should prefer
generics whenever they are appropriate. That would be when one needs
to express the same algorithm over an arbitrary range of parametric
types. Hence Hendrik's observation that your generic parameter
(which, by convention and for readability, should have a single upper-
case letter for a name) is not used. That means your algorithm is not
intended to apply to a range of types, so there's no need to specify
such a range.

If you find yourself writing yoru algorithm in terms of a supertype
such as 'Object', then downcasting to get your result, you likely
would benefit from generics, or polymorphism, or both.

The trick in identifying the parametrized type for an algorithm is to
find the part of the algorithm that depends on "some type" being used,
where you don't completely know ahead of time what that type will be
so you need a generic placeholder. That's why they call them
"generics". You explicitly include in the class all those types you
know you'll need, and parametrize the minimum nugget that is generic.

If it's not generic, you don't parametrize it.
 
T

Todd

Lew,

I agree with your statement regarding generic engineering. I
discovered that I would need to have two different types using
identical algorithms, hence I felt generics would be helpful. I did
not indicate ValueType was to be either double[] or
CartesianStateVector as I did not think it salient to the compile time
error.

I also agree that parameters should be single letters, however I was
attempting to clarify the usage for the group.

Thanks for your input,
Todd
 
L

Lew

Lew,

I agree with your statement regarding generic engineering.  I
discovered that I would need to have two different types using
identical algorithms, hence I felt generics would be helpful.  I did
not indicate ValueType was to be either double[] or
CartesianStateVector as I did not think it salient to the compile time
error.

This would have been something useful to say in response to Nendrik's
question about its value to your code.

Also, since you talked about looking for a "non-generic" solution and
your subject line refers to a "generics[-]based error", it is
surprising that you would not see the generic type parameter as
relevant.

The trouble with not providing an SSCCE is that the problem tends to
lie in the part that you omit.

<http://sscce.org/>

From your answer, it seems like generics will be useful to you. You
should seek to understand how they would work here, instead of sour-
graping the issue.
 
T

Tom Anderson

I have two classes at the same level: Trajectory (generic) and
ParkTrajectory. Trajectory takes a ValueType generic argument, i.e.,

public class Trajectory<ValueType> ...

Trajectory has an inner class called TrajectoryJTabbedPane with a
method setInitializationJPanel which takes an InitializationJPanel
argument. The InitializationJPanel class is inner to
TrajectoryJTabbedPane.

public class Trajectory<ValueType>
{
public class TrajectoryJTabbedPane
{
public void setInitializaionJPanel( final InitializationJPanel ijp )
{
this.ijp = ijp;
}

public class InitializationJPanel{}
}
}

My ParkTrajectory class extends the Trajectory class and worked well
prior to my attempts at making Trajectory generic.

public class ParkTrajectory extends Trajectory<double[]>
{
public class ParkOrbitJTabbedPane extends TrajectoryJTabbedPane
{
protected ParkOrbitJTabbedPane()
{
setInitializationJPanel( new ParkOrbitVehicleConfigurationJPanel() ); // <-----
}

private final ParkOrbitVehicleConfigurationJPanel extends InitializationJPanel{}
}
}

Now, the call to setInitializationJPanel within the
ParkOrbitJTabbedPane constructor results in the following error:

setInitializationJPanel(Trajectory<double[]>.InitializationJPanel) in
Trajectory<double[]>.TrajectoryJTabbedPane cannot be applied to
(ParkTrajectory.PparkOrbitJTabbedPane.ParkOrbitVehicleConfigurationJPanel)

Why is this so? The inheritance from InitializationJPanel has not
changed (at least directly). How do I get the method to work with the
generic model?

The code you posted, once the typos were cleaned up and an omission
filled, compiled fine for me. I'm using 1.5.0_16.

If you can post the actual code, we might be able to help you find an
actual problem with it. If the machine with the code isn't online, try a
USB key or a floppy disk or something.

tom
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Todd schreef:
Hendrik,

Thanks for responding. I am sorry about typing in the code, however,
the system on which the code resides and the system with which I can
access the internet are not connected - nor can they be.

Then copy the problem into a file, put it on a usb stick, or whatever,
and copy it in your news post. Be creative, dammit!
I will try to find a non-generic way to solve this problem.

Wouldn’t advise you so. Generics were introduced for a reason.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iEUEARECAAYFAkjvN10ACgkQBGFP0CTku6O5awCWOgLpP5dssMAR4AQrf5SDhXs4
ZwCglmTeGI8V7H+5NMAxi7FeKf0wEQE=
=kx+B
-----END PGP SIGNATURE-----
 
D

Daniel Pitts

Todd said:
Lew,

I agree with your statement regarding generic engineering. I
discovered that I would need to have two different types using
identical algorithms, hence I felt generics would be helpful. I did
not indicate ValueType was to be either double[] or
CartesianStateVector as I did not think it salient to the compile time
error.

I also agree that parameters should be single letters, however I was
attempting to clarify the usage for the group.

Thanks for your input,
Todd
In this case, it sounds more like you want to use Polymorphism rather
than Generics.

Especially since double[] can't possible support the same methods that
CartesianStateVector does, so you'll have to create a Wrapper around a
double[] that handles the methods your algorithm calls.

The main time you really need to create a generic class or method is
when that class or method can accept some object of an unknown type that
it needs to return an object of some specific type based on that generic
information.
 
T

Todd

All,

Thanks again for the responses. The issue with transporting the code
is mechanically simple as stated (USB stick, etc.), but not
practically as the information is on a classified system and requires
mulitple reviews to get it out.

I had hoped that I would be able to present enough information without
having to resort to the arduous transfer process. In the future, I
will do my best to come up with a better example and create an SSCCE
(when possible) before asking for help.

As it stands, I have used an intermediate method to handle the
CartesianStateVector object, stripping out the double[] information
and then passing it along. Essentially, the wrapper that Daniel
mentioned.

Thanks again,
Todd
 
T

Tom Anderson

Thanks again for the responses. The issue with transporting the code
is mechanically simple as stated (USB stick, etc.), but not
practically as the information is on a classified system and requires
mulitple reviews to get it out.

I had hoped that I would be able to present enough information without
having to resort to the arduous transfer process. In the future, I
will do my best to come up with a better example and create an SSCCE
(when possible) before asking for help.

As it stands, I have used an intermediate method to handle the
CartesianStateVector object, stripping out the double[] information
and then passing it along. Essentially, the wrapper that Daniel
mentioned.

Ah, Daniel's idea. Well, at least now if a nuclear missile lands on my
house by mistake, i'll know who to blame.

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top