* @param comparator
* the revision comparator.
*/
public static void applyChanges(@Nonnull Document doc, @Nonnull UpdateOp update, @Nonnull Comparator<Revision> comparator) {
for (Entry<Key, Operation> e : checkNotNull(update).getChanges().entrySet()) {
Key k = e.getKey();
Operation op = e.getValue();
switch (op.type) {
case SET: {
doc.put(k.toString(), op.value);
break;
}
case INCREMENT: {
Object old = doc.get(k.toString());
Long x = (Long) op.value;
if (old == null) {
old = 0L;
}
doc.put(k.toString(), ((Long) old) + x);
break;
}
case SET_MAP_ENTRY: {
Object old = doc.get(k.getName());
@SuppressWarnings("unchecked")
Map<Revision, Object> m = (Map<Revision, Object>) old;
if (m == null) {
m = new TreeMap<Revision, Object>(comparator);
doc.put(k.getName(), m);
}
if (k.getRevision() == null) {
throw new IllegalArgumentException("Cannot set map entry " + k.getName() + " with null revision");
}
m.put(k.getRevision(), op.value);
break;
}
case REMOVE_MAP_ENTRY: {
Object old = doc.get(k.getName());
@SuppressWarnings("unchecked")
Map<Revision, Object> m = (Map<Revision, Object>) old;
if (m != null) {
m.remove(k.getRevision());
}
break;
}
case CONTAINS_MAP_ENTRY:
// no effect