Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8247605: Avoid array allocation when concatenating with empty string
Reviewed-by: redestad, plevart
  • Loading branch information
amaembo committed Aug 10, 2020
1 parent bdac412 commit 6a66ac34e9c82914af5b2695dac495388bedc26f
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 241 deletions.
@@ -1790,6 +1790,42 @@ public final V put(K key, V value) {
return m.put(key, value);
}

public V putIfAbsent(K key, V value) {
if (!inRange(key))
throw new IllegalArgumentException("key out of range");
return m.putIfAbsent(key, value);
}

public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
if (!inRange(key))
throw new IllegalArgumentException("key out of range");
return m.merge(key, value, remappingFunction);
}

public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
if (!inRange(key)) {
// Do not throw if mapping function returns null
// to preserve compatibility with default computeIfAbsent implementation
if (mappingFunction.apply(key) == null) return null;
throw new IllegalArgumentException("key out of range");
}
return m.computeIfAbsent(key, mappingFunction);
}

public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
if (!inRange(key)) {
// Do not throw if remapping function returns null
// to preserve compatibility with default computeIfAbsent implementation
if (remappingFunction.apply(key, null) == null) return null;
throw new IllegalArgumentException("key out of range");
}
return m.compute(key, remappingFunction);
}

public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
return !inRange(key) ? null : m.computeIfPresent(key, remappingFunction);
}

public final V get(Object key) {
return !inRange(key) ? null : m.get(key);
}
@@ -523,7 +523,8 @@ public Iterator<Object[]> nullValueFriendlyMaps() {
new Object[]{"HashMap", (Supplier<Map<?, ?>>) HashMap::new},
new Object[]{"LinkedHashMap", (Supplier<Map<?, ?>>) LinkedHashMap::new},
new Object[]{"TreeMap", (Supplier<Map<?, ?>>) TreeMap::new},
new Object[]{"TreeMap(cmp)", (Supplier<Map<?, ?>>) () -> new TreeMap<>(Comparator.reverseOrder())}
new Object[]{"TreeMap(cmp)", (Supplier<Map<?, ?>>) () -> new TreeMap<>(Comparator.reverseOrder())},
new Object[]{"TreeMap.descendingMap", (Supplier<Map<?, ?>>) () -> new TreeMap<>().descendingMap()}
).iterator();
}
}
@@ -193,6 +193,8 @@ private static <T> Collection<Object[]> makeMapsMoreTypes(String desc,
new IdentityHashMap<>(), keys, val));
cases.add(createCase("TreeMap with " + desc,
new TreeMap<>(), keys, val));
cases.add(createCase("Descending TreeMap with " + desc,
new TreeMap<>().descendingMap(), keys, val));
cases.add(createCase("WeakHashMap with " + desc,
new WeakHashMap<>(), keys, val));
cases.add(createCase("ConcurrentHashMap with " + desc,

0 comments on commit 6a66ac3

Please sign in to comment.