Need help with generics based error

Discussion in 'Java' started by Todd, Oct 9, 2008.

  1. Todd

    Todd Guest

    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
    Todd, Oct 9, 2008
    #1
    1. Advertising

  2. -----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-----
    Hendrik Maryns, Oct 9, 2008
    #2
    1. Advertising

  3. Todd

    Todd Guest

    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
    Todd, Oct 9, 2008
    #3
  4. Todd

    Lew Guest

    On Oct 9, 1:22 pm, Todd <> wrote:
    > 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.

    --
    Lew
    Lew, Oct 9, 2008
    #4
  5. Todd

    Todd Guest

    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
    Todd, Oct 9, 2008
    #5
  6. Todd

    Lew Guest

    On Oct 9, 4:16 pm, Todd <> wrote:
    > 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.

    --
    Lew
    Lew, Oct 9, 2008
    #6
  7. Todd

    Tom Anderson Guest

    On Thu, 9 Oct 2008, Todd wrote:

    > 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

    --
    Understanding the universe is the final purpose, as far as I'm
    concerned. -- Ian York
    Tom Anderson, Oct 10, 2008
    #7
  8. -----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-----
    Hendrik Maryns, Oct 10, 2008
    #8
  9. Todd

    Daniel Pitts Guest

    Todd wrote:
    > 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.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Oct 10, 2008
    #9
  10. Todd

    Todd Guest

    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
    Todd, Oct 13, 2008
    #10
  11. Todd

    Tom Anderson Guest

    On Mon, 13 Oct 2008, Todd wrote:

    > 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

    --
    Baby got a masterplan. A foolproof masterplan.
    Tom Anderson, Oct 14, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juergen Berchtel
    Replies:
    1
    Views:
    5,966
    John C. Bollinger
    May 20, 2005
  2. Ian Pilcher

    Need source code w/ lots of generics

    Ian Pilcher, Jul 27, 2005, in forum: Java
    Replies:
    1
    Views:
    305
    Peter Sestoft
    Aug 5, 2005
  3. Ian Pilcher

    Need source code w/ lots of generics

    Ian Pilcher, Jul 27, 2005, in forum: Java
    Replies:
    1
    Views:
    321
    Thomas Weidenfeller
    Jul 27, 2005
  4. Leveridge Systems INC
    Replies:
    1
    Views:
    387
    red floyd
    May 13, 2004
  5. Soul
    Replies:
    0
    Views:
    501
Loading...

Share This Page