Change implements Serializable { private Change(@Nullable C originalObject, @Nullable C newObject) { Preconditions.checkState(originalObject != null || newObject != null, "Change should have at least one non null object"); this.originalObject = originalObject; this.newObject = newObject; } private  Change(@Nullable C originalObject, @Nullable C newObject); static Change<C> add(C newObject); static Change<C> update(C oldObject, C newObject); static Change<C> delete(C oldObject); static Change<C> identity(C object); Change<C> map(UnaryOperator<C> mapper); Optional<C> getNewObject(); Optional<C> getOriginalObject(); Identity<?> getId(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); @CheckForNull final C originalObject; @CheckForNull final C newObject; }
@Test void change_withNullOriginal_returnsAdd() { Change<TestState> change = Change.change(null, OBJECT1_V1); Assertions.assertNull(change.originalObject, "Original object set unexpectedly"); Assertions.assertEquals(OBJECT1_V1, change.newObject, "New object not set as expected"); }
@Test void change_withNonNullMatched_returnsUpdate() { Change<TestState> change = Change.change(OBJECT1_V1, OBJECT1_V2); Assertions.assertEquals(OBJECT1_V1, change.originalObject, "Original object not set as expected"); Assertions.assertEquals(OBJECT1_V2, change.newObject, "New object not set as expected"); }
@Test void change_withNullNew_returnsDelete() { Change<TestState> change = Change.change(OBJECT1_V1, null); Assertions.assertEquals(OBJECT1_V1, change.originalObject, "Original object not set as expected"); Assertions.assertNull(change.newObject, "New object set unexpectedly"); }
@Test void change_withBothNull_throwsException() { Assertions.assertThrows(IllegalArgumentException.class, () -> Change.change(null, null), "Expected exception on changing null to null"); }
@Test void change_withMismatch_throwsException() { Assertions.assertThrows(IllegalArgumentException.class, () -> Change.change(OBJECT1_V1, OBJECT2), "Expected exception on changing with mismatched ids"); }