protected void initialize(Class type,
Object oldInstance,
Object newInstance,
Encoder out) {
// System.out.println("Initializing: " + newInstance);
java.util.Map oldMap = (java.util.Map)oldInstance;
java.util.Map newMap = (java.util.Map)newInstance;
// Remove the new elements.
// Do this first otherwise we undo the adding work.
if (newMap != null) {
for ( Object newKey : newMap.keySet() ) {
// PENDING: This "key" is not in the right environment.
if (!oldMap.containsKey(newKey)) {
invokeStatement(oldInstance, "remove", new Object[]{newKey}, out);
}
}
}
// Add the new elements.
for ( Object oldKey : oldMap.keySet() ) {
Expression oldGetExp = new Expression(oldInstance, "get", new Object[]{oldKey});
// Pending: should use newKey.
Expression newGetExp = new Expression(newInstance, "get", new Object[]{oldKey});
try {
Object oldValue = oldGetExp.getValue();
Object newValue = newGetExp.getValue();
out.writeExpression(oldGetExp);
if (!MetaData.equals(newValue, out.get(oldValue))) {
invokeStatement(oldInstance, "put", new Object[]{oldKey, oldValue}, out);
} else if ((newValue == null) && !newMap.containsKey(oldKey)) {
// put oldValue(=null?) if oldKey is absent in newMap
invokeStatement(oldInstance, "put", new Object[]{oldKey, oldValue}, out);
}
}
catch (Exception e) {
out.getExceptionListener().exceptionThrown(e);
}
}
}
|