I have two java objects and I want to merge them into single object. Problem is the two objects does not contain plain primitive type properties(fields) they contain complex
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;
public final class PropertyMerger {
public static void mergeProperty(
Supplier sourceGetter,
Supplier targetGetter,
Consumer targetSetter
) {
var source = sourceGetter.get();
var target = targetGetter.get();
if (!Objects.equals(source, target)) {
targetSetter.accept(source);
}
}
}
Somwhere in your code:
PropertyMerger.mergeProperty(facebookOAuth2User::getId, existingFacebookOAuth2UserDB::getFacebookId, existingFacebookOAuth2UserDB::setFacebookId);
PropertyMerger.mergeProperty(facebookOAuth2User::getName, existingFacebookOAuth2UserDB::getName, existingFacebookOAuth2UserDB::setName);