@Override public Option<V> get(K key) { return map.get(key); }
@Override public LinkedHashMap<K, V> replace(Tuple2<K, V> currentElement, Tuple2<K, V> newElement) { Objects.requireNonNull(currentElement, "currentElement is null"); Objects.requireNonNull(newElement, "newElement is null"); // We replace the whole element, i.e. key and value have to be present. if (!Objects.equals(currentElement, newElement) && contains(currentElement)) { Queue<Tuple2<K, V>> newList = list; HashMap<K, V> newMap = map; final K currentKey = currentElement._1; final K newKey = newElement._1; // If current key and new key are equal, the element will be automatically replaced, // otherwise we need to remove the pair (newKey, ?) from the list manually. if (!Objects.equals(currentKey, newKey)) { final Option<V> value = newMap.get(newKey); if (value.isDefined()) { newList = newList.remove(Tuple.of(newKey, value.get())); } } newList = newList.replace(currentElement, newElement); newMap = newMap.remove(currentKey).put(newElement); return wrap(newList, newMap); } else { return this; } }
@Override public Option<V> get(K key) { return map.get(key); }
@Override public LinkedHashMap<K, V> replace(Tuple2<K, V> currentElement, Tuple2<K, V> newElement) { Objects.requireNonNull(currentElement, "currentElement is null"); Objects.requireNonNull(newElement, "newElement is null"); // We replace the whole element, i.e. key and value have to be present. if (!Objects.equals(currentElement, newElement) && contains(currentElement)) { Queue<Tuple2<K, V>> newList = list; HashMap<K, V> newMap = map; final K currentKey = currentElement._1; final K newKey = newElement._1; // If current key and new key are equal, the element will be automatically replaced, // otherwise we need to remove the pair (newKey, ?) from the list manually. if (!Objects.equals(currentKey, newKey)) { final Option<V> value = newMap.get(newKey); if (value.isDefined()) { newList = newList.remove(Tuple.of(newKey, value.get())); } } newList = newList.replace(currentElement, newElement); newMap = newMap.remove(currentKey).put(newElement); return wrap(newList, newMap); } else { return this; } }
@Test public void testHashMap() throws Exception { HashMap<String, A> src = HashMap.of("a", new B("a", "b")); String json = MAPPER.writeValueAsString(new HashMapPojo().setValue(src)); Assert.assertEquals(json, "{\"value\":{\"a\":{\"ExtFieldsPojoTest$B\":{\"a\":\"a\",\"b\":\"b\"}}}}"); HashMapPojo pojo = MAPPER.readValue(json, HashMapPojo.class); HashMap<String, A> restored = pojo.getValue(); Assert.assertTrue(restored.get("a").get() instanceof B); Assert.assertEquals(restored.get("a").get().a, "a"); Assert.assertEquals(((B) restored.get("a").get()).b, "b"); }
@Test public void testHashMap() throws Exception { HashMap<String, I> src = HashMap.of("a", new A(), "b", new B()); String json = MAPPER.writeValueAsString(new HashMapPojo().setValue(src)); Assert.assertEquals(json, "{\"value\":{\"a\":{\"type\":\"a\"},\"b\":{\"type\":\"b\"}}}"); HashMapPojo pojo = MAPPER.readValue(json, HashMapPojo.class); HashMap<String, I> restored = pojo.getValue(); Assert.assertTrue(restored.get("a").get() instanceof A); Assert.assertTrue(restored.get("b").get() instanceof B); }