@Override public V putIfAbsent(K key, V value) { checkType(key); checkType(value); return copyIfRequired(map.putIfAbsent(key, value)); }
@Override public V get(Object key) { return copyIfRequired(map.get(key)); }
@Override public V remove(Object key) { return copyIfRequired(map.remove(key)); }
@Override public V replace(K key, V value) { checkType(key); checkType(value); return copyIfRequired(map.replace(key, value)); }
@Override public Set<K> keySet() { Set<K> keys = new HashSet<>(map.size()); for (K k : map.keySet()) { keys.add(copyIfRequired(k)); } return keys; }
@Override public V replace(K key, V value) { checkType(key); checkType(value); return copyIfRequired(map.replace(key, value)); }
/** * Composes the given bi-function ({@code f(a,b)}) with a function checking the type of the output: * {@code checkType(f(a,b))}. So the output of the given function is checked to verify that it uses a valid type. * * @param function the function * @return the composition */ private BiFunction<? super K, ? super V, ? extends V> typeChecked(BiFunction<? super K, ? super V, ? extends V> function) { return (k, v) -> { checkType(k); V output = function.apply(k, v); if (output != null) { checkType(output); } return output; }; }
@Override public Collection<V> values() { List<V> values = new ArrayList<>(map.size()); for (V v : map.values()) { values.add(copyIfRequired(v)); } return values; }
@Override public V putIfAbsent(K key, V value) { checkType(key); checkType(value); return copyIfRequired(map.putIfAbsent(key, value)); }
/** * Composes the given function ({@code f(a)}) with a function checking the type of the output. So the output of the * given function is checked to verify that is uses a valid type. * * @param function the function * @return the composition */ private Function<? super K, ? extends V> typeChecked(Function<? super K, ? extends V> function) { return k -> { checkType(k); V output = function.apply(k); if (output != null) { checkType(output); } return output; }; }
@Override public V getOrDefault(Object key, V defaultValue) { return copyIfRequired(map.getOrDefault(key, defaultValue)); }
@Override public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) { checkType(key); checkType(value); return map.merge(key, value, (k, v) -> { // No need to check the key, already check above. V output = remappingFunction.apply(k, v); if (output != null) { checkType(output); } return output; }); }
@Override public V remove(Object key) { return copyIfRequired(map.remove(key)); }
@Override public V get(Object key) { return copyIfRequired(map.get(key)); }
@Override public Set<K> keySet() { Set<K> keys = new HashSet<>(map.size()); for (K k : map.keySet()) { keys.add(copyIfRequired(k)); } return keys; }