Java Object Diff and Patching

Viewed 100

I am trying to figure out a way to version complex Data Objects in Java. For this, I intend to store both the initial document and the diffs in a database (as well as the current object for faster access), and then be able to restore any version from the initial document by applying the diffs onto it as patches.

Thus far, I have come across Javers and java-object-diff for that, however...

  • Javers has no way of patching
  • In the java-object-diff documentation, it seems like you need both the initial as well as the target version to apply diffs, which seems kinda pointless since I want to get the target version

This seems like something that someone would have implemented a long time ago. However, thus far, I have not been able to figure out a working solution.

Can someone please help me with this?

For reference, I am currently trying to get it to work somewhat like this:

    @Test
    void applyDiffDeltaShouldCorrectlyApplyDeltaToCity() {
        GameCharacter sylvia = GameCharacters.sylvia();
        GameCharacter sylviaEdited = GameCharacters.sylviaEdited();
        
        DiffNode characterDelta
            = objectDiffer.compare(sylvia, sylviaEdited);
        GameCharacter sylviaWithAppliedDelta
            = applyDelta(sylvia, characterDelta);
        
        assertEquals(sylviaEdited.getName(), sylviaWithAppliedDelta.getName());
    }

    public static <T> T applyDelta(T baseObject, DiffNode diff) {
        //This won't work, but it would be cool if it did:
        T updatedObject = baseObject + diff;
        return updatedObject ;
    }
0 Answers
Related