forked from jonas-grgt/bob
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow null values for concstructor enforced builders
- Loading branch information
1 parent
29350c0
commit bc9240f
Showing
14 changed files
with
351 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
annotations/src/main/java/io/jonasg/bob/NotNullableRequiredField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.jonasg.bob; | ||
|
||
/** | ||
* Container Object for a required field and its value that cannot be set as | ||
* null | ||
* | ||
* @param <T> | ||
* the type of the required field its value | ||
*/ | ||
@SuppressWarnings("unused") | ||
public final class NotNullableRequiredField<T> implements RequiredField<T> { | ||
|
||
private T fieldValue; | ||
|
||
private final String fieldName; | ||
|
||
private final String typeName; | ||
|
||
NotNullableRequiredField(T fieldValue, String fieldName, String typeName) { | ||
this.fieldValue = fieldValue; | ||
this.fieldName = fieldName; | ||
this.typeName = typeName; | ||
} | ||
|
||
@Override | ||
public void set(T value) { | ||
this.fieldValue = value; | ||
} | ||
|
||
@Override | ||
public T orElseThrow() { | ||
if (fieldValue == null) { | ||
throw new MandatoryFieldMissingException(fieldName, typeName); | ||
} | ||
return fieldValue; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
annotations/src/main/java/io/jonasg/bob/NullableRequiredField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.jonasg.bob; | ||
|
||
/** | ||
* Container Object for a required field and its value that can be set as null | ||
* | ||
* @param <T> | ||
* the type of the required field its value | ||
*/ | ||
@SuppressWarnings("unused") | ||
public class NullableRequiredField<T> implements RequiredField<T> { | ||
|
||
private T fieldValue; | ||
|
||
private boolean fieldSet; | ||
|
||
private final String fieldName; | ||
|
||
private final String typeName; | ||
|
||
public NullableRequiredField(T fieldValue, String fieldName, String typeName) { | ||
this.fieldValue = fieldValue; | ||
this.fieldName = fieldName; | ||
this.typeName = typeName; | ||
} | ||
|
||
@Override | ||
public void set(T value) { | ||
this.fieldValue = value; | ||
this.fieldSet = true; | ||
} | ||
|
||
@Override | ||
public T orElseThrow() { | ||
if (!this.fieldSet && this.fieldValue == null) { | ||
throw new MandatoryFieldMissingException(this.fieldName, this.typeName); | ||
} | ||
return this.fieldValue; | ||
} | ||
} |
37 changes: 8 additions & 29 deletions
37
annotations/src/main/java/io/jonasg/bob/RequiredField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,17 @@ | ||
package io.jonasg.bob; | ||
|
||
/** | ||
* Container Object for a required field and its value | ||
* | ||
* @param <T> | ||
* the type of the required field its value | ||
*/ | ||
@SuppressWarnings("unused") | ||
public final class RequiredField<T> { | ||
public interface RequiredField<T> { | ||
|
||
private T fieldValue; | ||
|
||
private final String fieldName; | ||
|
||
private final String typeName; | ||
|
||
private RequiredField(T fieldValue, String fieldName, String typeName) { | ||
this.fieldValue = fieldValue; | ||
this.fieldName = fieldName; | ||
this.typeName = typeName; | ||
static <T> RequiredField<T> notNullableOfNameWithinType(String fieldName, String typeName) { | ||
return new NotNullableRequiredField<>(null, fieldName, typeName); | ||
} | ||
|
||
public static <T> RequiredField<T> ofNameWithinType(String fieldName, String typeName) { | ||
return new RequiredField<>(null, fieldName, typeName); | ||
static <T> RequiredField<T> nullableOfNameWithinType(String fieldName, String typeName) { | ||
return new NullableRequiredField<>(null, fieldName, typeName); | ||
} | ||
|
||
public void set(T value) { | ||
this.fieldValue = value; | ||
} | ||
void set(T value); | ||
|
||
T orElseThrow(); | ||
|
||
public T orElseThrow() { | ||
if (fieldValue == null) { | ||
throw new MandatoryFieldMissingException(fieldName, typeName); | ||
} | ||
return fieldValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.