return new PatchTree(result);
}
private static DataMap diffMaps(DataMap original, DataMap modified)
{
DataMap result = null;
for (String key : original.keySet())
{
if (! modified.containsKey(key))
{
result = result==null ? new DataMap() : result;
PatchOpFactory.REMOVE_FIELD_OP.store(result, key);
}
else
{
Object oValue = original.get(key);
Object mValue = modified.get(key);
if ((mValue.getClass() == DataMap.class) && (oValue.getClass() == DataMap.class))
{
DataMap subDiff = diffMaps((DataMap)oValue, (DataMap)mValue);
if (subDiff != null)
{
result = result==null ? new DataMap() : result;
result.put(Escaper.escapePathSegment(key), subDiff);
}
}
else if (! oValue.equals(mValue))
{
result = result==null ? new DataMap() : result;
PatchOpFactory.setFieldOp(mValue).store(result, key);
}
}
}
for (Map.Entry<String, Object> entry : modified.entrySet())
{
if (! original.containsKey(entry.getKey()))
{
result = result==null ? new DataMap() : result;
PatchOpFactory.setFieldOp(entry.getValue()).store(result, entry.getKey());
}
}
return result;
}