Skip to content

Commit de842e7

Browse files
gvdenbrogerg
and
gerg
authored
add concrete setter for optionals in staged required only builder (#204) (#205)
Co-authored-by: gerg <[email protected]>
1 parent 17a5fd0 commit de842e7

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

record-builder-processor/src/main/java/io/soabase/recordbuilder/processor/InternalRecordBuilderProcessor.java

+21
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ private void addStagedBuilderClasses() {
266266
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT).addParameter(parameterSpecBuilder.build())
267267
.addCode(codeBlock).returns(builderClassType.typeName()).build();
268268
classBuilder.addMethod(methodSpec);
269+
270+
if (metaData.addConcreteSettersForOptional()) {
271+
add1ConcreteOptionalSetterMethodToFinalStage(optionalComponent, classBuilder);
272+
}
269273
});
270274

271275
var builderMethod = MethodSpec.methodBuilder(metaData.builderMethodName())
@@ -308,6 +312,23 @@ private void add1StagedBuilderClass(RecordClassType component, Optional<RecordCl
308312
builder.addType(classBuilder.build());
309313
}
310314

315+
private void add1ConcreteOptionalSetterMethodToFinalStage(RecordClassType optionalComponent,
316+
TypeSpec.Builder classBuilder) {
317+
var optionalType = OptionalType.fromClassType(optionalComponent);
318+
if (optionalType.isPresent()) {
319+
var type = optionalType.get();
320+
var concreteCodeBlock = CodeBlock.builder().add("return $L().$L($L);", metaData.builderMethodName(),
321+
optionalComponent.name(), optionalComponent.name()).build();
322+
var concreteParameterSpecBuilder = ParameterSpec.builder(type.valueType(), optionalComponent.name());
323+
var concreteMethodSpec = MethodSpec.methodBuilder(optionalComponent.name())
324+
.addAnnotation(generatedRecordBuilderAnnotation)
325+
.addJavadoc("Call builder for optional component {@code $L}", optionalComponent.name())
326+
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT).addParameter(concreteParameterSpecBuilder.build())
327+
.addCode(concreteCodeBlock).returns(builderClassType.typeName()).build();
328+
classBuilder.addMethod(concreteMethodSpec);
329+
}
330+
}
331+
311332
private void addWithNestedClass() {
312333
/*
313334
* Adds a nested interface that adds withers similar to:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2019 The original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.soabase.recordbuilder.test.staged;
17+
18+
import io.soabase.recordbuilder.core.RecordBuilder;
19+
20+
import java.util.Optional;
21+
22+
@RecordBuilder
23+
@RecordBuilder.Options(builderMode = RecordBuilder.BuilderMode.STAGED_REQUIRED_ONLY, addConcreteSettersForOptional = true)
24+
public record OptionalStagedRequiredOnly(int a, Optional<String> b, String c) {
25+
}

record-builder-test/src/test/java/io/soabase/recordbuilder/test/staged/TestStagedBuilder.java

+12
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,16 @@ void testInitialized() {
9191
InitializedStaged obj = InitializedStagedBuilder.builder().name("foo").age(42).build();
9292
assertEquals(new InitializedStaged(42, "foo"), obj);
9393
}
94+
95+
@Test
96+
void testOptionalStagedRequiredOnlyConcreteSetter() {
97+
OptionalStagedRequiredOnly obj = OptionalStagedRequiredOnlyBuilder.builder().a(1).c("cccc").build();
98+
assertEquals(new OptionalStagedRequiredOnly(1, Optional.empty(), "cccc"), obj);
99+
100+
obj = OptionalStagedRequiredOnlyBuilder.builder().a(1).c("cccc").b(Optional.of("bbbb")).build();
101+
assertEquals(new OptionalStagedRequiredOnly(1, Optional.of("bbbb"), "cccc"), obj);
102+
103+
obj = OptionalStagedRequiredOnlyBuilder.builder().a(1).c("cccc").b("bbbb").build();
104+
assertEquals(new OptionalStagedRequiredOnly(1, Optional.of("bbbb"), "cccc"), obj);
105+
}
94106
}

0 commit comments

Comments
 (0)