boolean valuesWithoutIdentity = SCOUtils.mapHasValuesWithoutIdentity(fmd);
com.google.common.collect.Multimap<K, V> attachedKeysValues = HashMultimap.create();
Collection<Map.Entry<K, V>> detachedEntries = m.entries();
Iterator<Map.Entry<K, V>> iter = detachedEntries.iterator();
ApiAdapter api = ownerSM.getExecutionContext().getApiAdapter();
while (iter.hasNext())
{
Map.Entry<K, V> entry = iter.next();
K key = entry.getKey();
V val = entry.getValue();
if (api.isPersistable(key) && api.isDetachable(key))
{
key = (K) ownerSM.getExecutionContext().attachObjectCopy(ownerSM, key, keysWithoutIdentity);
}
if (api.isPersistable(val) && api.isDetachable(val))
{
val = (V) ownerSM.getExecutionContext().attachObjectCopy(ownerSM, val, valuesWithoutIdentity);
}
attachedKeysValues.put(key, val);
}
// Update the attached map with the detached entries
com.google.common.collect.Multimap<K, V> copy = HashMultimap.create();
copy.putAll(this);
// Delete any keys that are no longer in the Map
Iterator<Map.Entry<K, V>> attachedIter = copy.entries().iterator();
while (attachedIter.hasNext())
{
Map.Entry<K, V> entry = attachedIter.next();
K key = entry.getKey();
if (!attachedKeysValues.containsKey(key))
{
this.removeAll(key);
}
}
// Add any new keys/values and update any changed values
Iterator<Map.Entry<K, V>> keysIter = attachedKeysValues.entries().iterator();
while (keysIter.hasNext())
{
Map.Entry<K, V> entry = keysIter.next();
K theKey = entry.getKey();
V theValue = entry.getValue();
if (!this.containsKey(theKey))
{
// Not present so add it
this.put(theKey, theValue);
}
else
{
// Update any values
Object oldValue = this.get(theKey);
if (api.isPersistable(theValue) && api.getIdForObject(theValue) != api.getIdForObject(oldValue))
{
// In case they have changed the PC for this key (different id)
this.put(theKey, theValue);
}
else