innerbuilder icon indicating copy to clipboard operation
innerbuilder copied to clipboard

Allow builders to implement a Builder interface

Open HansBrende opened this issue 7 years ago • 0 comments

For my use case, all builders must implement the IBuilder<O> interface which looks like this:

package com.example;

public interface IBuilder<O> {
    O build();
}

It would be amazing if we could configure the settings so that the generated builder class looks like this:

public final class Thing {
    ...
    public static final class Builder implements com.example.IBuilder<Thing> {
        ...
        @Override
        public Thing build() {
            return new Thing(...);
        }
    }
}

Maybe by specifying a configuration setting like the following: build method = com.example.IBuilder.build

Which would also allow us to do something like this: build method = java.util.function.Supplier.get, generating the following code:

public static final class Builder implements java.util.function.Supplier<Thing> {
    ...
    @Override
    public Thing get() {
        return new Thing(...);
    }
}

Obviously, the specified interface would have to be a single-abstract-method functional interface, with the method having zero arguments.

That being the case, it might be redundant to specify the build method, when you could instead just specify the build interface. So something like this:

build interface = com.example.IBuilder or build interface = java.util.function.Supplier or build interface = org.apache.commons.lang3.builder.Builder would also be great.

HansBrende avatar Mar 05 '18 23:03 HansBrende