The ChangeTracker is particularly useful for scenarios involving change tracking
or auditing where fields of certain objects need to be monitored for modifications.
This class is thread-safe.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionRetrieves the instance ofClassFieldAccessorCacheassociated with theChangeTracker.<DTO> TrackedDto<DTO> getTrackedDto(DTO dto) Retrieves the tracked version of the specified Data Transfer Object (DTO), if it exists.<DTO> @Nullable TrackedDto<DTO> getTrackedDtoOrNull(DTO dto) Retrieves the tracked version of the specified Data Transfer Object (DTO), if it exists.<DTO> Set<TrackedDto<DTO>> getTrackedDtos(Class<DTO> dtoClass) Retrieves a set of tracked Data Transfer Objects (DTOs) of the specified type.<DTO> DTOtrackDto(DTO dto) Tracks the given Data Transfer Object (DTO) for detecting changes.<DTO> DTOTracks the specified fields of a given Data Transfer Object (DTO) for detecting changes.<DTO> DTOtrackDtoFields(DTO dto, Set<FieldAccessor> trackedFields) Tracks the specified fields of a given Data Transfer Object (DTO) for detecting changes.<DTO> DTOtrackDtoFields(DTO dto, Set<FieldAccessor> trackedFields, boolean snapshotEmpty) Tracks the fields of the given Data Transfer Object (DTO) for detecting changes.
-
Constructor Details
-
ChangeTracker
public ChangeTracker() -
ChangeTracker
-
-
Method Details
-
trackDto
public <DTO> DTO trackDto(DTO dto) Tracks the given Data Transfer Object (DTO) for detecting changes.This method initializes tracking for the DTO and ensures that its state can be monitored for modifications during its lifecycle.
- Type Parameters:
DTO- the type of the Data Transfer Object (DTO) to be tracked- Parameters:
dto- the Data Transfer Object (DTO) to be tracked; must not be null- Returns:
- the tracked instance of the provided DTO
- Throws:
NullPointerException- if the provideddtois null
-
trackDto
Tracks the specified fields of a given Data Transfer Object (DTO) for detecting changes. This method identifies and stores a snapshot of the fields provided in thetrackedFieldNamesset, enabling monitoring of modifications during the lifecycle of the DTO.- Type Parameters:
DTO- the type of the DTO being tracked- Parameters:
dto- the Data Transfer Object (DTO) to be tracked; must not be nulltrackedFieldNames- the set of field names in the DTO to be tracked; each field name must exist in the DTO- Returns:
- the tracked instance of the provided DTO
- Throws:
IllegalArgumentException- ifdtois nullIllegalArgumentException- if any field name intrackedFieldNamesdoes not exist in the DTO
-
trackDtoFields
Tracks the specified fields of a given Data Transfer Object (DTO) for detecting changes.This method allows monitoring of specific fields within the DTO by capturing their initial state.
- Type Parameters:
DTO- the type of the DTO being tracked- Parameters:
dto- the Data Transfer Object (DTO) to be tracked; must not be nulltrackedFields- the set of fields in the DTO to be tracked; each field must belong to the DTO- Returns:
- the tracked instance of the provided DTO
- Throws:
IllegalArgumentException- ifdtois null
-
trackDtoFields
Tracks the fields of the given Data Transfer Object (DTO) for detecting changes.This method allows monitoring specific fields within the DTO by capturing their initial state and optionally creating an empty snapshot.
- Type Parameters:
DTO- the type of the DTO being tracked- Parameters:
dto- the Data Transfer Object (DTO) to be tracked; must not be nulltrackedFields- the set of fields in the DTO to be tracked; each field must belong to the DTOsnapshotEmpty- if true, an empty snapshot will be created; otherwise, normal snapshot behavior is applied- Returns:
- the tracked instance of the provided DTO
- Throws:
NullPointerException- if the provideddtois null
-
getTrackedDto
Retrieves the tracked version of the specified Data Transfer Object (DTO), if it exists.The method looks up the internal storage for the tracked version of the given DTO and returns it.
- Parameters:
dto- the Data Transfer Object (DTO) whose tracked version is to be retrieved; can be null- Returns:
- the tracked version of the specified DTO, or null if no tracked version exists
- Throws:
IllegalArgumentException- if the specified DTO is not tracked
-
getTrackedDtos
Retrieves a set of tracked Data Transfer Objects (DTOs) of the specified type.- Type Parameters:
DTO- the type of the Data Transfer Object (DTO) to be retrieved- Parameters:
dtoClass- theClassobject representing the type of DTO to retrieve; must not be null- Returns:
- a set of
TrackedDto<DTO>objects that correspond to the specified DTO type; returns an empty set if no tracked DTOs of the specified type exist - Throws:
NullPointerException- ifdtoClassis null
-
getTrackedDtoOrNull
Retrieves the tracked version of the specified Data Transfer Object (DTO), if it exists.The method attempts to fetch the associated tracked DTO from the internal storage and returns it. If no tracked version exists for the provided DTO, the method returns
null.- Type Parameters:
DTO- the type of the Data Transfer Object (DTO)- Parameters:
dto- the Data Transfer Object (DTO) whose tracked version is to be retrieved; can be null- Returns:
- the tracked version of the specified DTO, or
nullif no tracked version exists
-
classFieldAccessorCache
Retrieves the instance ofClassFieldAccessorCacheassociated with theChangeTracker.- Returns:
- the
ClassFieldAccessorCacheinstance used by thisChangeTracker
-