Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8223712

Investigate more fluent LayoutPath API

    Details

      Description

      The current LayoutPath query API is functional, but a bit clunky to use. Example, consider the following layout:

      Sequence seq = Sequence.of(20,
                      Sequence.of(10,
                            Group.struct(
                                  Padding.of(32),
                                  Value.ofUnsignedInt(32).withName("elem")
                            )
                      ));

      The current API allows you to lookup the layout element with name 'elem' as follows:

      LayoutPath.of(seq).lookup("elem").get() //the result is an optional

      That's a bit of an handy shortcut, based on the assumption that there's only one element called "elem", otherwise, a more convoluted call would have been necessary:

      LayoutPath.of(seq).lookup(l -> l.name().get().equals("elem"))
      //the result is stream
      .findFirst().get()

      A different approach for constructing a path would be, instead, to obtain a root from a layout, and then iteratively 'walk' through all the path elements, until the desired layout is found, in a builder-style idiom:

      seq.toPath()
                      .sequenceElement()
                      .sequenceElement()
                      .groupElement("elem")

      This also scales well to other type of queries (e.g. by index, rather than by name).

        Attachments

          Activity

            People

            • Assignee:
              mcimadamore Maurizio Cimadamore
              Reporter:
              mcimadamore Maurizio Cimadamore
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: