Class DefaultHistory

  • All Implemented Interfaces:
    java.lang.Iterable<History.Entry>, History

    public class DefaultHistory
    extends java.lang.Object
    implements History
    History using a file for persistent backing.

    Implementers should install shutdown hook to call save() to save history to disk.

    • Constructor Detail

      • DefaultHistory

        public DefaultHistory()
      • DefaultHistory

        public DefaultHistory​(LineReader reader)
    • Method Detail

      • getPath

        private java.nio.file.Path getPath()
      • attach

        public void attach​(LineReader reader)
        Description copied from interface: History
        Initialize the history for the given reader.
        Specified by:
        attach in interface History
        Parameters:
        reader - the reader to attach to
      • load

        public void load()
                  throws java.io.IOException
        Description copied from interface: History
        Load history.
        Specified by:
        load in interface History
        Throws:
        java.io.IOException - if a problem occurs
      • read

        public void read​(java.nio.file.Path file,
                         boolean checkDuplicates)
                  throws java.io.IOException
        Description copied from interface: History
        Read history from the file. If checkDuplicates is true only the events that are not contained within the internal list are added.
        Specified by:
        read in interface History
        Parameters:
        file - History file
        checkDuplicates - If true, duplicate history entries will be discarded
        Throws:
        java.io.IOException - if a problem occurs
      • doHistoryFileDataKey

        private java.lang.String doHistoryFileDataKey​(java.nio.file.Path path)
      • isLineReaderHistory

        private boolean isLineReaderHistory​(java.nio.file.Path path)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • setLastLoaded

        private void setLastLoaded​(java.nio.file.Path path,
                                   int lastloaded)
      • setEntriesInFile

        private void setEntriesInFile​(java.nio.file.Path path,
                                      int entriesInFile)
      • incEntriesInFile

        private void incEntriesInFile​(java.nio.file.Path path,
                                      int amount)
      • getLastLoaded

        private int getLastLoaded​(java.nio.file.Path path)
      • getEntriesInFile

        private int getEntriesInFile​(java.nio.file.Path path)
      • addHistoryLine

        protected void addHistoryLine​(java.nio.file.Path path,
                                      java.lang.String line)
      • addHistoryLine

        protected void addHistoryLine​(java.nio.file.Path path,
                                      java.lang.String line,
                                      boolean checkDuplicates)
      • purge

        public void purge()
                   throws java.io.IOException
        Description copied from interface: History
        Purge history.
        Specified by:
        purge in interface History
        Throws:
        java.io.IOException - if a problem occurs
      • write

        public void write​(java.nio.file.Path file,
                          boolean incremental)
                   throws java.io.IOException
        Description copied from interface: History
        Write history to the file. If incremental only the events that are new since the last incremental operation to the file are added.
        Specified by:
        write in interface History
        Parameters:
        file - History file
        incremental - If true incremental write operation is performed.
        Throws:
        java.io.IOException - if a problem occurs
      • append

        public void append​(java.nio.file.Path file,
                           boolean incremental)
                    throws java.io.IOException
        Description copied from interface: History
        Append history to the file. If incremental only the events that are new since the last incremental operation to the file are added.
        Specified by:
        append in interface History
        Parameters:
        file - History file
        incremental - If true incremental append operation is performed.
        Throws:
        java.io.IOException - if a problem occurs
      • save

        public void save()
                  throws java.io.IOException
        Description copied from interface: History
        Save history.
        Specified by:
        save in interface History
        Throws:
        java.io.IOException - if a problem occurs
      • internalWrite

        private void internalWrite​(java.nio.file.Path path,
                                   int from)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • trimHistory

        protected void trimHistory​(java.nio.file.Path path,
                                   int max)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • createEntry

        protected DefaultHistory.EntryImpl createEntry​(int index,
                                                       java.time.Instant time,
                                                       java.lang.String line)
        Create a history entry. Subclasses may override to use their own entry implementations.
        Parameters:
        index - index of history entry
        time - entry creation time
        line - the entry text
        Returns:
        entry object
      • internalClear

        private void internalClear()
      • size

        public int size()
        Specified by:
        size in interface History
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface History
      • index

        public int index()
        Specified by:
        index in interface History
      • first

        public int first()
        Specified by:
        first in interface History
      • last

        public int last()
        Specified by:
        last in interface History
      • format

        private java.lang.String format​(History.Entry entry)
      • get

        public java.lang.String get​(int index)
        Specified by:
        get in interface History
      • add

        public void add​(java.time.Instant time,
                        java.lang.String line)
        Specified by:
        add in interface History
      • matchPatterns

        protected boolean matchPatterns​(java.lang.String patterns,
                                        java.lang.String line)
      • internalAdd

        protected void internalAdd​(java.time.Instant time,
                                   java.lang.String line)
      • internalAdd

        protected void internalAdd​(java.time.Instant time,
                                   java.lang.String line,
                                   boolean checkDuplicates)
      • maybeResize

        private void maybeResize()
      • spliterator

        public java.util.Spliterator<History.Entry> spliterator()
        Specified by:
        spliterator in interface java.lang.Iterable<History.Entry>
      • resetIndex

        public void resetIndex()
        Description copied from interface: History
        Reset index after remove
        Specified by:
        resetIndex in interface History
      • moveToLast

        public boolean moveToLast()
        This moves the history to the last entry. This entry is one position before the moveToEnd() position.
        Specified by:
        moveToLast in interface History
        Returns:
        Returns false if there were no history iterator or the history index was already at the last entry.
      • moveTo

        public boolean moveTo​(int index)
        Move to the specified index in the history
        Specified by:
        moveTo in interface History
        Parameters:
        index - The index to move to.
        Returns:
        Returns true if the index was moved.
      • moveToFirst

        public boolean moveToFirst()
        Moves the history index to the first entry.
        Specified by:
        moveToFirst in interface History
        Returns:
        Return false if there are no iterator in the history or if the history is already at the beginning.
      • moveToEnd

        public void moveToEnd()
        Move to the end of the history buffer. This will be a blank entry, after all of the other iterator.
        Specified by:
        moveToEnd in interface History
      • current

        public java.lang.String current()
        Return the content of the current buffer.
        Specified by:
        current in interface History
        Returns:
        the content of the current buffer
      • previous

        public boolean previous()
        Move the pointer to the previous element in the buffer.
        Specified by:
        previous in interface History
        Returns:
        true if we successfully went to the previous element
      • next

        public boolean next()
        Move the pointer to the next element in the buffer.
        Specified by:
        next in interface History
        Returns:
        true if we successfully went to the next element
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • escape

        private static java.lang.String escape​(java.lang.String s)
      • unescape

        static java.lang.String unescape​(java.lang.String s)