Interface Completable<T>
-
public interface Completable<T>Extension interface that allows runtime checking whether all fields of a bean have been initialized, that is, are not null.
The generic typeTshould always be set to the bean type. The type is used inassertComplete()and is done to be able to putassertComplete()at the end of an initialization chain. See example below.If used together with a bean style with
Optionalsupport (ClassicBeanStyleWithOptionalSupportandModernBeanStyle) some bean fields can be set to not mandatory and will be left out of the not-null check.
Here you find an example that also uses modern bean style (seeModernBeanStyle):public interface Person extends Completable<Person> { String firstName(); Person firstName(String s); String lastName(); Person lastName(String s); Optional<LocalDate> dateOfBirth(); Person dateOfBirth(LocalDate d); }assertComplete()should now always be called after the initialization of aPersonbean is completed.dateOfBirthis set as optional so it will not be considered when checking for completeness:// will throw no exception as firstName and lastName are set Person person1 = IBean.of(Person.class).firstName("John").lastName("Doe").assertComplete(); // will throw exception as firstName was not set Person person2 = IBean.of(Person.class).lastName("Doe").assertComplete(); // will throw exception as lastName is null Person person3 = IBean.of(Person.class).firstName("John").lastName(null).assertComplete(); // will throw no exception; setting dateOfBirth here is optional Person person4 = IBean.of(Person.class) .firstName("John") .dateOfBirth(LocalDate.of(11, 11, 1977)) .lastName("Doe").assertComplete();Attention:
Completablechecks all (mandatory) fields from being not null. Contrary toModificationAwareit does not check if any setter was called. And for declaring a field as non-mandatory it is not enough to just returnOptionalfrom the getter, you also have to use a bean style with optional support likeClassicBeanStyleWithOptionalSupportandModernBeanStyle.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description TassertComplete()Checks if all (non-optional) bean fields are notnull.booleanisComplete()
-
-
-
Method Detail
-
isComplete
boolean isComplete()
- Returns:
trueif all (non-optional) bean fields are notnull- See Also:
Completable
-
assertComplete
T assertComplete() throws BeanIncompleteException
Checks if all (non-optional) bean fields are notnull.- Returns:
- this bean if all fields are set and check was successful
- Throws:
BeanIncompleteException- if some of the (mandatory) fields arenull- See Also:
Completable
-
-