Hope and doubt is a tiny library aimed at simplifying validation of arguments to methods, especially constructors and setters/getters. It has no external dependencies and is under 9 KB at the time of this writing. It provides both optimistic and pessimistic validators to suit your needs. The differences are:
- Optimistic validators assume that the input is valid and throw unchecked exceptions if the input is invalid. Optimistic validators are created via
Hope.that()
- Pessimistic validators assume that the input is invalid and throw checked exceptions if this turns out to be the case. Pessimistic validators are created via
Doubt.that()
<dependency>
<groupId>com.martiansoftware</groupId>
<artifactId>hope-and-doubt</artifactId>
<version>0.1.0</version>
</dependency>
public void setName(String name) {
String validatedName = Hope.that(name).isNotNullOrEmpty().value();
...
}
If name
is null, this will fail with:
Exception in thread "main" com.martiansoftware.validation.UncheckedValidationException: value must not be null
If name
is an empty String, it will fail with
Exception in thread "main" com.martiansoftware.validation.UncheckedValidationException: value must not be empty
public void setName(String name) throws CheckedValidationException {
String validatedName = Doubt.that(name).isNotNullOrEmpty().value();
...
}
If name
is null, this will fail with:
Exception in thread "main" com.martiansoftware.validation.CheckedValidationException: value must not be null
If name
is an empty String, it will fail with
Exception in thread "main" com.martiansoftware.validation.CheckedValidationException: value must not be empty
public void setName(String name) {
String validatedName = Hope.that(name).named("a person's name").isNotNullOrEmpty().value();
...
}
If name
is null, this will fail with:
Exception in thread "main" com.martiansoftware.validation.UncheckedValidationException: a person's name must not be null
public void setName(String name) {
String validatedName = Hope.that(name).orElse("Anonymous").isNotNullOrEmpty().value();
...
}
public void setFoo(List<Foo> fooList, Bar[] barArray) {
List<Foo> myFoos = Hope.that(fooList).isNotNullOrEmpty().value();
Bar[] myBars = Hope.that(barArray).isNotNullOrEmpty().value();
...
}
public void setName(String name) {
String validatedName = Hope.that(name)
.isNotNullOrEmpty()
.isFalse(n -> n.equalsIgnoreCase("anonymous"))
.value();
...
}
public void setAge(Integer age) {
String validatedAge = Hope.that(name)
.isNotNullOrEmpty()
.isFalse(n -> n < 0,
"negative ages are not allowed")
.value();
...
}
public void setName(String name) {
String validatedName = Hope.that(name)
.orElse("Mrs. Jane Doe")
.isNotNullOrEmpty()
.matchesAny("^Mrs?\\. .*", // accepts "Mr. or "Mrs."
"^Dr\\. .*") // accepts "Dr."
.value();
...
}
public void setBaz(String baz) {
int i = Hope.that(baz).isNotNullOrEmpty().map(Integer::valueOf).value();
...
}