Class DataContextChangeTracker

java.lang.Object
io.jmix.flowui.model.impl.DataContextChangeTracker

public class DataContextChangeTracker extends Object
Per-attribute dirty state of a DataContextImpl: which attributes of which managed instances the user changed, and the baseline value each change is measured against. Baselines exist only for dirty attributes; an attribute whose current value returns to its baseline is removed. Entity-level dirty membership is signalled through the two callbacks.
  • Constructor Details

    • DataContextChangeTracker

      public DataContextChangeTracker(Consumer<Object> onEntityDirty, Consumer<Object> onEntityClean)
      Parameters:
      onEntityDirty - callback invoked when an entity becomes dirty (gains its first dirty attribute)
      onEntityClean - callback invoked when an entity becomes clean again (loses its last dirty attribute)
  • Method Details

    • trackChange

      public void trackChange(Object entity, String attribute, @Nullable Object prevValue, @Nullable Object newValue, boolean reference)
      Records a scalar or single-reference attribute change against its baseline. If a baseline already exists for the attribute, the change is measured against it: the attribute is un-dirtied when the current value returns to the baseline, and nothing else happens otherwise. With no baseline yet, the attribute becomes dirty (with prevValue as baseline) only when the value actually changed.
      Parameters:
      reference - true to compare by entity id / identity rather than by value
    • snapshotCollectionBaseline

      public void snapshotCollectionBaseline(Object entity, String attribute, Collection<?> baselineContents)
      Snapshots the membership of a to-many attribute as its baseline, so a later mutation can be compared against it (see trackCollectionChange(java.lang.Object, java.lang.String, java.util.Collection<?>)). Called by the merge when it installs a collection. Only a clean attribute's baseline is (re)snapshotted; a dirty attribute keeps its existing baseline so an unsaved user edit is not measured against freshly merged contents.
    • trackCollectionChange

      public void trackCollectionChange(Object entity, String attribute, Collection<?> current)
      Records the current membership of a to-many attribute against its snapshotted baseline: the attribute is un-dirtied when the membership matches the baseline again, and marked dirty when it differs. When no baseline was snapshotted the attribute is marked dirty conservatively (the change cannot be reconstructed). Called from the observable-collection mutation callback.
    • isAttributeDirty

      public boolean isAttributeDirty(Object entity, String attribute)
      Whether the given attribute of the entity currently carries an unsaved change.
    • getModifiedAttributes

      public Set<String> getModifiedAttributes(Object entity)
      The names of the entity's currently-dirty attributes (embedded sub-attributes as dotted paths), as an unmodifiable snapshot; empty if the entity is clean.
    • rebaseline

      public void rebaseline(Object entity, String attribute, @Nullable Object incomingValue, @Nullable Object currentValue, boolean reference)
      Moves a dirty scalar or single-reference attribute's baseline to an incoming (merged) value, so the user's edit is henceforth measured against it; the attribute is un-dirtied if it now equals that value. No-op when the attribute is not dirty. Used by a fresh merge, which rebaselines protected user edits rather than overwriting them.
      Parameters:
      reference - true to compare by entity id / identity rather than by value
    • rebaselineCollection

      public void rebaselineCollection(Object entity, String attribute, Collection<?> incoming, Collection<?> current)
      The to-many counterpart of rebaseline(java.lang.Object, java.lang.String, java.lang.Object, java.lang.Object, boolean): moves a dirty collection attribute's baseline to the membership of an incoming (merged) collection, un-dirtying it if the current membership now matches. No-op when the attribute is not dirty.
    • markDirty

      public void markDirty(Object entity, String attribute, @Nullable Object baseline)
      Marks an attribute dirty with an explicit baseline, without a value comparison. Used to import dirty state from a child context (see DataContextImpl.mergeFromChild).
    • markSetLoaded

      public void markSetLoaded(Object entity, String attribute)
      Remembers that an attribute was made loaded by a user set or a non-null merge-install, so the loaded flag can be re-asserted after a fresh merge installs a narrower loaded-state cache (see setLoadedAttributes(java.lang.Object) and DataContextImpl.reapplySetLoaded).
    • setLoadedAttributes

      public Set<String> setLoadedAttributes(Object entity)
      The attributes previously recorded by markSetLoaded(java.lang.Object, java.lang.String) for the entity; empty if none. Survives clear(), and is dropped only when the entity leaves the context (see drop(java.lang.Object)).
    • drop

      public void drop(Object entity)
      Forgets all tracked state (baselines, dirty attributes and set-loaded markers) for an entity that is leaving the context, firing the clean callback if it was dirty.
    • clear

      public void clear()
      Drops all baselines and dirty attributes for every entity (e.g. on save or clear-changes). Set-loaded markers are intentionally kept, unlike drop(java.lang.Object).
    • dirtyAttributes

      protected Set<String> dirtyAttributes(Object entity)
    • putDirty

      protected void putDirty(Object entity, String attribute, @Nullable Object baseline)
    • markAttributeDirty

      protected void markAttributeDirty(Object entity, String attribute)
    • removeDirty

      protected void removeDirty(Object entity, String attribute)
    • removeDirtyKeepBaseline

      protected void removeDirtyKeepBaseline(Object entity, String attribute)
    • refKey

      protected static @Nullable Object refKey(@Nullable Object refOrId)
    • membershipBag

      public static Map<Object,Integer> membershipBag(Collection<?> collection)
      The membership bag (element key -> count) of a collection, used as a to-many attribute's baseline. Each element is keyed by its entity id, or by identity when it has none.