9 Replies Latest reply: Apr 2, 2012 9:53 AM by 729242 RSS

    Some help please in understanding JavaFX 2.0 Generics API definition

    278860
      In the Javadocs for JavaFX there is a set of helper/builder classes - for example


      Class LineChartBuilder<X,Y,B extends LineChartBuilder<X,Y,B>>

      which has method definitions (+ others)

      public LineChart<X,Y> build()

      public static <X,Y> LineChartBuilder<X,Y,?> create()

      code snippet
      .......
           //     x-Axis marks
      xAxisList = FXCollections.observableArrayList();
           xAxisList.add("One");
           xAxisList.add("Two");
           xAxisList.add("Three");
           xAxisList.add("Four");
           xAxisList.add("Five");
           CategoryAxis catXAxis = new CategoryAxis(xAxisList);
           
           //     y-Axis
           NumberAxis yAxis = new NumberAxis("Price", 0, 100, 10);

      LineChartBuilder<CategoryAxis, NumberAxis, ?> builder;

      ???? What does the create() invocation look like ??????

           LineChart<X, y> chart = builder.build();
      .......
      code snippet

      any help would be greatly appreciated -
      am still trying to get my mind around how to read these Generics signatures

      Edited by: smaric on Feb 8, 2012 11:47 AM
        • 1. Re: Some help please in understanding JavaFX 2.0 Generics API definition
          rp0428
          Always post your OS and Java versions since things change from version to version.

          The best way to get familiar with generics is to read through the examples in The Java Tutorials:

          http://docs.oracle.com/javase/tutorial/java/generics/index.html

          The more complex constructs that use multiple Type specifications are built on the simpler ones so if you don't understand the simpler cases the complex ones won't make any sense.

          Please read through the above generics topics and then come back with some specific questions; that is, refer to a specific line and tell us what you think it means and then ask if that's correct.
          • 2. Re: Some help please in understanding JavaFX 2.0 Generics API definition
            EJP
            public static <X,Y> LineChartBuilder<X,Y,?> create()
            LineChartBuilder<CategoryAxis, NumberAxis, ?> builder;

            ???? What does the create() invocation look like ??????
            LineChartBuilder<A,B,C> builder = LineChartBuilder.create();
            where A, B, C are whatever you need them to be.
            • 3. Re: Some help please in understanding JavaFX 2.0 Generics API definition
              rp0428
              LineChartBuilder<A,B,C> builder = LineChartBuilder.create();
              So why is there there no mention of the Types in the create itself when they were there in this line above?
              public static <X,Y> LineChartBuilder<X,Y,?> create()
              I'm not clear on the <X, Y> part as the return element myself.
              • 4. Re: Some help please in understanding JavaFX 2.0 Generics API definition
                EJP
                So why is there there no mention of the Types in the create itself when they were there in this line above?
                There can be, e.g. LineChartBuilder.<A,B>create(), but it's not required, by the rules of Generics. Not one I understand BTW, it appears to me to be a prima facie type safety leak.
                Map<String, Object> map = Collections.emptyMap();
                // or
                Map<String, Object> map = Collections.<String, Object>emptyMap();
                is a better-known example.
                I'm not clear on the <X, Y> part as the return element myself.
                They are the type parameters of the required LineChartBuilder object, whatever they are.
                • 6. Re: Some help please in understanding JavaFX 2.0 Generics API definition
                  Darryl Burke
                  Moderator advice: Please read the announcement(s) at the top of the forum listings and the FAQ linked from every page. They are there for a purpose.

                  Then edit your post and format the code correctly.

                  db
                  • 7. Re: Some help please in understanding JavaFX 2.0 Generics API definition
                    278860
                    EJP wrote:
                    public static <X,Y> LineChartBuilder<X,Y,?> create()
                    LineChartBuilder<CategoryAxis, NumberAxis, ?> builder;

                    ???? What does the create() invocation look like ??????
                    LineChartBuilder<A,B,C> builder = LineChartBuilder.create();
                    where A, B, C are whatever you need them to be.
                    LineChartBuilder<A,B,C> builder = LineChartBuilder.create();
                    where A, B, C are whatever you need them to be.
                    I get the Type idea

                    But C needs to be of Type LineChartBuilder<X,Y,B> according to the Class definition
                    public class LineChartBuilder<X,Y,B extends LineChartBuilder<X,Y,B>> extends XYChartBuilder<X,Y,B>
                    and this is where I'm struggling - how do I break out of the B extends part of the definition -- for the following
                    LineChartBuilder<CategoryAxis, NumberAxis, B> builder = LineChartBuilder.create();
                    The above does not compile -
                    B cannot be resolved to a type
                    The method create() is ambiguous for the type LineChartBuilder

                    Thanks for your help
                    • 8. Re: Some help please in understanding JavaFX 2.0 Generics API definition
                      EJP
                      I don't know what the third type parameter is, and the create() method doesn't care, as it specifies ?. So what it is is clearly up to you.

                      EDIT: the rather inadequate Javadoc says 'Class LineChartBuilder<X,Y,B extends LineChartBuilder<X,Y,B>>', so clearly B is the name of the class itself. I would try LineClassBuilder itself here.
                      • 9. Re: Some help please in understanding JavaFX 2.0 Generics API definition
                        729242
                        Any update on this issue? I have a similar trouble with BarCharBuilder.

                        The following piece of code throws an compiler error below:
                        +"reference to create is ambiguous, both method create() in javafx.scene.chart.RegionBuilder and method <X,Y>create() in javafx.scene.chart.BarChartBuilder match"+
                        How can I tell the compiler to apply the create() method defined in BarChartBuilder?
                        BarChart barChart = BarChartBuilder.<String,Number>create().XAxis(CategoryAxisBuilder.create().build()).YAxis(NumberAxisBuilder.create().build()).build();
                        I understand that the same instance can be created without builder classes.
                        BarChart barChart = new BarChart<String, Number>(new CategoryAxis(), new NumberAxis());
                        Thanks