Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>` for lazy field value initialization.
Generates setter methods that accept `Supplier<T>` 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<T>`.

**When ENABLED**:
```java
Expand Down Expand Up @@ -556,8 +558,9 @@ BookDto book = BookDtoBuilder.create()

**Default**: `ENABLED` | **Compiler Option**: `-Asimplebuilder.generateUpdateHelpers=ENABLED|DISABLED`

Generates `xyzUpdate(UnaryOperator<T>)` methods for every builder field. The update helper transforms the
current value and returns the builder for continued fluent chaining.
Generates `xyzUpdate(UnaryOperator<T>)` 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,8 +100,8 @@ public static JacksonIntegrationDtoBuilder create() {
* @param ageSupplier supplier for age
* @return current instance of builder
*/
public JacksonIntegrationDtoBuilder age(Supplier<Integer> ageSupplier) {
this.age = changedValue(ageSupplier.get());
public JacksonIntegrationDtoBuilder age(IntSupplier ageSupplier) {
this.age = changedValue(ageSupplier.getAsInt());
return this;
}

Expand Down Expand Up @@ -142,11 +144,11 @@ public JacksonIntegrationDtoBuilder age(int age) {
* @return current instance of builder
* @throws IllegalStateException if <code>age</code> has not been set yet
*/
public JacksonIntegrationDtoBuilder ageUpdate(UnaryOperator<Integer> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -211,8 +213,8 @@ public OrderWithDefaultsBuilder idUpdate(UnaryOperator<String> idUpdater) {
* @param prioritySupplier supplier for priority
* @return current instance of builder
*/
public OrderWithDefaultsBuilder priority(Supplier<Integer> prioritySupplier) {
this.priority = changedValue(prioritySupplier.get());
public OrderWithDefaultsBuilder priority(IntSupplier prioritySupplier) {
this.priority = changedValue(prioritySupplier.getAsInt());
return this;
}

Expand Down Expand Up @@ -253,11 +255,11 @@ public OrderWithDefaultsBuilder priority(int priority) {
* @return current instance of builder
* @throws IllegalStateException if <code>priority</code> has not been set yet
*/
public OrderWithDefaultsBuilder priorityUpdate(UnaryOperator<Integer> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -327,8 +329,8 @@ public ProductRecordBuilder nameUpdate(UnaryOperator<String> nameUpdater) {
* @param priceSupplier supplier for price
* @return current instance of builder
*/
public ProductRecordBuilder price(Supplier<Double> priceSupplier) {
this.price = changedValue(priceSupplier.get());
public ProductRecordBuilder price(DoubleSupplier priceSupplier) {
this.price = changedValue(priceSupplier.getAsDouble());
return this;
}

Expand Down Expand Up @@ -371,11 +373,11 @@ public ProductRecordBuilder price(double price) {
* @return current instance of builder
* @throws IllegalStateException if <code>price</code> has not been set yet
*/
public ProductRecordBuilder priceUpdate(UnaryOperator<Double> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -115,8 +117,8 @@ public static ProductWithDefaultsBuilder create() {
* @param activeSupplier supplier for active
* @return current instance of builder
*/
public ProductWithDefaultsBuilder active(Supplier<Boolean> activeSupplier) {
this.active = changedValue(activeSupplier.get());
public ProductWithDefaultsBuilder active(BooleanSupplier activeSupplier) {
this.active = changedValue(activeSupplier.getAsBoolean());
return this;
}

Expand Down Expand Up @@ -417,8 +419,8 @@ public ProductWithDefaultsBuilder nameUpdate(UnaryOperator<String> nameUpdater)
* @param priceSupplier supplier for price
* @return current instance of builder
*/
public ProductWithDefaultsBuilder price(Supplier<Double> priceSupplier) {
this.price = changedValue(priceSupplier.get());
public ProductWithDefaultsBuilder price(DoubleSupplier priceSupplier) {
this.price = changedValue(priceSupplier.getAsDouble());
return this;
}

Expand Down Expand Up @@ -463,11 +465,11 @@ public ProductWithDefaultsBuilder price(double price) {
* @return current instance of builder
* @throws IllegalStateException if <code>price</code> has not been set yet
*/
public ProductWithDefaultsBuilder priceUpdate(UnaryOperator<Double> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,8 +53,9 @@
* Generates Supplier-based methods for builder fields.
*
* <p>This generator creates methods that accept {@code Supplier<T>} 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.
*
* <p><b>Important behavior:</b> 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
Expand Down Expand Up @@ -110,9 +116,39 @@ public List<BuilderMethodDto> 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<T>} and invokes it to get the field
* value.
* Creates a supplier method that accepts a {@code Supplier<T>} 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)
Expand All @@ -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 =
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -64,8 +67,9 @@
*
* <p>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<T>} and applies it to the field's current value. Throws {@link
* IllegalStateException} if the value has not been set yet.
* UnaryOperator<T>} — 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.
*
* <p>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.
Expand Down Expand Up @@ -154,8 +158,8 @@ public List<BuilderMethodDto> 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()
Expand All @@ -169,14 +173,15 @@ public List<BuilderMethodDto> 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());
methodDto.addArgument("originalFieldName", originalFieldName);
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 =
Expand Down Expand Up @@ -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).
Expand Down
Loading