public interface Completable<T>
T should always be set to the bean type. The
type is used in assertComplete() and is done to be able to put
assertComplete() at the end of an initialization chain. See example
below.
If used together with a bean style with Optional support (
ClassicBeanStyleWithOptionalSupport and ModernBeanStyle) 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 (see
ModernBeanStyle):
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 a Person bean is completed.
dateOfBirth is 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:Completable checks all (mandatory) fields
from being not null. Contrary to ModificationAware it does not check
if any setter was called. And for declaring a field as non-mandatory it is
not enough to just return Optional from the getter, you also have to
use a bean style with optional support like
ClassicBeanStyleWithOptionalSupport and ModernBeanStyle.
| Modifier and Type | Method and Description |
|---|---|
T |
assertComplete()
Checks if all (non-optional) bean fields are not
null. |
boolean |
isComplete() |
boolean isComplete()
true if all (non-optional) bean fields are not
nullCompletableT assertComplete() throws BeanIncompleteException
null.BeanIncompleteException - if some of the (mandatory) fields are nullCompletable