Skip to content

Instantly share code, notes, and snippets.

@ceharris
Last active November 23, 2016 14:40
Show Gist options
  • Save ceharris/8a8a76bec494c1490fd188232b7c1378 to your computer and use it in GitHub Desktop.
Save ceharris/8a8a76bec494c1490fd188232b7c1378 to your computer and use it in GitHub Desktop.
prospecto collection syntax improvement

This is the current syntax...

mapOfObjects(Role.class, Contributor.class)
    .reference("person", PERSON_SUMMARY_TEMPLATE)
    .arrayOfObjects("credits")
          .reference("responsibleOrganization", ORG_SUMMARY_TEMPLATE)
          .value("percentCredit")
          .end()

What if we separate the method that specifies a collection (e.g. array or map) from the thing it contains? For example, we could have a syntax like this...

map(Role.class)
    .object(Contributor.class)
        .reference("person", PERSON_SUMMARY_TEMPLATE)
        .array("credits")
            .object(Credit.class)
                .reference("responsibleOrganization", ORG_SUMMARY_TEMPLATE)
                .value("percentCredit")
                .end()

The map and array methods create a builder that has a just a few methods on it.

interface CollectionBuilder {
  
  CollectionBuilder map(...);

  CollectionBuilder array(...);

  ViewTemplateBuilder object(...);

  ViewTemplateBuilder value(...);

}

The cool new thing this approach allows is collection composition.

map(Role.class)
    .array()
        .object(Contributor.class)
            .reference("person", PERSON_SUMMARY_TEMPLATE)
            .array("credits")
                .object(Credit.class)
                    .reference("responsibleOrganization", ORG_SUMMARY_TEMPLATE)
                    .value("percentCredit")
                    .end()

So I can have a map whose entries are arrays, or an array of arrays, or an array of maps, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment