}
@SuppressWarnings("unchecked")
@Override
public void merge(Map<K, V> prev, Map<K, V> curr, ReflectiveConfig config, ConfigWriter diffwriter, Field field) throws ConfigurationException {
ConfigField fieldAnno = field.getAnnotation(ConfigField.class);
ConfigTypeAdapter<V, Object> valueAdapter = (ConfigTypeAdapter<V, Object>) getValueAdapter(field, config);
ConfigTypeAdapter<K, String> keyAdapter = (ConfigTypeAdapter<K, String>) getKeyAdapter(field, config);
ConfigWriter collectionWriter = diffwriter.getChildWriter(getCollectionName(fieldAnno), field);
// remove nodes that were deleted since prev
for (Entry<K, V> e : prev.entrySet())
if (curr.get(e.getKey()) == null)
collectionWriter.removeCollectionElement(fieldAnno.mapKey(), keyAdapter.serialize(e.getKey(), config, null));
// add new nodes and merge existing
for (Entry<K, V> e : curr.entrySet()) {
// serialize key
String serializedKey = keyAdapter.serialize(e.getKey(), config, null);
// if new node
if (prev.get(e.getKey()) == null) {
ConfigWriter elementWriter = collectionWriter.getCollectionElementWriter(fieldAnno.mapKey(), serializedKey, field);
// serialize
Object serialized = valueAdapter.serialize(e.getValue(), config, field);
valueAdapter.write(serialized, config, elementWriter, field);
elementWriter.flushWriter();
}
// existing node
else {
ConfigWriter elementWriter = collectionWriter.getCollectionElementDiffWriter(fieldAnno.mapKey(), serializedKey);
valueAdapter.merge(prev.get(e.getKey()), e.getValue(), config, elementWriter, field);
elementWriter.flushDiffs();
}
}
}