Add KiwiMaps methods to get a non-null value or throw an exception #1094
Labels
new feature
A new feature such as a new class, method, package, group of classes, etc.
Milestone
Map
hasget
which returns "the value to which the specified key is mapped, or null if this map contains no mapping for the key", andgetOrDefault
which returns "the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key".But many times I want to get a non-null value from a map for a specific key. In other words, the map should have a mapping (i.e., there is an entry in the map with that key) which contains a non-null value. And if it doesn't , I want to treat it an exceptional condition and throw an exception.
So, we can add a method to
KiwiMaps
that has the following signature:It should throw a
NoSuchElementException
when the map doesn't contain the specified key, or if it has the key but the associated value is null.Since it might also be desirable to throw some other kind of exception, e.g., a
ValidationException
orIllegalStateException
, there should be the more generic method:Note this only permits runtime exceptions, so that we don't force a
try/catch
(or a Lombok@SneakyThrows
).To permit throwing checked exceptions, we can also add:
This last method would be the exception, not the rule, i.e., we expect the normal case to be that a
RuntimeException
is preferred.The text was updated successfully, but these errors were encountered: