UpdateUtils { public static void applyChanges(@NotNull Document doc, @NotNull UpdateOp update) { doc.put(Document.ID, update.getId()); 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 REMOVE: { doc.remove(k.toString()); break; } case MAX: { Comparable newValue = (Comparable) op.value; Object old = doc.get(k.toString()); if (old == null || newValue.compareTo(old) > 0) { 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>(StableRevisionComparator.REVERSE); 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; } } } }  static void applyChanges(@NotNull Document doc,
                                    @NotNull UpdateOp update); static boolean checkConditions(@NotNull Document doc,
                                          @NotNull Map<Key, Condition> conditions); static void assertUnconditional(@NotNull UpdateOp up);  }
@Test public void applyChanges() { Revision r = Revision.newRevision(1); String id = Utils.getIdFromPath("/foo"); Document d = new Document(); d.put(Document.ID, id); UpdateOp op = newUpdateOp(id); op.set("p", 42L); UpdateUtils.applyChanges(d, op); assertEquals(42L, d.get("p")); op = newUpdateOp(id); op.max("p", 23L); UpdateUtils.applyChanges(d, op); assertEquals(42L, d.get("p")); op = newUpdateOp(id); op.max("p", 58L); UpdateUtils.applyChanges(d, op); assertEquals(58L, d.get("p")); op = newUpdateOp(id); op.increment("p", 3); UpdateUtils.applyChanges(d, op); assertEquals(61L, d.get("p")); op = newUpdateOp(id); op.setMapEntry("t", r, "value"); UpdateUtils.applyChanges(d, op); assertEquals("value", getMapEntry(d, "t", r)); op = newUpdateOp(id); op.removeMapEntry("t", r); UpdateUtils.applyChanges(d, op); assertNull(getMapEntry(d, "t", r)); }