Skip to content

Commit

Permalink
Minor improvements to Map.merge usage of @PolyNull and @NonNull. (
Browse files Browse the repository at this point in the history
  • Loading branch information
cpovirk authored Jun 11, 2023
1 parent c414784 commit 7e9c413
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/java.base/share/classes/java/util/Collections.java
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,7 @@ public V replace(K key, V value) {

@Override
public @PolyNull V merge(K key, @NonNull V value,
BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) {
BiFunction<? super @NonNull V, ? super @NonNull V, ? extends @PolyNull V> remappingFunction) {
throw new UnsupportedOperationException();
}

Expand Down Expand Up @@ -2839,7 +2839,7 @@ public V replace(K key, V value) {
}
@Override
public @PolyNull V merge(K key, @NonNull V value,
BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) {
BiFunction<? super @NonNull V, ? super @NonNull V, ? extends @PolyNull V> remappingFunction) {
synchronized (mutex) {return m.merge(key, value, remappingFunction);}
}

Expand Down Expand Up @@ -3923,7 +3923,7 @@ public V replace(K key, V value) {

@Override
public @PolyNull V merge(K key, @NonNull V value,
BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) {
BiFunction<? super @NonNull V, ? super @NonNull V, ? extends @PolyNull V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
return m.merge(key, value, (v1, v2) -> {
V newValue = remappingFunction.apply(v1, v2);
Expand Down Expand Up @@ -4924,7 +4924,7 @@ public V replace(K key, V value) {

@Override
public @PolyNull V merge(K key, @NonNull V value,
BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) {
BiFunction<? super @NonNull V, ? super @NonNull V, ? extends @PolyNull V> remappingFunction) {
throw new UnsupportedOperationException();
}

Expand Down Expand Up @@ -5270,7 +5270,7 @@ public V replace(K key, V value) {

@Override
public @PolyNull V merge(K key, @NonNull V value,
BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) {
BiFunction<? super @NonNull V, ? super @NonNull V, ? extends @PolyNull V> remappingFunction) {
throw new UnsupportedOperationException();
}

Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/util/HashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@ else if (v != null) {
*/
@Override
public @PolyNull V merge(K key, @NonNull V value,
BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) {
BiFunction<? super @NonNull V, ? super @NonNull V, ? extends @PolyNull V> remappingFunction) {
if (value == null || remappingFunction == null)
throw new NullPointerException();
int hash = hash(key);
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/util/Hashtable.java
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ public synchronized V replace(K key, V value) {
* remapping function modified this map
*/
@Override
public synchronized @PolyNull V merge(K key, @NonNull V value, BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) {
public synchronized @PolyNull V merge(K key, @NonNull V value, BiFunction<? super @NonNull V, ? super @NonNull V, ? extends @PolyNull V> remappingFunction) {
Objects.requireNonNull(remappingFunction);

Entry<?,?> tab[] = table;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ abstract static class AbstractImmutableMap<K,V> extends AbstractMap<K,V> impleme
@Override public @PolyNull V compute(K key, BiFunction<? super K,? super V,? extends @PolyNull V> rf) { throw uoe(); }
@Override public @PolyNull V computeIfAbsent(K key, Function<? super K,? extends @PolyNull V> mf) { throw uoe(); }
@Override public @PolyNull V computeIfPresent(K key, BiFunction<? super K,? super V,? extends @PolyNull V> rf) { throw uoe(); }
@Override public @PolyNull V merge(K key, @NonNull V value, BiFunction<? super V,? super V,? extends @PolyNull V> rf) { throw uoe(); }
@Override public @PolyNull V merge(K key, @NonNull V value, BiFunction<? super @NonNull V,? super @NonNull V,? extends @PolyNull V> rf) { throw uoe(); }
@Override public V put(K key, V value) { throw uoe(); }
@Override public void putAll(Map<? extends K,? extends V> m) { throw uoe(); }
@Override public V putIfAbsent(K key, V value) { throw uoe(); }
Expand Down
6 changes: 1 addition & 5 deletions src/java.base/share/classes/java/util/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -1349,12 +1349,8 @@ default boolean replace(K key, V oldValue, V newValue) {
* null
* @since 1.8
*/
@CFComment({
"It would be more flexible to make the return type of remappingFunction be `@Nullable V`. A",
"remappingFunction that returns null is is probably rare, and these annotations accommodate",
"the majority of uses that don't return null."})
default @PolyNull V merge(K key, @NonNull V value,
BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) {
BiFunction<? super @NonNull V, ? super @NonNull V, ? extends @PolyNull V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Objects.requireNonNull(value);
V oldValue = get(key);
Expand Down
4 changes: 3 additions & 1 deletion src/java.base/share/classes/java/util/TreeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import org.checkerframework.checker.nullness.qual.EnsuresKeyFor;
import org.checkerframework.checker.nullness.qual.EnsuresKeyForIf;
import org.checkerframework.checker.nullness.qual.KeyFor;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.checker.nullness.qual.PolyNull;
import org.checkerframework.checker.signedness.qual.UnknownSignedness;
import org.checkerframework.dataflow.qual.Pure;
import org.checkerframework.dataflow.qual.SideEffectFree;
Expand Down Expand Up @@ -723,7 +725,7 @@ else if (cmp > 0)
* remapping function modified this map
*/
@Override
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
public @PolyNull V merge(K key, @NonNull V value, BiFunction<? super @NonNull V, ? super @NonNull V, ? extends @PolyNull V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Objects.requireNonNull(value);
Entry<K,V> t = root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2052,7 +2052,7 @@ else if (f instanceof ReservationNode)
* @throws RuntimeException or Error if the remappingFunction does so,
* in which case the mapping is unchanged
*/
public @PolyNull V merge(K key, @NonNull V value, BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) {
public @PolyNull V merge(K key, @NonNull V value, BiFunction<? super @NonNull V, ? super @NonNull V, ? extends @PolyNull V> remappingFunction) {
if (key == null || value == null || remappingFunction == null)
throw new NullPointerException();
int h = spread(key.hashCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ else if ((oldValue = putIfAbsent(key, newValue)) == null)
*/
@Override
default @PolyNull V merge(K key, @NonNull V value,
BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) {
BiFunction<? super @NonNull V, ? super @NonNull V, ? extends @PolyNull V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Objects.requireNonNull(value);
retry: for (;;) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,7 @@ else if (doRemove(key, v) != null)
* @since 1.8
*/
public @PolyNull V merge(K key, @NonNull V value,
BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) {
BiFunction<? super @NonNull V, ? super @NonNull V, ? extends @PolyNull V> remappingFunction) {
if (key == null || value == null || remappingFunction == null)
throw new NullPointerException();
for (;;) {
Expand Down

0 comments on commit 7e9c413

Please sign in to comment.