Class ChangeTracker

java.lang.Object
org.litebridgedb.tracking.ChangeTracker

public final class ChangeTracker extends Object
This class provides functionality to track field-level changes of data transfer objects (DTOs) by maintaining a snapshot of their specified fields. It allows identifying changes made to the tracked fields during the object's lifecycle.

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 Details

  • Method Details

    • trackDto

      public <DTO> DTO trackDto(DTO dto)
    • trackDto

      public <DTO> DTO trackDto(DTO dto, Set<String> trackedFieldNames)
      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 the trackedFieldNames set, 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 null
      trackedFieldNames - 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 - if dto is null
      IllegalArgumentException - if any field name in trackedFieldNames does not exist in the DTO
    • trackDtoFields

      public <DTO> DTO trackDtoFields(DTO dto, Set<FieldAccessor> trackedFields)
      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 null
      trackedFields - 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 - if dto is null
    • trackDtoFields

      public <DTO> DTO trackDtoFields(DTO dto, Set<FieldAccessor> trackedFields, boolean snapshotEmpty)
    • getTrackedDto

      public <DTO> TrackedDto<DTO> getTrackedDto(DTO dto)
      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

      public <DTO> Set<TrackedDto<DTO>> getTrackedDtos(Class<DTO> dtoClass)
    • getTrackedDtoOrNull

      public <DTO> @Nullable TrackedDto<DTO> getTrackedDtoOrNull(DTO dto)
    • classFieldAccessorCache

      public ClassFieldAccessorCache classFieldAccessorCache()