UpdateUtils { public static boolean checkConditions(@NotNull Document doc, @NotNull Map<Key, Condition> conditions) { for (Map.Entry<Key, Condition> entry : conditions.entrySet()) { Condition c = entry.getValue(); Key k = entry.getKey(); Object value = doc.get(k.getName()); Revision r = k.getRevision(); if (c.type == Condition.Type.EXISTS) { boolean checkForExistence = Boolean.TRUE.equals(c.value); if (value == null) { if (checkForExistence) { return false; } } else { if (r == null) { if (!checkForExistence) { return false; } } else if (value instanceof Map) { Map<?, ?> map = (Map<?, ?>) value; if (checkForExistence) { if (!map.containsKey(r)) { return false; } } else { if (map.containsKey(r)) { return false; } } } else { return false; } } } else if (c.type == Condition.Type.EQUALS || c.type == Condition.Type.NOTEQUALS) { if (r != null) { if (value instanceof Map) { value = ((Map) value).get(r); } else { value = null; } } boolean equal = Objects.equal(value, c.value); if (c.type == Condition.Type.EQUALS && !equal) { return false; } else if (c.type == Condition.Type.NOTEQUALS && equal) { return false; } } else { throw new IllegalArgumentException("Unknown condition: " + c.type); } } return true; }  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 checkConditions() { 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); op.setMapEntry("t", r, "value"); UpdateUtils.applyChanges(d, op); op = newUpdateOp(id); op.containsMapEntry("t", r, true); assertTrue(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.containsMapEntry("t", r, false); assertFalse(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.containsMapEntry("q", r, true); assertFalse(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.containsMapEntry("q", r, false); assertTrue(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.equals("t", r, "value"); assertTrue(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.notEquals("t", r, "value"); assertFalse(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.equals("t", r, "foo"); assertFalse(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.notEquals("t", r, "foo"); assertTrue(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.equals("t", Revision.newRevision(1), "value"); assertFalse(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.notEquals("t", Revision.newRevision(1), "value"); assertTrue(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.equals("t", "value"); assertFalse(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.notEquals("t", "value"); assertTrue(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.equals("p", r, 42L); assertFalse(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.notEquals("p", r, 42L); assertTrue(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.equals("p", 42L); assertTrue(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.notEquals("p", 42L); assertFalse(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.equals("p", 7L); assertFalse(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.notEquals("p", 7L); assertTrue(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.notEquals("other", 7L); assertTrue(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.notEquals("other", r, 7L); assertTrue(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.notEquals("other", r, null); assertFalse(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.equals("other", r, null); assertTrue(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.notEquals("p", null); assertTrue(UpdateUtils.checkConditions(d, op.getConditions())); op = newUpdateOp(id); op.notEquals("other", r, null); assertFalse(UpdateUtils.checkConditions(d, op.getConditions())); }