diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index a4855275..c674fbe5 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -315,7 +315,9 @@ All options use `OptionState` enum with values: `ENABLED`, `DISABLED`, or `UNSET **Default**: `ENABLED` | **Compiler Option**: `-Asimplebuilder.generateFieldSupplier=ENABLED|DISABLED` -Generates setter methods that accept `Supplier` for lazy field value initialization. +Generates setter methods that accept `Supplier` for lazy field value initialization. Primitive +`boolean`, `int`, `long`, and `double` fields use `BooleanSupplier`, `IntSupplier`, `LongSupplier`, +or `DoubleSupplier` instead; other primitive and wrapper types keep `Supplier`. **When ENABLED**: ```java @@ -556,8 +558,9 @@ BookDto book = BookDtoBuilder.create() **Default**: `ENABLED` | **Compiler Option**: `-Asimplebuilder.generateUpdateHelpers=ENABLED|DISABLED` -Generates `xyzUpdate(UnaryOperator)` methods for every builder field. The update helper transforms the -current value and returns the builder for continued fluent chaining. +Generates `xyzUpdate(UnaryOperator)` methods for every builder field. Primitive `int`, `long`, +and `double` fields use `IntUnaryOperator`, `LongUnaryOperator`, or `DoubleUnaryOperator` instead. +The update helper transforms the current value and returns the builder for continued fluent chaining. ```java PersonDto person = PersonDtoBuilder.create() diff --git a/example/generated-example-builder/org/javahelpers/simple/builders/example/JacksonIntegrationDtoBuilder.java b/example/generated-example-builder/org/javahelpers/simple/builders/example/JacksonIntegrationDtoBuilder.java index af8ba15f..d3cd483c 100644 --- a/example/generated-example-builder/org/javahelpers/simple/builders/example/JacksonIntegrationDtoBuilder.java +++ b/example/generated-example-builder/org/javahelpers/simple/builders/example/JacksonIntegrationDtoBuilder.java @@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import java.util.function.BooleanSupplier; import java.util.function.Consumer; +import java.util.function.IntSupplier; +import java.util.function.IntUnaryOperator; import java.util.function.Supplier; import java.util.function.UnaryOperator; import javax.annotation.processing.Generated; @@ -98,8 +100,8 @@ public static JacksonIntegrationDtoBuilder create() { * @param ageSupplier supplier for age * @return current instance of builder */ - public JacksonIntegrationDtoBuilder age(Supplier ageSupplier) { - this.age = changedValue(ageSupplier.get()); + public JacksonIntegrationDtoBuilder age(IntSupplier ageSupplier) { + this.age = changedValue(ageSupplier.getAsInt()); return this; } @@ -142,11 +144,11 @@ public JacksonIntegrationDtoBuilder age(int age) { * @return current instance of builder * @throws IllegalStateException if age has not been set yet */ - public JacksonIntegrationDtoBuilder ageUpdate(UnaryOperator ageUpdater) { + public JacksonIntegrationDtoBuilder ageUpdate(IntUnaryOperator ageUpdater) { if (!this.age.isSet()) { throw new IllegalStateException("Cannot update 'age' before it is set"); } - this.age = changedValue(ageUpdater.apply(this.age.value())); + this.age = changedValue(ageUpdater.applyAsInt(this.age.value())); return this; } diff --git a/example/generated-example-builder/org/javahelpers/simple/builders/example/OrderWithDefaultsBuilder.java b/example/generated-example-builder/org/javahelpers/simple/builders/example/OrderWithDefaultsBuilder.java index 1890737f..2c9f0b14 100644 --- a/example/generated-example-builder/org/javahelpers/simple/builders/example/OrderWithDefaultsBuilder.java +++ b/example/generated-example-builder/org/javahelpers/simple/builders/example/OrderWithDefaultsBuilder.java @@ -5,6 +5,8 @@ import static org.javahelpers.simple.builders.core.util.TrackedValue.unsetValue; import java.util.function.BooleanSupplier; import java.util.function.Consumer; +import java.util.function.IntSupplier; +import java.util.function.IntUnaryOperator; import java.util.function.Supplier; import java.util.function.UnaryOperator; import javax.annotation.processing.Generated; @@ -211,8 +213,8 @@ public OrderWithDefaultsBuilder idUpdate(UnaryOperator idUpdater) { * @param prioritySupplier supplier for priority * @return current instance of builder */ - public OrderWithDefaultsBuilder priority(Supplier prioritySupplier) { - this.priority = changedValue(prioritySupplier.get()); + public OrderWithDefaultsBuilder priority(IntSupplier prioritySupplier) { + this.priority = changedValue(prioritySupplier.getAsInt()); return this; } @@ -253,11 +255,11 @@ public OrderWithDefaultsBuilder priority(int priority) { * @return current instance of builder * @throws IllegalStateException if priority has not been set yet */ - public OrderWithDefaultsBuilder priorityUpdate(UnaryOperator priorityUpdater) { + public OrderWithDefaultsBuilder priorityUpdate(IntUnaryOperator priorityUpdater) { if (!this.priority.isSet()) { throw new IllegalStateException("Cannot update 'priority' before it is set"); } - this.priority = changedValue(priorityUpdater.apply(this.priority.value())); + this.priority = changedValue(priorityUpdater.applyAsInt(this.priority.value())); return this; } diff --git a/example/generated-example-builder/org/javahelpers/simple/builders/example/ProductRecordBuilder.java b/example/generated-example-builder/org/javahelpers/simple/builders/example/ProductRecordBuilder.java index eb83a499..2a36b613 100644 --- a/example/generated-example-builder/org/javahelpers/simple/builders/example/ProductRecordBuilder.java +++ b/example/generated-example-builder/org/javahelpers/simple/builders/example/ProductRecordBuilder.java @@ -5,6 +5,8 @@ import static org.javahelpers.simple.builders.core.util.TrackedValue.unsetValue; import java.util.function.BooleanSupplier; import java.util.function.Consumer; +import java.util.function.DoubleSupplier; +import java.util.function.DoubleUnaryOperator; import java.util.function.Supplier; import java.util.function.UnaryOperator; import javax.annotation.processing.Generated; @@ -327,8 +329,8 @@ public ProductRecordBuilder nameUpdate(UnaryOperator nameUpdater) { * @param priceSupplier supplier for price * @return current instance of builder */ - public ProductRecordBuilder price(Supplier priceSupplier) { - this.price = changedValue(priceSupplier.get()); + public ProductRecordBuilder price(DoubleSupplier priceSupplier) { + this.price = changedValue(priceSupplier.getAsDouble()); return this; } @@ -371,11 +373,11 @@ public ProductRecordBuilder price(double price) { * @return current instance of builder * @throws IllegalStateException if price has not been set yet */ - public ProductRecordBuilder priceUpdate(UnaryOperator priceUpdater) { + public ProductRecordBuilder priceUpdate(DoubleUnaryOperator priceUpdater) { if (!this.price.isSet()) { throw new IllegalStateException("Cannot update 'price' before it is set"); } - this.price = changedValue(priceUpdater.apply(this.price.value())); + this.price = changedValue(priceUpdater.applyAsDouble(this.price.value())); return this; } diff --git a/example/generated-example-builder/org/javahelpers/simple/builders/example/ProductWithDefaultsBuilder.java b/example/generated-example-builder/org/javahelpers/simple/builders/example/ProductWithDefaultsBuilder.java index 6d350b6c..8cde6653 100644 --- a/example/generated-example-builder/org/javahelpers/simple/builders/example/ProductWithDefaultsBuilder.java +++ b/example/generated-example-builder/org/javahelpers/simple/builders/example/ProductWithDefaultsBuilder.java @@ -5,6 +5,8 @@ import static org.javahelpers.simple.builders.core.util.TrackedValue.unsetValue; import java.util.function.BooleanSupplier; import java.util.function.Consumer; +import java.util.function.DoubleSupplier; +import java.util.function.DoubleUnaryOperator; import java.util.function.Supplier; import java.util.function.UnaryOperator; import javax.annotation.processing.Generated; @@ -115,8 +117,8 @@ public static ProductWithDefaultsBuilder create() { * @param activeSupplier supplier for active * @return current instance of builder */ - public ProductWithDefaultsBuilder active(Supplier activeSupplier) { - this.active = changedValue(activeSupplier.get()); + public ProductWithDefaultsBuilder active(BooleanSupplier activeSupplier) { + this.active = changedValue(activeSupplier.getAsBoolean()); return this; } @@ -417,8 +419,8 @@ public ProductWithDefaultsBuilder nameUpdate(UnaryOperator nameUpdater) * @param priceSupplier supplier for price * @return current instance of builder */ - public ProductWithDefaultsBuilder price(Supplier priceSupplier) { - this.price = changedValue(priceSupplier.get()); + public ProductWithDefaultsBuilder price(DoubleSupplier priceSupplier) { + this.price = changedValue(priceSupplier.getAsDouble()); return this; } @@ -463,11 +465,11 @@ public ProductWithDefaultsBuilder price(double price) { * @return current instance of builder * @throws IllegalStateException if price has not been set yet */ - public ProductWithDefaultsBuilder priceUpdate(UnaryOperator priceUpdater) { + public ProductWithDefaultsBuilder priceUpdate(DoubleUnaryOperator priceUpdater) { if (!this.price.isSet()) { throw new IllegalStateException("Cannot update 'price' before it is set"); } - this.price = changedValue(priceUpdater.apply(this.price.value())); + this.price = changedValue(priceUpdater.applyAsDouble(this.price.value())); return this; } diff --git a/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/field/SupplierMethodGenerator.java b/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/field/SupplierMethodGenerator.java index 2e449be8..54c02906 100644 --- a/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/field/SupplierMethodGenerator.java +++ b/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/field/SupplierMethodGenerator.java @@ -32,12 +32,17 @@ import java.util.Collections; import java.util.List; +import java.util.function.BooleanSupplier; +import java.util.function.DoubleSupplier; +import java.util.function.IntSupplier; +import java.util.function.LongSupplier; import java.util.function.Supplier; import org.javahelpers.codegen.model.javadoc.JavadocDtoBuilder; import org.javahelpers.codegen.model.method.MethodParameterDto; import org.javahelpers.codegen.model.method.MethodParameterDtoBuilder; import org.javahelpers.codegen.model.type.TypeName; import org.javahelpers.codegen.model.type.TypeNameGeneric; +import org.javahelpers.codegen.model.type.TypeNamePrimitive; import org.javahelpers.simple.builders.processor.generators.MethodGenerator; import org.javahelpers.simple.builders.processor.generators.util.BuilderJavadocConstants; import org.javahelpers.simple.builders.processor.model.core.BuilderFieldDto; @@ -48,8 +53,9 @@ * Generates Supplier-based methods for builder fields. * *

This generator creates methods that accept {@code Supplier} functional interfaces for lazy - * initialization of field values. The supplier is invoked immediately when the setter is called, - * and the result is stored in the builder. + * initialization of field values. Primitive {@code boolean}, {@code int}, {@code long}, and {@code + * double} fields use the matching primitive supplier interface instead. The supplier is invoked + * immediately when the setter is called, and the result is stored in the builder. * *

Important behavior: The supplier is evaluated eagerly when the method is called, not * lazily when {@code build()} is invoked. This is useful for deferred initialization, dynamic value @@ -110,9 +116,39 @@ public List generateMethods( return Collections.singletonList(supplierMethod); } + private static TypeName supplierType(TypeName fieldType) { + if (fieldType instanceof TypeNamePrimitive primitive) { + TypeName specialized = + switch (primitive.getType()) { + case BOOLEAN -> TypeName.of(BooleanSupplier.class); + case INT -> TypeName.of(IntSupplier.class); + case LONG -> TypeName.of(LongSupplier.class); + case DOUBLE -> TypeName.of(DoubleSupplier.class); + default -> null; + }; + if (specialized != null) { + return specialized; + } + } + return new TypeNameGeneric(map2TypeName(Supplier.class), fieldType); + } + + private static String supplierAccessor(TypeName fieldType) { + if (fieldType instanceof TypeNamePrimitive primitive) { + return switch (primitive.getType()) { + case BOOLEAN -> "getAsBoolean"; + case INT -> "getAsInt"; + case LONG -> "getAsLong"; + case DOUBLE -> "getAsDouble"; + default -> "get"; + }; + } + return "get"; + } + /** - * Creates a supplier method that accepts a {@code Supplier} and invokes it to get the field - * value. + * Creates a supplier method that accepts a {@code Supplier} or primitive supplier and invokes + * it to get the field value. * * @param fieldName the estimated field name (used for method name) * @param fieldNameInBuilder the builder field name (may be renamed) @@ -129,7 +165,8 @@ private BuilderMethodDto createFieldSupplier( TypeName fieldType, TypeName builderType, BuilderProcessingContext context) { - TypeNameGeneric supplierType = new TypeNameGeneric(map2TypeName(Supplier.class), fieldType); + TypeName supplierType = supplierType(fieldType); + String supplierAccessor = supplierAccessor(fieldType); String parameterName = fieldName + SUFFIX_SUPPLIER; MethodParameterDto parameter = @@ -143,12 +180,13 @@ private BuilderMethodDto createFieldSupplier( methodDto.setCode( """ - this.$fieldName:N = $builderFieldWrapper:T.changedValue($dtoMethodParam:N.get()); + this.$fieldName:N = $builderFieldWrapper:T.changedValue($dtoMethodParam:N.$supplierAccessor:N()); return this; """); methodDto.addArgument("fieldName", fieldNameInBuilder); methodDto.addArgument("dtoMethodParam", parameterName); methodDto.addArgument("builderFieldWrapper", TRACKED_VALUE_TYPE); + methodDto.addArgument("supplierAccessor", supplierAccessor); methodDto.setPriority(BuilderMethodDto.PRIORITY_HIGH); methodDto.setJavadoc( diff --git a/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/field/UpdateHelperGenerator.java b/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/field/UpdateHelperGenerator.java index d47adc10..13ebcb26 100644 --- a/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/field/UpdateHelperGenerator.java +++ b/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/field/UpdateHelperGenerator.java @@ -44,6 +44,9 @@ import java.util.Optional; import java.util.Set; import java.util.TreeSet; +import java.util.function.DoubleUnaryOperator; +import java.util.function.IntUnaryOperator; +import java.util.function.LongUnaryOperator; import java.util.function.UnaryOperator; import org.javahelpers.codegen.model.javadoc.JavadocCodeBlockDto; import org.javahelpers.codegen.model.javadoc.JavadocDtoBuilder; @@ -64,8 +67,9 @@ * *

This generator is enabled by default and can be deactivated by setting the configuration flag * {@code generateUpdateHelpers} to {@code DISABLED}. Each generated method accepts a {@code - * UnaryOperator} and applies it to the field's current value. Throws {@link - * IllegalStateException} if the value has not been set yet. + * UnaryOperator} — or the primitive unary operator for {@code int}, {@code long}, and {@code + * double} — and applies it to the field's current value. Throws {@link IllegalStateException} if + * the value has not been set yet. * *

If a DTO already has a field whose setter has the same signature, the setter wins and the * update helper is skipped with a warning. @@ -154,8 +158,8 @@ public List generateMethods( BuilderFieldDto field, TypeName builderType, BuilderProcessingContext context) { String originalFieldName = field.getOriginalFieldName(); String parameterName = originalFieldName + "Updater"; - TypeNameGeneric updaterType = - new TypeNameGeneric(map2TypeName(UnaryOperator.class), field.getFieldType()); + TypeName updaterType = updaterType(field.getFieldType()); + String applyMethod = applyMethod(field.getFieldType()); BuilderMethodDto methodDto = BuilderMethodDtoBuilder.create() @@ -169,7 +173,7 @@ public List generateMethods( if (!this.$fieldName:N.isSet()) { throw new $illegalStateException:T("Cannot update '$originalFieldName:N' before it is set"); } - this.$fieldName:N = $builderFieldWrapper:T.changedValue($param:N.apply(this.$fieldName:N.value())); + this.$fieldName:N = $builderFieldWrapper:T.changedValue($param:N.$applyMethod:N(this.$fieldName:N.value())); return this; """); methodDto.addArgument("fieldName", field.getFieldNameInBuilder()); @@ -177,6 +181,7 @@ public List generateMethods( methodDto.addArgument("param", parameterName); methodDto.addArgument("builderFieldWrapper", TRACKED_VALUE_TYPE); methodDto.addArgument("illegalStateException", map2TypeName(IllegalStateException.class)); + methodDto.addArgument("applyMethod", applyMethod); methodDto.getMethodCodeDto().addCodeBlockImport(IllegalStateException.class); methodDto.setPriority(BuilderMethodDto.PRIORITY_LOW); String description = @@ -204,6 +209,34 @@ The value must have been set before (directly or via an existing instance). return Collections.singletonList(methodDto); } + private static TypeName updaterType(TypeName fieldType) { + if (fieldType instanceof TypeNamePrimitive primitive) { + TypeName specialized = + switch (primitive.getType()) { + case INT -> TypeName.of(IntUnaryOperator.class); + case LONG -> TypeName.of(LongUnaryOperator.class); + case DOUBLE -> TypeName.of(DoubleUnaryOperator.class); + default -> null; + }; + if (specialized != null) { + return specialized; + } + } + return new TypeNameGeneric(map2TypeName(UnaryOperator.class), fieldType); + } + + private static String applyMethod(TypeName fieldType) { + if (fieldType instanceof TypeNamePrimitive primitive) { + return switch (primitive.getType()) { + case INT -> "applyAsInt"; + case LONG -> "applyAsLong"; + case DOUBLE -> "applyAsDouble"; + default -> "apply"; + }; + } + return "apply"; + } + /** * Returns Javadoc guidance pointing to the builder-consumer overload when such a helper will be * generated for this field (nested DTO or collection of nested DTOs). diff --git a/processor/src/test/java/org/javahelpers/simple/builders/processor/BuilderProcessorTest.java b/processor/src/test/java/org/javahelpers/simple/builders/processor/BuilderProcessorTest.java index b45444e8..f00a0a29 100644 --- a/processor/src/test/java/org/javahelpers/simple/builders/processor/BuilderProcessorTest.java +++ b/processor/src/test/java/org/javahelpers/simple/builders/processor/BuilderProcessorTest.java @@ -832,6 +832,50 @@ public NonEmptyCtorFinals(String name, int age) { ProcessorAsserts.assertNotContaining(generatedCode, "result.setName", "result.setAge"); } + /** Primitive fields use primitive functional interfaces where Java provides them. */ + @Test + void primitiveFunctionalInterfaces() { + JavaFileObject source = + JavaFileObjects.forSourceString( + "test.PrimitiveDto", + """ + package test; + import org.javahelpers.simple.builders.core.annotations.SimpleBuilder; + + @SimpleBuilder + public record PrimitiveDto( + boolean active, + byte small, + short medium, + char initial, + int count, + long timestamp, + float ratio, + double score) {} + """); + + Compilation compilation = compile(source); + String generatedCode = loadGeneratedSource(compilation, "PrimitiveDtoBuilder"); + assertGenerationSucceeded(compilation, "PrimitiveDtoBuilder", generatedCode); + + ProcessorAsserts.assertingResult( + generatedCode, + contains("public PrimitiveDtoBuilder active(BooleanSupplier activeSupplier)"), + contains("public PrimitiveDtoBuilder small(Supplier smallSupplier)"), + contains("public PrimitiveDtoBuilder medium(Supplier mediumSupplier)"), + contains("public PrimitiveDtoBuilder initial(Supplier initialSupplier)"), + contains("public PrimitiveDtoBuilder count(IntSupplier countSupplier)"), + contains("public PrimitiveDtoBuilder timestamp(LongSupplier timestampSupplier)"), + contains("public PrimitiveDtoBuilder ratio(Supplier ratioSupplier)"), + contains("public PrimitiveDtoBuilder score(DoubleSupplier scoreSupplier)"), + contains("countSupplier.getAsInt()"), + contains("public PrimitiveDtoBuilder activeUpdate(UnaryOperator activeUpdater)"), + contains("public PrimitiveDtoBuilder countUpdate(IntUnaryOperator countUpdater)"), + contains("public PrimitiveDtoBuilder timestampUpdate(LongUnaryOperator timestampUpdater)"), + contains("public PrimitiveDtoBuilder scoreUpdate(DoubleUnaryOperator scoreUpdater)"), + contains("countUpdater.applyAsInt(this.count.value())")); + } + @Test void shouldHandleFieldsOfConstructorAndSetters() { // Given @@ -867,7 +911,7 @@ void shouldHandleFieldsOfConstructorAndSetters() { "public CtorAndSetterBuilder name(String name)", // helpermethod for setter "public CtorAndSetterBuilder name(Supplier nameSupplier)", // supplier for setter "public CtorAndSetterBuilder a(int a)", // helpermethod for constructor param - "public CtorAndSetterBuilder a(Supplier aSupplier)", // supplier for constructor + "public CtorAndSetterBuilder a(IntSupplier aSupplier)", // supplier for constructor "CtorAndSetter result = new CtorAndSetter(this.a.value());", "this.name.ifSet(result::setName);"); } @@ -1063,7 +1107,7 @@ public RequiredFieldViaConstructor(String name, int age) { "public RequiredFieldViaConstructorBuilder age(int age)", // Supplier methods for constructor fields (the bug we fixed!) "public RequiredFieldViaConstructorBuilder name(Supplier nameSupplier)", - "public RequiredFieldViaConstructorBuilder age(Supplier ageSupplier)", + "public RequiredFieldViaConstructorBuilder age(IntSupplier ageSupplier)", // Setter field should also have supplier "public RequiredFieldViaConstructorBuilder description(String description)", "public RequiredFieldViaConstructorBuilder description(String format, Object... args)", @@ -2466,7 +2510,7 @@ void shouldNotGenerateConsumersForPrimitiveFields() { // Primitives should not get consumer methods, but arrays now get ArrayListBuilder consumers ProcessorAsserts.assertingResult( generatedCode, - contains("public PrimAndArrayBuilder count(Supplier"), + contains("public PrimAndArrayBuilder count(IntSupplier countSupplier)"), contains("public PrimAndArrayBuilder names(Supplier"), notContains("public PrimAndArrayBuilder count(Consumer"), contains("public PrimAndArrayBuilder names(Consumer>")); @@ -2648,9 +2692,9 @@ void shouldHandleMixedFields() { // Expect signatures for all supported setter patterns per field kind ProcessorAsserts.assertingResult( generatedCode, - // primitive int: direct and supplier (boxed) + // primitive int: direct and specialized supplier contains("public HasMixedBuilder a(int a)"), - contains("public HasMixedBuilder a(Supplier aSupplier)"), + contains("public HasMixedBuilder a(IntSupplier aSupplier)"), // array String[]: varargs, supplier, List parameter, and ArrayListBuilder consumer contains("public HasMixedBuilder names(String... names)"), contains("public HasMixedBuilder names(Supplier namesSupplier)"), @@ -2864,10 +2908,10 @@ void shouldBoxPrimitiveTypeArguments() { notContains(""), notContains(""), notContains(""), - contains("public HasConsumerBuilder intValue(Supplier intValueSupplier)"), - contains("public HasConsumerBuilder longValue(Supplier longValueSupplier)"), - contains("public HasConsumerBuilder doubleValue(Supplier doubleValueSupplier)"), - contains("public HasConsumerBuilder booleanValue(Supplier booleanValueSupplier)")); + contains("public HasConsumerBuilder intValue(IntSupplier intValueSupplier)"), + contains("public HasConsumerBuilder longValue(LongSupplier longValueSupplier)"), + contains("public HasConsumerBuilder doubleValue(DoubleSupplier doubleValueSupplier)"), + contains("public HasConsumerBuilder booleanValue(BooleanSupplier booleanValueSupplier)")); } @Test diff --git a/processor/src/test/java/org/javahelpers/simple/builders/processor/ComprehensiveFeatureIntegrationTest.java b/processor/src/test/java/org/javahelpers/simple/builders/processor/ComprehensiveFeatureIntegrationTest.java index b33249f2..ebf39673 100644 --- a/processor/src/test/java/org/javahelpers/simple/builders/processor/ComprehensiveFeatureIntegrationTest.java +++ b/processor/src/test/java/org/javahelpers/simple/builders/processor/ComprehensiveFeatureIntegrationTest.java @@ -90,6 +90,8 @@ void allFeatures_generatedCorrectly() { import java.util.Set; import java.util.function.BooleanSupplier; import java.util.function.Consumer; + import java.util.function.IntSupplier; + import java.util.function.IntUnaryOperator; import java.util.function.Supplier; import java.util.function.UnaryOperator; import javax.annotation.processing.Generated; @@ -351,8 +353,8 @@ public PersonDtoBuilder addressUpdate(UnaryOperator addressUpdater) * @param ageSupplier supplier for age * @return current instance of builder */ - public PersonDtoBuilder age(Supplier ageSupplier) { - this.age = changedValue(ageSupplier.get()); + public PersonDtoBuilder age(IntSupplier ageSupplier) { + this.age = changedValue(ageSupplier.getAsInt()); return this; } @@ -399,11 +401,11 @@ public PersonDtoBuilder age(int age) { * @return current instance of builder * @throws IllegalStateException if age has not been set yet */ - public PersonDtoBuilder ageUpdate(UnaryOperator ageUpdater) { + public PersonDtoBuilder ageUpdate(IntUnaryOperator ageUpdater) { if (!this.age.isSet()) { throw new IllegalStateException("Cannot update 'age' before it is set"); } - this.age = changedValue(ageUpdater.apply(this.age.value())); + this.age = changedValue(ageUpdater.applyAsInt(this.age.value())); return this; } diff --git a/processor/src/test/java/org/javahelpers/simple/builders/processor/FormattingModeTest.java b/processor/src/test/java/org/javahelpers/simple/builders/processor/FormattingModeTest.java index b44f08c0..9a818307 100644 --- a/processor/src/test/java/org/javahelpers/simple/builders/processor/FormattingModeTest.java +++ b/processor/src/test/java/org/javahelpers/simple/builders/processor/FormattingModeTest.java @@ -104,6 +104,8 @@ public int getCount() { import static org.javahelpers.simple.builders.core.util.TrackedValue.unsetValue; import java.util.function.BooleanSupplier; import java.util.function.Consumer; + import java.util.function.IntSupplier; + import java.util.function.IntUnaryOperator; import java.util.function.Supplier; import java.util.function.UnaryOperator; import javax.annotation.processing.Generated; @@ -183,8 +185,8 @@ public static FormatTestDtoBuilder create() { * @param countSupplier supplier for count * @return current instance of builder */ - public FormatTestDtoBuilder count(Supplier countSupplier) { - this.count = changedValue(countSupplier.get()); + public FormatTestDtoBuilder count(IntSupplier countSupplier) { + this.count = changedValue(countSupplier.getAsInt()); return this; } @@ -216,11 +218,11 @@ public FormatTestDtoBuilder count(int count) { * @return current instance of builder * @throws IllegalStateException if count has not been set yet */ - public FormatTestDtoBuilder countUpdate(UnaryOperator countUpdater) { + public FormatTestDtoBuilder countUpdate(IntUnaryOperator countUpdater) { if (!this.count.isSet()) { throw new IllegalStateException("Cannot update 'count' before it is set"); } - this.count = changedValue(countUpdater.apply(this.count.value())); + this.count = changedValue(countUpdater.applyAsInt(this.count.value())); return this; } diff --git a/processor/src/test/java/org/javahelpers/simple/builders/processor/UpdateHelperGeneratorTest.java b/processor/src/test/java/org/javahelpers/simple/builders/processor/UpdateHelperGeneratorTest.java index e63000e9..8af3635f 100644 --- a/processor/src/test/java/org/javahelpers/simple/builders/processor/UpdateHelperGeneratorTest.java +++ b/processor/src/test/java/org/javahelpers/simple/builders/processor/UpdateHelperGeneratorTest.java @@ -49,15 +49,15 @@ void updateHelpers_DefaultEnabled_GeneratesMethodsWithSignaturesGuardAndExamples ProcessorAsserts.assertContaining( generated, "public PersonDtoBuilder nameUpdate(UnaryOperator nameUpdater)"); ProcessorAsserts.assertContaining( - generated, - "public PersonDtoBuilder quantityUpdate(UnaryOperator quantityUpdater)"); + generated, "public PersonDtoBuilder quantityUpdate(IntUnaryOperator quantityUpdater)"); // Unset-field guard and update code ProcessorAsserts.assertContaining(generated, "if (!this.name.isSet())"); ProcessorAsserts.assertContaining( generated, "throw new IllegalStateException(\"Cannot update 'name' before it is set\")"); ProcessorAsserts.assertContaining( - generated, "this.quantity = changedValue(quantityUpdater.apply(this.quantity.value()));"); + generated, + "this.quantity = changedValue(quantityUpdater.applyAsInt(this.quantity.value()));"); // Method-level Javadoc examples initialize the field, then update it ProcessorAsserts.assertContaining(