Philosophy
A semantic emptiness is a key concept used to indicate the absence of a value. Even if an object is not actually referenced as null, it can be considered null if it is semantically filled with empty values.
In the beginning there was...
You had to convert an empty value to null to persist it in a database. Also you had to convert null to an empty string to display it in a GUI or something elsewhere like Stream or Writer. If you don't want to use null at all, you had to write a code to getting default value/object. like this:
if (value != null && value.length() <= 0) {
entity.setValue(null);
} else {
entity.setValue(value);
}
or
entity.setValue(value == null || value.length() <= 0 ? null : value);
Okay, That's totally fine. But how about this?
entity.setValue(Nullify.of(value));
in some cases, you might want to use empty string("") instead of null:
Nullify.toString(value);
Further, checking for semantically empty
if (collection == null || collection.size() <= 0) {
collection = Arrays.asList("default_value");
} else {
boolean isSemanticallyEmpty = true;
for (String value : collection) {
if (value != null && value.length() > 0) {
isSemanticallyEmpty = false;
break;
}
}
if (isSemanticallyEmpty) {
collection = Arrays.asList("default_value");
}
}
above code can also be replaced with:
Nullify.of(collection, Arrays.asList("default_value"));
Conclusion
If you're tired of writing code that checks whether a value is semantically empty or not, you can use Nullify.
silentsoft
/
nullify
Nullify for null representations and assertions of objects.
Nullify
Nullify for null representations and assertions of objects.
Philosophy
A semantic emptiness is a key concept used to indicate the absence of a value
Even if an object is not actually referenced as null, it can be considered null if it is semantically filled with empty values.
In the beginning there was...
You had to convert an empty value to null to persist it in a database
Also you had to convert null to an empty string to display it in a GUI or something elsewhere like Stream or Writer
If you don't want to use null at all, you had to write a code to getting default value/object. like this:
if (value != null && value.length() <= 0) {
entity.setValue(null);
} else {
entity.setValue(value);
}
or
entity.setValue(value == null || value.length() <= 0 ?…
Top comments (2)
Nice little utility. Not sure if I'll use it, but nice none-the-less. The very long if-else-if chain of instanceof checks would likely lead me to prefer doing the relevant check directly in most cases. Anyway, I submitted a couple issues on GitHub for improvements to documentation that I think would be helpful for potential users of nullify.
Thank you :)