class PatchUtil {

    public static <A, B> Map<A, B> setMap(Map<A, B> instance, Map<A, B> newValues, boolean patch) {
        if (patch) {
            if (newValues == null || newValues.size() == 0) {
                return instance;
            }
        }

        if (instance == null) {
            instance = new HashMap<>();
        }

        if (patch) {
            for (Map.Entry<A, B> entry : newValues.entrySet()) {
                if (entry.getValue() == null || realValue(entry.getValue()) == null) {
                    // remove
                    instance.remove(entry.getKey());
                } else {
                    // add or update
                    instance.put(entry.getKey(), entry.getValue());
                }
            }
        } else {
            instance.clear();
            instance.putAll(safe(newValues));
        }

        return instance;
    }

}

class PatchUtilTest {

    private TestObject testObject;

    @Test
    public void check_an_update_override_everything() throws Exception {

        Map<String, String> patch = new HashMap<String, String>() {{
            put("d", "4");
        }};

        PatchUtil.setMap(testObject.getMap(), patch, false);

        assertThat(testObject.getMap()).hasSize(1);
        assertThat(testObject.getMap()).containsEntry("d", "4");
    }

}