3 Replies Latest reply on Apr 22, 2016 4:57 PM by Hristo Stoyanov

    [Erai 4] ListComponent questions

    Hristo Stoyanov Master

      Hi all,

      I am trying to iterate over all components in an instance of org.jboss.errai.databinding.client.components.ListComponent. After all, this is a list of components, so it should be easy ... but is not.

      1. There is the "getComponent(int index)", but there is no corresponding "size()" or "length()" methods to help with for-loop over it.
      2. It does not implement java.lang.Iterable,
      3. nor does it provide a Java 8 "stream()" method.

       

      Maybe I am missing something obvious here, but how can one iterate over ALL components in ListComponent (regardless if they are selected or not)? Or get the total size?

       

      As a temporary workaround I "select" all components (in this case, I do not use the select/deselect functionality, so I can abuse it) and then I use "getSelectedComponents()" to iterate over the returned collection...

       

      Thanks

        • 1. Re: [Erai 4] ListComponent questions
          Max Barkley Expert

          Hi Hristo,

           

          There aren't getSize or iterator methods on ListComponent because it is expected that most manipulation of the ListComponent is done through the List it is bound to. When your ListComponent is bound to a model List, it is an invariant that each model in your model list will have a corresponding component at the same index in the ListComponent. That means you can iterate through all components using a for-loop where you check the index against the model list length, and call getComponent(int index).

          • 2. Re: [Erai 4] ListComponent questions
            Hristo Stoyanov Master

            Hi Max,

            Can we just add 2 convenience methods in the org.jboss.errai.databinding.client.components.ComponentList interface, for example:

             

            default Stream<C> stream(){

               return IntStream

                 .range(0, getValue().size())

                 .mapToObj(i ->getComponent(i));

            }


            default Iterator<C> iterator(){

               return stream().iterator();

            }

             

             

            With a stream/iterator of components, one can easily process tuples of Component/Model.

             

            The only check to be done is if GWT 2.8 SNAPSHOT already provides java.util.stream.IntStream, which should be there by now.

             

            I filed an Errai issues as well for your consideration. Here is my temp workaround:

             

            public interface ErraiUtils {

             

                static <M, C extends TakesValue<M>> Stream<C> stream(ListComponent<M,C> listComponent) {

                    return IntStream

                            .range(0, listComponent.getValue().size())

                            .mapToObj(i -> listComponent.getComponent(i));

                }

             

             

                static <M, C extends TakesValue<M>> Iterator<C> iterator(ListComponent<M,C> listComponent) {

                    return stream(listComponent).iterator();

                }

            }

             

            Thanks.

            • 3. Re: [Erai 4] ListComponent questions
              Hristo Stoyanov Master

              ....Oops IntStream still not available in GWT