7 Replies Latest reply: Jul 26, 2010 6:09 AM by 843793 RSS

    How does containership work with Generics?

    843793
      Hi,
      I'm trying to implement a generic tree(each node having any number of children) using generics. It goes like this:

      class GenericNode<T>
      {
      T data;
      List<T> children;
      //Implementations for methods such as addChild(), setData(), removeChild() etc..
      }

      Now I have another class called GenericTree that holds the root node. I want this class to work in conjunction with the GenericNode class.

      class GenericTree<T>
      {
      GenericNode<T> root;
      //methods such as setRoot(), findDepth(GenericNode<T> node)
      }

      The issue here, is that it doesn't make sense to use the generic parameter T along with GenericTree.
      For instance, the declaration
      GenericTree<Integer> doesn't make sense, because the tree itself doesn't store Integer objects, it stores GenericNode<Integer> objects. This isn't sensible, readable code. How do I get around this?
        • 1. Re: How does containership work with Generics?
          843793
          firozjehangir wrote:
          The issue here, is that it doesn't make sense to use the generic parameter T along with GenericTree.
          Why not? You've got a tree containing Integer data. Sounds correct to me.
          For instance, the declaration
          GenericTree<Integer> doesn't make sense, because the tree itself doesn't store Integer objects, it stores GenericNode<Integer> objects.
          What? No, the data in your tree are Integer object. The fact that it stores GenericNode<Integer> objects is an implementation detail about how you store and handle those nodes, but it doesn't change the fact that your tree contains Integer objects.
          This isn't sensible, readable code.
          I disagree.
          • 2. Re: How does containership work with Generics?
            EJP
            I'm trying to implement a generic tree(each node having any number of children) using generics. It goes like this:

            class GenericNode<T>
            {
            T data;
            List<T> children;
            I suggest the problem is right here. I suggest it should be List<GenericNode<T>> children.
            • 3. Re: How does containership work with Generics?
              843793
              @JoachimSauer: Thanks for the quick reply..

              What if, in a general case, I have a lot of containership..i.e.

              I have class A<T>
              {
              T data;
              }

              class B<T>
              {
              A<T> a;
              }

              class C<T>
              {
              B<T> b;
              }

              class D<T>
              {
              C<T> c;
              }

              and so on..In this case, the declaration

              D<Integer> d;

              would not be very clear would it? Wouldn't you assume that D contains Integer objects, while it in fact contains C<Integer> objects, which in turn contain B<Integer> objects and so on.

              Sorry about the lack of clarity and the fact that I'm a noob!
              • 4. Re: How does containership work with Generics?
                843793
                ejp wrote:
                I'm trying to implement a generic tree(each node having any number of children) using generics. It goes like this:

                class GenericNode<T>
                {
                T data;
                List<T> children;
                I suggest the problem is right here. I suggest it should be List<GenericNode<T>> children.
                Sorry about that. It was a mistake. Your'e right. It should be List<GenericNode<T>> children. But my question is in spite of this.
                • 5. Re: How does containership work with Generics?
                  843793
                  firozjehangir wrote:
                  @JoachimSauer: Thanks for the quick reply..

                  What if, in a general case, I have a lot of containership..i.e.
                  I've never heard the word "containership" used in this context.

                  Wouldn't you assume that D contains Integer objects
                  No, I wouldn't.

                  Your view of generics seems to be skewed by the collections framework, which is a pretty straight-forward use of generics, but definitely not the only way to apply them.

                  When I see D<Integer>, I would assume that it's some generic class D that can handle different data types in some way and that this specific type handles Integer values in some way.

                  If, for example it is a Factor<Integer>, then I'd assume it creates Integer objects. If it is a TreeNodeFactory<Integer>, then I'd assume that it creates TreeNode<Integer> objects. That's pretty clear.

                  On the contrary, if it were a TreeNodeFactory<TreeNode<Integer>>, then I would be confused, because that would indicate that a TreeNodeFactory could create something other than TreeNodes.
                  , while it in fact contains C<Integer> objects, which in turn contain B<Integer> objects and so on.
                  Sorry about the lack of clarity
                  It's not too bad, nevermind.
                  and the fact that I'm a noob!
                  Never apologize for not knowing something.
                  • 6. Re: How does containership work with Generics?
                    843793
                    Is there a better way to do this?
                    • 7. Re: How does containership work with Generics?
                      843793
                      firozjehangir wrote:
                      Is there a better way to do this?
                      Since I don't agree with your assessment that there is a problem here, I can't give you a suggestion for a "better way".