Skip to content

Commit

Permalink
chore: rename RequiredField to ValidatableField
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-grgt committed Apr 24, 2024
1 parent d6f1cfe commit 231e777
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
* the type of the required field its value
*/
@SuppressWarnings("unused")
public final class NotNullableRequiredField<T> implements RequiredField<T> {
public final class NoneNullableValidatableField<T> implements ValidatableField<T> {

private T fieldValue;

private final String fieldName;

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* the type of the required field its value
*/
@SuppressWarnings("unused")
public class NullableRequiredField<T> implements RequiredField<T> {
public class NullableValidatableField<T> implements ValidatableField<T> {

private T fieldValue;

Expand All @@ -17,7 +17,7 @@ public class NullableRequiredField<T> implements RequiredField<T> {

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;
Expand Down
17 changes: 0 additions & 17 deletions annotations/src/main/java/io/jonasg/bob/RequiredField.java

This file was deleted.

17 changes: 17 additions & 0 deletions annotations/src/main/java/io/jonasg/bob/ValidatableField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.jonasg.bob;

public interface ValidatableField<T> {

static <T> ValidatableField<T> ofNoneNullableField(String fieldName, String typeName) {
return new NoneNullableValidatableField<>(null, fieldName, typeName);
}

static <T> ValidatableField<T> ofNullableField(String fieldName, String typeName) {
return new NullableValidatableField<>(null, fieldName, typeName);
}

void set(T value);

T orElseThrow();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> nameField = RequiredField.notNullableOfNameWithinType("name", "Person");
ValidatableField<String> nameField = ValidatableField.ofNoneNullableField("name", "Person");

// when
ThrowingCallable whenOrElseThrowIsCalled = nameField::orElseThrow;
Expand All @@ -27,7 +27,7 @@ void throwExceptionWhenFieldValueIsNotSet() {
@Test
void returnFieldValue() {
// given
RequiredField<String> nameField = RequiredField.notNullableOfNameWithinType("name", "Person");
ValidatableField<String> nameField = ValidatableField.ofNoneNullableField("name", "Person");
nameField.set("John");

// when
Expand All @@ -41,7 +41,7 @@ void returnFieldValue() {
@Test
void throwExceptionWhenNotNullableRequiredFieldSetToNull() {
// given
RequiredField<String> nameField = RequiredField.notNullableOfNameWithinType("name", "Person");
ValidatableField<String> nameField = ValidatableField.ofNoneNullableField("name", "Person");
nameField.set(null);

// when
Expand All @@ -55,12 +55,12 @@ void throwExceptionWhenNotNullableRequiredFieldSetToNull() {
}

@Nested
class NullableRequiredFieldTest {
class NullableValidatableFieldTest {

@Test
void throwExceptionWhenFieldValueIsNotSet() {
// given
RequiredField<String> nameField = RequiredField.nullableOfNameWithinType("name", "Person");
ValidatableField<String> nameField = ValidatableField.ofNullableField("name", "Person");

// when
ThrowingCallable whenOrElseThrowIsCalled = nameField::orElseThrow;
Expand All @@ -74,7 +74,7 @@ void throwExceptionWhenFieldValueIsNotSet() {
@Test
void returnFieldValue() {
// given
RequiredField<String> nameField = RequiredField.nullableOfNameWithinType("name", "Person");
ValidatableField<String> nameField = ValidatableField.ofNullableField("name", "Person");
nameField.set("John");

// when
Expand All @@ -88,7 +88,7 @@ void returnFieldValue() {
@Test
void returnFieldValueWhenSetToNull() {
// given
RequiredField<String> nameField = RequiredField.nullableOfNameWithinType("name", "Person");
ValidatableField<String> nameField = ValidatableField.ofNullableField("name", "Person");
nameField.set(null);

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ private List<FieldSpec> 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> make = RequiredField.notNullableOfNameWithinType("make", "GenerateStepBuilderWithSingleMandatoryAnnotatedField");
private final ValidatableField<String> make = ValidatableField.ofNoneNullableField("make", "GenerateStepBuilderWithSingleMandatoryAnnotatedField");

private double engineSize;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> make = RequiredField.nullableOfNameWithinType("make", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls");
private final ValidatableField<String> make = ValidatableField.ofNullableField("make", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls");

private final RequiredField<Integer> year = RequiredField.nullableOfNameWithinType("year", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls");
private final ValidatableField<Integer> year = ValidatableField.ofNullableField("year", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls");

private double engineSize;

private final RequiredField<Boolean> isElectric = RequiredField.nullableOfNameWithinType("isElectric", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls");
private final ValidatableField<Boolean> isElectric = ValidatableField.ofNullableField("isElectric", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls");

private final RequiredField<Float> fuelEfficiency = RequiredField.nullableOfNameWithinType("fuelEfficiency", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls");
private final ValidatableField<Float> fuelEfficiency = ValidatableField.ofNullableField("fuelEfficiency", "AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNulls");

public AnnotatedMandatoryFieldsCanBeSetToNullWithAllowNullsBuilder() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> make = RequiredField.nullableOfNameWithinType("make", "ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulls");
private final ValidatableField<String> make = ValidatableField.ofNullableField("make", "ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulls");

private final RequiredField<Integer> year = RequiredField.nullableOfNameWithinType("year", "ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulls");
private final ValidatableField<Integer> year = ValidatableField.ofNullableField("year", "ConstructorMandatoryFieldsCanBeSetToNullWithAllowNulls");

private double engineSize;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> make = RequiredField.nullableOfNameWithinType("make", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull");
private final ValidatableField<String> make = ValidatableField.ofNullableField("make", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull");

private final RequiredField<Integer> year = RequiredField.nullableOfNameWithinType("year", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull");
private final ValidatableField<Integer> year = ValidatableField.ofNullableField("year", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull");

private double engineSize;

private final RequiredField<Boolean> isElectric = RequiredField.nullableOfNameWithinType("isElectric", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull");
private final ValidatableField<Boolean> isElectric = ValidatableField.ofNullableField("isElectric", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull");

private final RequiredField<Float> fuelEfficiency = RequiredField.nullableOfNameWithinType("fuelEfficiency", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull");
private final ValidatableField<Float> fuelEfficiency = ValidatableField.ofNullableField("fuelEfficiency", "FieldsDeclaredInBuildableAnnotationCanBeSetToNull");

public FieldsDeclaredInBuildableAnnotationCanBeSetToNullBuilder() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> make = RequiredField.notNullableOfNameWithinType("make", "ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet");
private final ValidatableField<String> make = ValidatableField.ofNoneNullableField("make", "ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet");

private final RequiredField<Integer> year = RequiredField.notNullableOfNameWithinType("year", "ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet");
private final ValidatableField<Integer> year = ValidatableField.ofNoneNullableField("year", "ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet");

private final RequiredField<Double> engineSize = RequiredField.notNullableOfNameWithinType("engineSize", "ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet");
private final ValidatableField<Double> engineSize = ValidatableField.ofNoneNullableField("engineSize", "ThrowExceptionWhenMandatoryAnnotatedFieldsAreNotSet");

private boolean isElectric;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> make = RequiredField.notNullableOfNameWithinType("make", "ThrowExceptionWhenMandatoryConstructorFieldsAreNotSet");
private final ValidatableField<String> make = ValidatableField.ofNoneNullableField("make", "ThrowExceptionWhenMandatoryConstructorFieldsAreNotSet");

private final RequiredField<Integer> year = RequiredField.notNullableOfNameWithinType("year", "ThrowExceptionWhenMandatoryConstructorFieldsAreNotSet");
private final ValidatableField<Integer> year = ValidatableField.ofNoneNullableField("year", "ThrowExceptionWhenMandatoryConstructorFieldsAreNotSet");

private double engineSize;

Expand Down

0 comments on commit 231e777

Please sign in to comment.