5 Replies Latest reply: Jul 19, 2010 4:38 AM by 843793 RSS

    Small Bug in JavaGenericsFaq Book

    843793
      Angelika Langer's JavaGenericsFaq [http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html] Book


      In Page: 101 of Java Generics FAQs Book Below method should be


      public final class Wrapper<T> {
      private final T theObject;
      public Wrapper(T t) { theObject = t; }
      public T getWrapper() { return theObject; }

      private final class WrapperComparator<W extends Wrapper<? extends
      Comparable<T>>>
      implements Comparator<W> {
      public int compare(W lhs, W rhs) {
      return lhs.theObject.compareTo((T)(rhs.theObject));
      }
      *}*
      public <V extends Wrapper<? extends Comparable<T>>> Comparator<V>
      comparator() {
      return this.new WrapperComparator<V>();
      }



      INSTEAD OF


      public final class Wrapper<T> {
      private final T theObject;
      public Wrapper(T t) { theObject = t; }
      public T getWrapper() { return theObject; }

      private final class WrapperComparator<W extends Wrapper<? extends
      Comparable<T>>>
      implements Comparator<W> {
      public int compare(W lhs, W rhs) {
      return lhs.theObject.compareTo((T)(rhs.theObject));
      }
      public <V extends Wrapper<? extends Comparable<T>>> Comparator<V>
      comparator() {
      return this.new WrapperComparator<V>();
      }
      *}*



      But in the current code in the book it is ending after comparator() method which will end up in compilation error.
        • 1. Re: Small Bug in JavaGenericsFaq Book
          791266
          Please, use code formatting when you post code.

          Btw. Shouldn't you report that over there instead of here?
          • 2. Re: Small Bug in JavaGenericsFaq Book
            EJP
            You seem to have it back to front. The 2nd-last } closes the comparator() method and the final } closes the class.
            • 3. Re: Small Bug in JavaGenericsFaq Book
              843793
              Dear ejp,

              Can you please try out yourself in eclipse and let us know your comments please?

              I have posted this problem in the forum also so that members also will be aware.

              I will take care of code formatting tips from my next thread.

              Thanks
              Prashant
              • 4. Re: Small Bug in JavaGenericsFaq Book
                EJP
                Can you please try out yourself in eclipse
                No.

                Next question:
                and let us know your comments please?
                I have already done that. I can see by inspection that the code you posted under 'instead of' has the correct number of closing braces, and the code you posted above it under 'shoud be', i.e. as the apparent correction does not. I don't need Eclipse for that. I don't even need a Java compiler. I know the syntax.

                Either (i) you have mislabeled these two pieces of code and you intended to state that the second one was correct, or (ii) you are wrong.
                • 5. Re: Small Bug in JavaGenericsFaq Book
                  843793
                  prasanth_vijayawada wrote:
                  Angelika Langer's JavaGenericsFaq [http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html] Book


                  In Page: 101 of Java Generics FAQs Book Below method should be
                  In fact both pieces of code you showed are wrong. It needs to be this:
                  public final class Wrapper<T> {
                       private final T theObject;
                  
                       public Wrapper(final T t) {
                            theObject = t;
                       }
                  
                       public T getWrapper() {
                            return theObject;
                       }
                  
                       private final class WrapperComparator<W extends Wrapper<? extends Comparable<T>>> implements Comparator<W> {
                            public int compare(final W lhs, final W rhs) {
                                 return lhs.theObject.compareTo((T) (rhs.theObject));
                            }
                       } // this is missing in the PDF
                  
                       public <V extends Wrapper<? extends Comparable<T>>> Comparator<V> comparator() {
                            return this.new WrapperComparator<V>();
                       }
                  } // this is missing in your "correct" example.
                  So the code in the PDF is missing the closing brace of the WrapperComparator (just after the closing brace after the compare() method).
                  And your "corrected" code added that, but removed the necessary closing brace at the ed (just after the closing brace after the comparator() method).