Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Delegate builder methods in composition of classes #3793

Open
ilia1243 opened this issue Dec 5, 2024 · 0 comments
Open

[FEATURE] Delegate builder methods in composition of classes #3793

ilia1243 opened this issue Dec 5, 2024 · 0 comments

Comments

@ilia1243
Copy link

ilia1243 commented Dec 5, 2024

Describe the feature

Consider two classes with @Builder, one is a field of another. If methods of outer builder could "delegate" to methods of inner builder, it would be possible to build the whole composition in a simpler way.

@Builder
public class Inner {
    private String innerField;
}

@Builder
public class Outer {
    // This tells Lombok to use InnerBuilder inside OuterBuilder
    @BuilderDelegate
    private Inner inner;
    private String outerField;
}

Example usage:

Outer outer = Outer.builder()
            .outerField("outerField")
            .innerField("innerField")
            .build();
Existing way to do the same
        Outer outer = Outer.builder()
                .inner(Inner.builder()
                        .innerField("innerField")
                        .build())
                .outerField("outerField")
                .build();

This can already be achieved by customizing the OuterBuilder with some boiler-plate code:

@Builder
public class Outer {
    private Inner inner;
    private String outerField;

    public static class OuterBuilder {
        private final Inner.InnerBuilder inner = Inner.builder();

        private OuterBuilder inner(Inner.InnerBuilder inner) {
            return null;
        }

        public OuterBuilder innerField(String innerField) {
            inner.innerField(innerField);
            return this;
        }

        public Outer build() {
            return new Outer(this.inner.build(), this.outerField);
        }
    }
}

Related question with the same goal but different implementation https://stackoverflow.com/questions/54309139/delegate-and-builder-combination-in-lombok

@ilia1243 ilia1243 changed the title [FEATURE] [FEATURE] Delegate builders in composition of classes Dec 5, 2024
@ilia1243 ilia1243 changed the title [FEATURE] Delegate builders in composition of classes [FEATURE] Delegate builder methods in composition of classes Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant