Hi,
I'm nullmarking one of my libraries, and it has the following method:
/**
* Gets the value to which the given key is mapped, if existing. <br>
* <br>
* Unlike {@link Map#get(Object)}, with this method the compiler verifies the type of the key argument. Also, as it
* returns an {@link Optional}, calling code can use chained method calls instead of separate {@code if} blocks.
*
* @return a present {@link Optional} if the given map contains a non-{@code null} value for the given {@code key}.
*/
public <K, V> Optional<V> tryGet(Map<K, V> map, K key)
{
return Optional.ofNullable(map.get(key));
}
This is the annotated signature I came up with:
public <K extends @Nullable Object, V extends @Nullable Object> Optional<V> tryGet(Map<K, V> map, K key)
Deciding on the annotations for the type parameters and the method arguments felt fairly straightforward. But I'm less sure about the return type of tryGet(): As Optional.get() etc will in fact never return a null value, my gut feeling would be to declare the return type as Optional<@NonNull V>. However, this is probably redundant (or wrong?) as the JDK nullness annotation overlay(?) by JSpecify might already add @NonNull to relevant methods of the Optional class.
So, my question is: what's the correct declaration here?
Also, did I miss something or is this case not mentioned in the user guide? (Not saying it should; you guys are the experts here.)
Kind regards,
Jens
P.S.: Congratulations to the 1.0.0 release, and a big thank you for your hard work leading to it!