Skip to content

Add isInitialized to immutable builders? #852

Description

@daicoden

Modifiables have an isInitialized method to make sure all required attributes have been set.

As an alternative to @Check could we expose isInitialized in the builder. The main reason @check won't work is if you want to reference instance objects which are not part of the immutable, which can be accessible to the builder.

Use case looks something like this:

@Immutable
public class Example {
  Integer getValue();
  
  public static class Builder extends ImmutableExample.Builder {
    private final Validator validator;

    public Builder(Validator validator) {
      this.validator = validator
    }

    @Override public void build() {
      if (isInitialized) {
        validator.validate(this);
      }

      return super.build();
    }
  }
}

Activity

  1. elucash commented on Oct 18, 2018

    @elucash
    Member

    this may be already implemented IIRC, please look for similarly named style attribute where naming template has empty default value, setting it to non empty should produce the method (again IIRC)

  2. elucash commented on Oct 18, 2018

    @elucash
    Member

    canBuild

  3. daicoden commented on Oct 18, 2018

    @daicoden
    ContributorAuthor

    thank you!!! canBuild indeed is it.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions