Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Support derived fields #441

Open
Open
@asjp1970

Description

@asjp1970

I thought of using the Builder pattern to create a rather complex Object containing a chain of certificates and keys. Our intention is to take from clients of the class the burden to provide specific cryptographic parameters; those parameters (algorithms, sizes of keys, salts, etc.) are always fixed and we want everything set under the hood.

To initialize my object I just need a list of parameters, more or less long, but simple: expiration dates, passwords to do PBEncryption, etc. JCA specific attributes (like private keys and X509 certs) can perfectly remain Optional, since I will provide them for clients using the handful of the other (required parameters).

The problem I've found with FreeBuilder is that I cannot hook in the build() method to build the other (complicated) stuff on the client's behalf. Surely is not a problem, and if it is a conscious decision, please disregard the rest of this idea.

Something like this:

In the generated Builder:

...
public MyClass_Builder build() {
    Preconditions.checkState(_unsetProperties.isEmpty(), "Not set: %s", _unsetProperties);
    buildHook()
    return new Value(this);
  }
...
/**
   * Hook to introduce additional build actions before {@link MyClass_Builder#build()} returns
   * the value of this builder.
   *
   */
protected void buildHook(){}

And in the inner Builder:

@Override
protected void Builder buildHook() {
    // Do here additional stuff to build Optional parameters
  }

The reasons why I think this could be useful are:

  • NOT to allow clients of MyClass access the Optional properties BEFORE I have made sure they have been properly set. I only keep them optional to hide from clients the complexity of providing those properties.
  • It still relies on FreeBuilder checks of the other mandatory properties.
  • There is no way in MyClass_Builder to construct the other parts of it by overriding the setMethods... other than reproducing in that class something similar to the control of the EnumSet, by overriding all setters for mandatory methods, and then only when all of them have been invoked, take the additional (complex) build steps... That would not worth using FreeBuilder.
  • Still the other nice features of FreeBuilder (that we are using) would be available: mapping and merging.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions