Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KiwiMaps methods to get a non-null value or throw an exception #1094

Closed
sleberknight opened this issue Jan 7, 2024 · 1 comment · Fixed by #1097
Closed

Add KiwiMaps methods to get a non-null value or throw an exception #1094

sleberknight opened this issue Jan 7, 2024 · 1 comment · Fixed by #1097
Assignees
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Milestone

Comments

@sleberknight
Copy link
Member

sleberknight commented Jan 7, 2024

Map has get which returns "the value to which the specified key is mapped, or null if this map contains no mapping for the key", and getOrDefault 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:

public static <K, V> V getOrThrow(Map<K, V> map, K key)

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 or IllegalStateException, there should be the more generic method:

public static <K, V> V getOrThrow(Map<K, V> map, K key, Supplier<RuntimeException> exceptionSupplier)

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:

public static <K, V, E extends Exception> V getOrThrowChecked(Map<K, V> map,
                                                              K key,
                                                              Supplier<E> exceptionSupplier) throws E {

This last method would be the exception, not the rule, i.e., we expect the normal case to be that a RuntimeException is preferred.

@sleberknight sleberknight added the new feature A new feature such as a new class, method, package, group of classes, etc. label Jan 7, 2024
@sleberknight sleberknight added this to the 3.2.1 milestone Jan 7, 2024
@sleberknight sleberknight self-assigned this Jan 7, 2024
@sleberknight
Copy link
Member Author

sleberknight commented Jan 9, 2024

These methods should throw IlegalArgumentException if any of the arguments are null

sleberknight added a commit that referenced this issue Jan 13, 2024
…1097)

* Add getOrThrow that accepts a map and key
* Add getOrThrow that accepts a map, key, and RuntimeException supplier
* Add getOrThrowChecked that accepts a map, key, and Exception supplier

Closes #1094
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant