- Type Parameters:
DTO- the type of the DTO object being tracked
TrackedDto allows tracking changes in the fields of a given DTO (Data Transfer Object) over time.
It maintains snapshots of field states and can identify changes by comparing the current state
of the DTO's fields with previous snapshots.
This class is immutable and thread-safe. A typical usage involves taking snapshots of the DTO's fields, detecting changes, and optionally tracking nested DTOs or collections/maps of nested DTOs.
-
Constructor Summary
ConstructorsConstructorDescriptionTrackedDto(DTO dto, Collection<FieldAccessor> fields, ClassFieldAccessorCache classFieldAccessorCache, Consumer<Object> trackDtoCallback) Construct aTrackedDtoinstance that wraps and tracks a given data transfer object (DTO).TrackedDto(DTO dto, ClassFieldAccessorCache classFieldAccessorCache, Consumer<Object> trackDtoCallback) Construct aTrackedDtoinstance that tracks all fields of the given DTO object. -
Method Summary
Modifier and TypeMethodDescriptionRetrieves the collection of modified fields in the tracked DTO since the last snapshot.changedFields(boolean refresh) Retrieves the collection of modified fields in the tracked DTO since the last snapshot.dto()Retrieve the underlying DTO that is being tracked.voidsnapshot(boolean overwrite) Captures the current state of the tracked fields, creating a snapshot for later comparison.voidInitializes an empty snapshot of the tracked fields for the current object.
-
Constructor Details
-
TrackedDto
public TrackedDto(DTO dto, ClassFieldAccessorCache classFieldAccessorCache, Consumer<Object> trackDtoCallback) Construct aTrackedDtoinstance that tracks all fields of the given DTO object.- Parameters:
dto- the data transfer object (DTO) to be wrapped and tracked; must not be nulltrackDtoCallback- the callback function to be triggered when tracking changes
-
TrackedDto
public TrackedDto(DTO dto, Collection<FieldAccessor> fields, ClassFieldAccessorCache classFieldAccessorCache, Consumer<Object> trackDtoCallback) Construct aTrackedDtoinstance that wraps and tracks a given data transfer object (DTO).- Parameters:
dto- the data transfer object (DTO) to be wrapped and tracked; must not be nullfields- the collection ofFieldAccessorobjects representing the fields to be tracked; must not be nulltrackDtoCallback- the callback function to be triggered when tracking changes; must not be null- Throws:
IllegalArgumentException- if any of the parameters are null
-
-
Method Details
-
dto
Retrieve the underlying DTO that is being tracked.Ensures that the DTO has not been garbage collected. If the DTO has been garbage collected, an exception is thrown.
- Returns:
- the non-null tracked
DTOinstance - Throws:
IllegalStateException- if the DTO object has been garbage collected
-
snapshot
public void snapshot(boolean overwrite) Captures the current state of the tracked fields, creating a snapshot for later comparison.- Parameters:
overwrite- iftrue, any existing snapshots are cleared, and a new snapshot is taken; iffalse, an exception is thrown if a snapshot already exists- Throws:
IllegalStateException- if a previous snapshot has already been taken andoverwriteisfalse.
-
snapshotEmpty
public void snapshotEmpty()Initializes an empty snapshot of the tracked fields for the current object.This method captures the initial state of all fields as
null, with a hash value of 0.- Throws:
IllegalStateException- if a previous snapshot has already been taken.
-
changedFields
Retrieves the collection of modified fields in the tracked DTO since the last snapshot.On first invocation after a snapshot, the DTO is evaluated against the existing snapshot and a
ChangedFieldsresult is generated. ThisChangedFieldsinstance is cached, so subsequent calls to this method will return the same instance and is safe to use repeatedly.- Returns:
- a
ChangedFieldsinstance representing the modified fields - See Also:
-
changedFields
Retrieves the collection of modified fields in the tracked DTO since the last snapshot.If
refreshistrue, the method updates the internal snapshots based on the current modifications before returning the changed fields; effectively refreshing the tracking state. This means that the next call tochangedFields()will no longer return the same resultIf
refreshisfalse, the method behaves exactly aschangedFields().- Parameters:
refresh- a boolean flag indicating whether to refresh the state of tracked fields before determining the changed fields. Iftrue, the method updates the internal snapshots to reflect the latest state of the tracked fields. Iffalse, the method uses the previously cached snapshots to determine changed fields.- Returns:
- a
ChangedFieldsinstance representing the modified fields in the tracked DTO. If no changes have occurred, this instance will represent an empty set of changed fields. - Throws:
IllegalStateException- if field snapshots have not been previously taken.- See Also:
-