6 Replies Latest reply: Sep 4, 2010 1:10 AM by 843793 RSS

    Type parameter question

    843793
      I'm trying to figure out if I can simplify this interface definition:
      public interface BaseService<T extends BaseEntity<ID>, ID> {
          public T find(ID id);
          public void persist(T entity);
      }
      To implement this interface, I have to do something like:
      public class CountryService implements BaseService<Country, Long> {}
      Where Country is defined like:
      public class Country extends BaseEntity<Long> {}
      My question is: Is there some way to get rid of the second type parameter in BaseService, so that I can do this instead:
      public class CountryService implements BaseService<Country> {}
      but still enforce the type of the id parameter on the find() method in BaseService?

      The obvious solution doesn't compile:

      {color:#ff0000}public interface BaseService<T extends BaseEntity<ID>> {
         public T find(ID id);
         public void persist(T entity);
      }{color}

      Any help is greatly appreciated!
        • 1. Re: Type parameter question
          843793
          Hello e-a-c,

          as long as you want/need to abstract over the type of ID, you will have to live with the second type parameter. You might consider using Scala which supports [Generics of a Higher Kind|http://www.cs.kuleuven.be/~adriaan/files/genericshk/tcpoly.pdf].

          With kind regards
          Ben
          • 2. Re: Type parameter question
            843793
            Hi,

            Another suggestion is to parametrize BaseEntity on a ID hierarchy
            public abstract class BaseEntity<T extends ID> {
                public abstract T getId();
            
            }
            This can lead to the following the BaseService:
            public interface BaseService<T extends BaseEntity<? extends ID>> {
                T find(ID id);
                void persist(T entity);
            
            }
            Now if you define Country as
            public class Country extends BaseEntity<ID> {
                @Override
                public ID getId() {
                    return null;
                }
            
            }
            and finally CountryService as
            public class CountryService implements BaseService<Country> {
            
                @Override
                public Country find(ID id) {
                    return null;
                }
            
                @Override
                public void persist(Country entity) {
                    
                }
            
            }
            Hope this help.
            • 3. Re: Type parameter question
              843793
              ID is a type parameter. There is no ID class.
              • 4. Re: Type parameter question
                796085
                e-a-c wrote:
                ID is a type parameter. There is no ID class.
                Then following the naming conventions so that's obvious would be a good idea, no?
                • 5. Re: Type parameter question
                  794029
                  Where is:

                  "BaseEntity"

                  defined?
                  • 6. Re: Type parameter question
                    843793
                    Write a function that takes as parameters two variables of the type you designed in question 1, and returns "true" if the two structures are equal, and "false" if they are not equal.
                    LeanSpa