diff --git a/annotations/src/main/java/io/jonasg/bob/NotNullableRequiredField.java b/annotations/src/main/java/io/jonasg/bob/NoneNullableValidatableField.java similarity index 79% rename from annotations/src/main/java/io/jonasg/bob/NotNullableRequiredField.java rename to annotations/src/main/java/io/jonasg/bob/NoneNullableValidatableField.java index 7269083..f07cd30 100644 --- a/annotations/src/main/java/io/jonasg/bob/NotNullableRequiredField.java +++ b/annotations/src/main/java/io/jonasg/bob/NoneNullableValidatableField.java @@ -8,7 +8,7 @@ * the type of the required field its value */ @SuppressWarnings("unused") -public final class NotNullableRequiredField implements RequiredField { +public final class NoneNullableValidatableField implements ValidatableField { private T fieldValue; @@ -16,7 +16,7 @@ public final class NotNullableRequiredField implements RequiredField { private final String typeName; - NotNullableRequiredField(T fieldValue, String fieldName, String typeName) { + NoneNullableValidatableField(T fieldValue, String fieldName, String typeName) { this.fieldValue = fieldValue; this.fieldName = fieldName; this.typeName = typeName; diff --git a/annotations/src/main/java/io/jonasg/bob/NullableRequiredField.java b/annotations/src/main/java/io/jonasg/bob/NullableValidatableField.java similarity index 81% rename from annotations/src/main/java/io/jonasg/bob/NullableRequiredField.java rename to annotations/src/main/java/io/jonasg/bob/NullableValidatableField.java index 45ae5ef..9593cad 100644 --- a/annotations/src/main/java/io/jonasg/bob/NullableRequiredField.java +++ b/annotations/src/main/java/io/jonasg/bob/NullableValidatableField.java @@ -7,7 +7,7 @@ * the type of the required field its value */ @SuppressWarnings("unused") -public class NullableRequiredField implements RequiredField { +public class NullableValidatableField implements ValidatableField { private T fieldValue; @@ -17,7 +17,7 @@ public class NullableRequiredField implements RequiredField { private final String typeName; - public NullableRequiredField(T fieldValue, String fieldName, String typeName) { + public NullableValidatableField(T fieldValue, String fieldName, String typeName) { this.fieldValue = fieldValue; this.fieldName = fieldName; this.typeName = typeName; diff --git a/annotations/src/main/java/io/jonasg/bob/RequiredField.java b/annotations/src/main/java/io/jonasg/bob/RequiredField.java deleted file mode 100644 index cb19e5e..0000000 --- a/annotations/src/main/java/io/jonasg/bob/RequiredField.java +++ /dev/null @@ -1,17 +0,0 @@ -package io.jonasg.bob; - -public interface RequiredField { - - static RequiredField notNullableOfNameWithinType(String fieldName, String typeName) { - return new NotNullableRequiredField<>(null, fieldName, typeName); - } - - static RequiredField nullableOfNameWithinType(String fieldName, String typeName) { - return new NullableRequiredField<>(null, fieldName, typeName); - } - - void set(T value); - - T orElseThrow(); - -} diff --git a/annotations/src/main/java/io/jonasg/bob/ValidatableField.java b/annotations/src/main/java/io/jonasg/bob/ValidatableField.java new file mode 100644 index 0000000..378aa04 --- /dev/null +++ b/annotations/src/main/java/io/jonasg/bob/ValidatableField.java @@ -0,0 +1,17 @@ +package io.jonasg.bob; + +public interface ValidatableField { + + static ValidatableField ofNoneNullableField(String fieldName, String typeName) { + return new NoneNullableValidatableField<>(null, fieldName, typeName); + } + + static ValidatableField ofNullableField(String fieldName, String typeName) { + return new NullableValidatableField<>(null, fieldName, typeName); + } + + void set(T value); + + T orElseThrow(); + +} diff --git a/annotations/src/test/java/io/jonasg/bob/RequiredFieldTest.java b/annotations/src/test/java/io/jonasg/bob/ValidatableFieldTest.java similarity index 74% rename from annotations/src/test/java/io/jonasg/bob/RequiredFieldTest.java rename to annotations/src/test/java/io/jonasg/bob/ValidatableFieldTest.java index 5a37b81..10adcb1 100644 --- a/annotations/src/test/java/io/jonasg/bob/RequiredFieldTest.java +++ b/annotations/src/test/java/io/jonasg/bob/ValidatableFieldTest.java @@ -5,15 +5,15 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -class RequiredFieldTest { +class ValidatableFieldTest { @Nested - class NotNullableRequiredFieldTest { + class NoneNullableValidatableFieldTest { @Test void throwExceptionWhenFieldValueIsNotSet() { // given - RequiredField nameField = RequiredField.notNullableOfNameWithinType("name", "Person"); + ValidatableField nameField = ValidatableField.ofNoneNullableField("name", "Person"); // when ThrowingCallable whenOrElseThrowIsCalled = nameField::orElseThrow; @@ -27,7 +27,7 @@ void throwExceptionWhenFieldValueIsNotSet() { @Test void returnFieldValue() { // given - RequiredField nameField = RequiredField.notNullableOfNameWithinType("name", "Person"); + ValidatableField nameField = ValidatableField.ofNoneNullableField("name", "Person"); nameField.set("John"); // when @@ -41,7 +41,7 @@ void returnFieldValue() { @Test void throwExceptionWhenNotNullableRequiredFieldSetToNull() { // given - RequiredField nameField = RequiredField.notNullableOfNameWithinType("name", "Person"); + ValidatableField nameField = ValidatableField.ofNoneNullableField("name", "Person"); nameField.set(null); // when @@ -55,12 +55,12 @@ void throwExceptionWhenNotNullableRequiredFieldSetToNull() { } @Nested - class NullableRequiredFieldTest { + class NullableValidatableFieldTest { @Test void throwExceptionWhenFieldValueIsNotSet() { // given - RequiredField nameField = RequiredField.nullableOfNameWithinType("name", "Person"); + ValidatableField nameField = ValidatableField.ofNullableField("name", "Person"); // when ThrowingCallable whenOrElseThrowIsCalled = nameField::orElseThrow; @@ -74,7 +74,7 @@ void throwExceptionWhenFieldValueIsNotSet() { @Test void returnFieldValue() { // given - RequiredField nameField = RequiredField.nullableOfNameWithinType("name", "Person"); + ValidatableField nameField = ValidatableField.ofNullableField("name", "Person"); nameField.set("John"); // when @@ -88,7 +88,7 @@ void returnFieldValue() { @Test void returnFieldValueWhenSetToNull() { // given - RequiredField nameField = RequiredField.nullableOfNameWithinType("name", "Person"); + ValidatableField nameField = ValidatableField.ofNullableField("name", "Person"); nameField.set(null); // when diff --git a/processor/src/main/java/io/jonasg/bob/BuilderTypeSpecFactory.java b/processor/src/main/java/io/jonasg/bob/BuilderTypeSpecFactory.java index a361233..e80408f 100644 --- a/processor/src/main/java/io/jonasg/bob/BuilderTypeSpecFactory.java +++ b/processor/src/main/java/io/jonasg/bob/BuilderTypeSpecFactory.java @@ -167,14 +167,14 @@ private List generateFields() { protected FieldSpec generateField(BuildableField field) { if (field.isConstructorArgument() && isAnEnforcedConstructorPolicy() || field.isMandatory()) { String methodName = this.strategy().contains(Strategy.ALLOW_NULLS) - ? "nullableOfNameWithinType" - : "notNullableOfNameWithinType"; + ? "ofNullableField" + : "ofNoneNullableField"; return FieldSpec - .builder(ParameterizedTypeName.get(ClassName.get(RequiredField.class), + .builder(ParameterizedTypeName.get(ClassName.get(ValidatableField.class), TypeName.get(boxedType(field.type()))), field.name(), Modifier.PRIVATE, Modifier.FINAL) .initializer("$T.$L(\"" + field.name() + "\", \"" - + this.typeDefinition.typeName() + "\")", RequiredField.class, methodName) + + this.typeDefinition.typeName() + "\")", ValidatableField.class, methodName) .build(); } else { return FieldSpec.builder(TypeName.get(field.type()), field.name(), Modifier.PRIVATE) diff --git a/processor/src/test/resources/tests/Strategies/StepWise/GenerateStepBuilderWithSingleMandatoryAnnotatedField/Expected_DefaultGenerateStepBuilderWithSingleMandatoryAnnotatedFieldBuilder.java b/processor/src/test/resources/tests/Strategies/StepWise/GenerateStepBuilderWithSingleMandatoryAnnotatedField/Expected_DefaultGenerateStepBuilderWithSingleMandatoryAnnotatedFieldBuilder.java index 5b6552b..50d171b 100644 --- a/processor/src/test/resources/tests/Strategies/StepWise/GenerateStepBuilderWithSingleMandatoryAnnotatedField/Expected_DefaultGenerateStepBuilderWithSingleMandatoryAnnotatedFieldBuilder.java +++ b/processor/src/test/resources/tests/Strategies/StepWise/GenerateStepBuilderWithSingleMandatoryAnnotatedField/Expected_DefaultGenerateStepBuilderWithSingleMandatoryAnnotatedFieldBuilder.java @@ -1,12 +1,12 @@ package io.jonasg.bob.test; -import io.jonasg.bob.RequiredField; +import io.jonasg.bob.ValidatableField; import java.lang.String; public final class DefaultGenerateStepBuilderWithSingleMandatoryAnnotatedFieldBuilder implements GenerateStepBuilderWithSingleMandatoryAnnotatedFieldBuilder.BuildStep, GenerateStepBuilderWithSingleMandatoryAnnotatedFieldBuilder { private int year; - private final RequiredField make = RequiredField.notNullableOfNameWithinType("make", "GenerateStepBuilderWithSingleMandatoryAnnotatedField"); + private final ValidatableField make = ValidatableField.ofNoneNullableField("make", "GenerateStepBuilderWithSingleMandatoryAnnotatedField"); private double engineSize; diff --git a/processor/src/test/resources/tests/Strategies/Strict/AllowNulls/AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls/Expected_AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNullsBuilder.java b/processor/src/test/resources/tests/Strategies/Strict/AllowNulls/AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls/Expected_AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNullsBuilder.java index 95e5529..2da7eb0 100644 --- a/processor/src/test/resources/tests/Strategies/Strict/AllowNulls/AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls/Expected_AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNullsBuilder.java +++ b/processor/src/test/resources/tests/Strategies/Strict/AllowNulls/AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls/Expected_AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNullsBuilder.java @@ -1,21 +1,21 @@ package io.jonasg.bob.test; -import io.jonasg.bob.RequiredField; +import io.jonasg.bob.ValidatableField; import java.lang.Boolean; import java.lang.Float; import java.lang.Integer; import java.lang.String; public final class AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNullsBuilder { - private final RequiredField make = RequiredField.nullableOfNameWithinType("make", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls"); + private final ValidatableField make = ValidatableField.ofNullableField("make", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls"); - private final RequiredField year = RequiredField.nullableOfNameWithinType("year", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls"); + private final ValidatableField year = ValidatableField.ofNullableField("year", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls"); private double engineSize; - private final RequiredField isElectric = RequiredField.nullableOfNameWithinType("isElectric", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls"); + private final ValidatableField isElectric = ValidatableField.ofNullableField("isElectric", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls"); - private final RequiredField fuelEfficiency = RequiredField.nullableOfNameWithinType("fuelEfficiency", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls"); + private final ValidatableField fuelEfficiency = ValidatableField.ofNullableField("fuelEfficiency", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls"); public AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNullsBuilder() { } diff --git a/processor/src/test/resources/tests/Strategies/Strict/AllowNulls/ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulls/Expected_ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulsBuilder.java b/processor/src/test/resources/tests/Strategies/Strict/AllowNulls/ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulls/Expected_ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulsBuilder.java index a7c6c05..cc21679 100644 --- a/processor/src/test/resources/tests/Strategies/Strict/AllowNulls/ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulls/Expected_ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulsBuilder.java +++ b/processor/src/test/resources/tests/Strategies/Strict/AllowNulls/ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulls/Expected_ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulsBuilder.java @@ -1,13 +1,13 @@ package io.jonasg.bob.test; -import io.jonasg.bob.RequiredField; +import io.jonasg.bob.ValidatableField; import java.lang.Integer; import java.lang.String; public final class ConstructorMandatoryFieldsCanBeSetToNullWithAllowNullsBuilder { - private final RequiredField make = RequiredField.nullableOfNameWithinType("make", "ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulls"); + private final ValidatableField make = ValidatableField.ofNullableField("make", "ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulls"); - private final RequiredField year = RequiredField.nullableOfNameWithinType("year", "ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulls"); + private final ValidatableField year = ValidatableField.ofNullableField("year", "ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulls"); private double engineSize; diff --git a/processor/src/test/resources/tests/Strategies/Strict/AllowNulls/FieldsDeclaredInBuildableAnnotationCanBeSetToNull/Expected_FieldsDeclaredInBuildableAnnotationCanBeSetToNullBuilder.java b/processor/src/test/resources/tests/Strategies/Strict/AllowNulls/FieldsDeclaredInBuildableAnnotationCanBeSetToNull/Expected_FieldsDeclaredInBuildableAnnotationCanBeSetToNullBuilder.java index fc55f73..bd04097 100644 --- a/processor/src/test/resources/tests/Strategies/Strict/AllowNulls/FieldsDeclaredInBuildableAnnotationCanBeSetToNull/Expected_FieldsDeclaredInBuildableAnnotationCanBeSetToNullBuilder.java +++ b/processor/src/test/resources/tests/Strategies/Strict/AllowNulls/FieldsDeclaredInBuildableAnnotationCanBeSetToNull/Expected_FieldsDeclaredInBuildableAnnotationCanBeSetToNullBuilder.java @@ -1,21 +1,21 @@ package io.jonasg.bob.test; -import io.jonasg.bob.RequiredField; +import io.jonasg.bob.ValidatableField; import java.lang.Boolean; import java.lang.Float; import java.lang.Integer; import java.lang.String; public final class FieldsDeclaredInBuildableAnnotationCanBeSetToNullBuilder { - private final RequiredField make = RequiredField.nullableOfNameWithinType("make", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull"); + private final ValidatableField make = ValidatableField.ofNullableField("make", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull"); - private final RequiredField year = RequiredField.nullableOfNameWithinType("year", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull"); + private final ValidatableField year = ValidatableField.ofNullableField("year", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull"); private double engineSize; - private final RequiredField isElectric = RequiredField.nullableOfNameWithinType("isElectric", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull"); + private final ValidatableField isElectric = ValidatableField.ofNullableField("isElectric", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull"); - private final RequiredField fuelEfficiency = RequiredField.nullableOfNameWithinType("fuelEfficiency", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull"); + private final ValidatableField fuelEfficiency = ValidatableField.ofNullableField("fuelEfficiency", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull"); public FieldsDeclaredInBuildableAnnotationCanBeSetToNullBuilder() { } diff --git a/processor/src/test/resources/tests/Strategies/Strict/ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet/Expected_ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSetBuilder.java b/processor/src/test/resources/tests/Strategies/Strict/ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet/Expected_ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSetBuilder.java index 72d7471..620fdfe 100644 --- a/processor/src/test/resources/tests/Strategies/Strict/ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet/Expected_ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSetBuilder.java +++ b/processor/src/test/resources/tests/Strategies/Strict/ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet/Expected_ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSetBuilder.java @@ -1,16 +1,16 @@ package io.jonasg.bob.test; -import io.jonasg.bob.RequiredField; +import io.jonasg.bob.ValidatableField; import java.lang.Double; import java.lang.Integer; import java.lang.String; public final class ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSetBuilder { - private final RequiredField make = RequiredField.notNullableOfNameWithinType("make", "ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet"); + private final ValidatableField make = ValidatableField.ofNoneNullableField("make", "ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet"); - private final RequiredField year = RequiredField.notNullableOfNameWithinType("year", "ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet"); + private final ValidatableField year = ValidatableField.ofNoneNullableField("year", "ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet"); - private final RequiredField engineSize = RequiredField.notNullableOfNameWithinType("engineSize", "ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet"); + private final ValidatableField engineSize = ValidatableField.ofNoneNullableField("engineSize", "ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet"); private boolean isElectric; diff --git a/processor/src/test/resources/tests/Strategies/Strict/ThrowExceptionWhenMandatoryConstructorFieldsAreNotSet/Expected_ThrowExceptionWhenMandatoryConstructorFieldsAreNotSetBuilder.java b/processor/src/test/resources/tests/Strategies/Strict/ThrowExceptionWhenMandatoryConstructorFieldsAreNotSet/Expected_ThrowExceptionWhenMandatoryConstructorFieldsAreNotSetBuilder.java index b87e537..57b1c77 100644 --- a/processor/src/test/resources/tests/Strategies/Strict/ThrowExceptionWhenMandatoryConstructorFieldsAreNotSet/Expected_ThrowExceptionWhenMandatoryConstructorFieldsAreNotSetBuilder.java +++ b/processor/src/test/resources/tests/Strategies/Strict/ThrowExceptionWhenMandatoryConstructorFieldsAreNotSet/Expected_ThrowExceptionWhenMandatoryConstructorFieldsAreNotSetBuilder.java @@ -1,13 +1,13 @@ package io.jonasg.bob.test; -import io.jonasg.bob.RequiredField; +import io.jonasg.bob.ValidatableField; import java.lang.Integer; import java.lang.String; public final class ThrowExceptionWhenMandatoryConstructorFieldsAreNotSetBuilder { - private final RequiredField make = RequiredField.notNullableOfNameWithinType("make", "ThrowExceptionWhenMandatoryConstructorFieldsAreNotSet"); + private final ValidatableField make = ValidatableField.ofNoneNullableField("make", "ThrowExceptionWhenMandatoryConstructorFieldsAreNotSet"); - private final RequiredField year = RequiredField.notNullableOfNameWithinType("year", "ThrowExceptionWhenMandatoryConstructorFieldsAreNotSet"); + private final ValidatableField year = ValidatableField.ofNoneNullableField("year", "ThrowExceptionWhenMandatoryConstructorFieldsAreNotSet"); private double engineSize;