private void typeCheck(Object key, Object value) { if (key != null && !keyType.isInstance(key)) throw new ClassCastException(badKeyMsg(key)); if (value != null && !valueType.isInstance(value)) throw new ClassCastException(badValueMsg(value)); }
@SuppressWarnings("unchecked") public void putAll(Map<? extends K, ? extends V> t) { // Satisfy the following goals: // - good diagnostics in case of type mismatch // - all-or-nothing semantics // - protection from malicious t // - correct behavior if t is a concurrent map Object[] entries = t.entrySet().toArray(); List<Map.Entry<K,V>> checked = new ArrayList<>(entries.length); for (Object o : entries) { Map.Entry<?,?> e = (Map.Entry<?,?>) o; Object k = e.getKey(); Object v = e.getValue(); typeCheck(k, v); checked.add( new AbstractMap.SimpleImmutableEntry<>((K) k, (V) v)); } for (Map.Entry<K,V> e : checked) m.put(e.getKey(), e.getValue()); }
Class<K> keyType, Class<V> valueType) { return new CheckedMap<>(m, keyType, valueType);
Class<K> keyType, Class<V> valueType) { return new CheckedMap<>(m, keyType, valueType);
@SuppressWarnings("unchecked") public void putAll(Map<? extends K, ? extends V> t) { // Satisfy the following goals: // - good diagnostics in case of type mismatch // - all-or-nothing semantics // - protection from malicious t // - correct behavior if t is a concurrent map Object[] entries = t.entrySet().toArray(); List<Map.Entry<K,V>> checked = new ArrayList<>(entries.length); for (Object o : entries) { Map.Entry<?,?> e = (Map.Entry<?,?>) o; Object k = e.getKey(); Object v = e.getValue(); typeCheck(k, v); checked.add( new AbstractMap.SimpleImmutableEntry<>((K) k, (V) v)); } for (Map.Entry<K,V> e : checked) m.put(e.getKey(), e.getValue()); }
/** * Returns a dynamically typesafe view of the specified map. Trying to * insert an element of the wrong type into this map throws a * {@code ClassCastException}. At creation time the types in {@code m} are * not checked for correct type. * * @param m * the map to be wrapped in a typesafe map. * @param keyType * the type of the keys permitted to insert. * @param valueType * the type of the values permitted to insert. * @return a typesafe map. */ public static <K, V> Map<K, V> checkedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType) { return new CheckedMap<K, V>(m, keyType, valueType); }
/** * Returns a dynamically typesafe view of the specified map. Trying to * insert an element of the wrong type into this map throws a * {@code ClassCastException}. At creation time the types in {@code m} are * not checked for correct type. * * @param m * the map to be wrapped in a typesafe map. * @param keyType * the type of the keys permitted to insert. * @param valueType * the type of the values permitted to insert. * @return a typesafe map. */ public static <K, V> Map<K, V> checkedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType) { return new CheckedMap<K, V>(m, keyType, valueType); }
/** * Returns a dynamically typesafe view of the specified map. Trying to * insert an element of the wrong type into this map throws a * {@code ClassCastException}. At creation time the types in {@code m} are * not checked for correct type. * * @param m * the map to be wrapped in a typesafe map. * @param keyType * the type of the keys permitted to insert. * @param valueType * the type of the values permitted to insert. * @return a typesafe map. */ public static <K, V> Map<K, V> checkedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType) { return new CheckedMap<K, V>(m, keyType, valueType); }
/** * Returns a dynamically typesafe view of the specified map. Trying to * insert an element of the wrong type into this map throws a * {@code ClassCastException}. At creation time the types in {@code m} are * not checked for correct type. * * @param m * the map to be wrapped in a typesafe map. * @param keyType * the type of the keys permitted to insert. * @param valueType * the type of the values permitted to insert. * @return a typesafe map. */ public static <K, V> Map<K, V> checkedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType) { return new CheckedMap<K, V>(m, keyType, valueType); }
/** * Returns a dynamically typesafe view of the specified map. Trying to * insert an element of the wrong type into this map throws a * {@code ClassCastException}. At creation time the types in {@code m} are * not checked for correct type. * * @param m * the map to be wrapped in a typesafe map. * @param keyType * the type of the keys permitted to insert. * @param valueType * the type of the values permitted to insert. * @return a typesafe map. */ public static <K, V> Map<K, V> checkedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType) { return new CheckedMap<K, V>(m, keyType, valueType); }
/** * Returns a dynamically typesafe view of the specified map. Trying to * insert an element of the wrong type into this map throws a * {@code ClassCastException}. At creation time the types in {@code m} are * not checked for correct type. * * @param m * the map to be wrapped in a typesafe map. * @param keyType * the type of the keys permitted to insert. * @param valueType * the type of the values permitted to insert. * @return a typesafe map. */ public static <K, V> Map<K, V> checkedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType) { return new CheckedMap<K, V>(m, keyType, valueType); }
/** * Returns a dynamically typesafe view of the specified map. Trying to * insert an element of the wrong type into this map throws a * {@code ClassCastException}. At creation time the types in {@code m} are * not checked for correct type. * * @param m * the map to be wrapped in a typesafe map. * @param keyType * the type of the keys permitted to insert. * @param valueType * the type of the values permitted to insert. * @return a typesafe map. */ public static <K, V> Map<K, V> checkedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType) { return new CheckedMap<K, V>(m, keyType, valueType); }
public V put(K key, V value) { typeCheck(key, value); return m.put(key, value); }
public V put(K key, V value) { typeCheck(key, value); return m.put(key, value); }
private void typeCheck(Object key, Object value) { if (key != null && !keyType.isInstance(key)) throw new ClassCastException(badKeyMsg(key)); if (value != null && !valueType.isInstance(value)) throw new ClassCastException(badValueMsg(value)); }
/** * Returns a dynamically typesafe view of the specified map. Trying to * insert an element of the wrong type into this map throws a * {@code ClassCastException}. At creation time the types in {@code m} are * not checked for correct type. * * @param m * the map to be wrapped in a typesafe map. * @param keyType * the type of the keys permitted to insert. * @param valueType * the type of the values permitted to insert. * @return a typesafe map. */ public static <K, V> Map<K, V> checkedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType) { return new CheckedMap<K, V>(m, keyType, valueType); }