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;
    }

}