Class History


  • public class History
    extends Object
    Track done and undone changes. Done changes can be undone; undone changes can be redone. Each change is actually not tracked directly but through a history entry. The history entry stores only the metadata, while the change object stores the actual data. Thus, the history entries are much smaller and can be kept in memory, while the change objects are only loaded into memory on demand.
    • Field Detail

      • _projectId

        protected final long _projectId
      • _position

        protected int _position
      • _cachedPosition

        protected int _cachedPosition
      • _desiredCachedPosition

        protected int _desiredCachedPosition
      • _positionBeingCached

        protected int _positionBeingCached
      • _cachingFuture

        protected Future<Void> _cachingFuture
      • _cachingExecutorService

        protected final ExecutorService _cachingExecutorService
      • _lastModified

        protected Instant _lastModified
    • Constructor Detail

      • History

        public History​(Grid initialGrid,
                       ChangeDataStore dataStore,
                       GridCache gridStore,
                       long projectId)
        Creates an empty on an initial grid.
        Parameters:
        initialGrid - the initial state of the project
        dataStore - where to store change data
        gridStore - where to store intermediate cached grids
      • History

        public History​(Grid initialGrid,
                       ChangeDataStore dataStore,
                       GridCache gridStore,
                       List<HistoryEntry> entries,
                       int position,
                       long projectId)
                throws OperationException
        Constructs a history with an initial grid and a list of history entries.
        Parameters:
        initialGrid - the first grid of the project, at creation time
        dataStore - where change data is stored for all changes of the project
        entries - the list of entries of the history
        position - the current position in the history
        Throws:
        OperationException - if one step in the list of history entries failed to apply to the supplied grid
    • Method Detail

      • getCurrentGrid

        public Grid getCurrentGrid()
        Returns the state of the grid at the current position in the history.
      • currentGridNeedsRefreshing

        public boolean currentGridNeedsRefreshing()
        Is the current grid incomplete? If so, it should be refreshed with refreshCurrentGrid() after a while.
      • getInitialGrid

        public Grid getInitialGrid()
        Returns the state of the grid at before any operation was applied on it
      • getGrid

        protected Grid getGrid​(int position,
                               boolean refresh)
                        throws OperationException
        Returns the state of the grid at a given index in the history
        Parameters:
        position - a 0-based index in the list of successive grids
        refresh - whether the grid should be refreshed if it depends on change data being currently fetched
        Throws:
        OperationException
      • getPosition

        public int getPosition()
      • getCurrentEntryId

        public long getCurrentEntryId()
      • getCachedPosition

        public int getCachedPosition()
      • getGridCache

        public GridCache getGridCache()
      • getLastModified

        public Instant getLastModified()
        The last time this history was modified (or created).
      • updateCachedPosition

        protected void updateCachedPosition()
      • refreshCurrentGrid

        public void refreshCurrentGrid()
      • getLastPastEntries

        public List<HistoryEntry> getLastPastEntries​(int count)
      • undoRedo

        public GridPreservation undoRedo​(long lastDoneEntryID)
                                  throws OperationException
        Rewinds or brings the history forward.
        Parameters:
        lastDoneEntryID - the id of the last change to be performed before the desired state of the project. Use 0L for the initial state.
        Returns:
        the degree to which the grid was preserved while changing the position in the history
        Throws:
        OperationException - if the application of changes required for this move did not succeed
      • getPrecedingEntryID

        public long getPrecedingEntryID​(long entryID)
        Given an history entry id, return the id of the preceding history entry, or -1 if there is none.
      • dispose

        public void dispose()
        Uncaches all grids from memory and stops all related fetching and caching processes.
      • entryIndex

        public int entryIndex​(long entryID)
        Return the position of the history entry with the supplied id, or throws IllegalArgumentException if that id cannot be found.
      • getEntry

        protected HistoryEntry getEntry​(long entryID)