I have Java classes with some nullable members, which get constructed from JSON objects with some optional keys. After construction, I will modify the objects, populating some previously-null members, and modifying or nulling some that existed in the JSON.
I need a method that will return a new object where all the nullable fields that I’ve filled in or modified remain populated, but the ones I didn’t touch get nulled even if they weren’t null when the object was constructed.
How can I accomplish this?
The constructor could create an empty set to track fields that have been changed, then all the setters could add their field to that set, then the copy function could check that set to determine which fields to null if they haven’t been changed.