Class FileAccumulator

java.lang.Object
com.scivicslab.turingworkflow.plugins.logoutput.FileAccumulator
All Implemented Interfaces:
com.scivicslab.pojoactor.core.accumulator.Accumulator, Closeable, AutoCloseable

public class FileAccumulator extends Object implements com.scivicslab.pojoactor.core.accumulator.Accumulator, Closeable
An Accumulator implementation that writes output entries to a text file.

Each entry is formatted with a [source] prefix on every line and flushed to disk immediately. The accumulator implements Closeable so it can be used in try-with-resources blocks.

Once close() has been called, subsequent calls to add(java.lang.String, java.lang.String, java.lang.String) will increment the counter but produce no file output.

Since:
1.0
Author:
devteam@scivicslab.com
  • Constructor Details

    • FileAccumulator

      public FileAccumulator(Path filePath) throws IOException
      Constructs a FileAccumulator that writes to the specified file path. The file is created (or truncated) immediately.
      Parameters:
      filePath - the path to the output file
      Throws:
      IOException - if the file cannot be opened for writing
    • FileAccumulator

      public FileAccumulator(String filePath) throws IOException
      Constructs a FileAccumulator that writes to the specified file path string.
      Parameters:
      filePath - the path string to the output file
      Throws:
      IOException - if the file cannot be opened for writing
  • Method Details

    • getFilePath

      public Path getFilePath()
      Returns the path to the output file.
      Returns:
      the file path
    • add

      public void add(String source, String type, String data)

      Writes the formatted entry to the file and flushes immediately. If the accumulator is closed or the data is null/empty, only the counter is incremented.

      Specified by:
      add in interface com.scivicslab.pojoactor.core.accumulator.Accumulator
    • getSummary

      public String getSummary()
      Specified by:
      getSummary in interface com.scivicslab.pojoactor.core.accumulator.Accumulator
    • getCount

      public int getCount()
      Specified by:
      getCount in interface com.scivicslab.pojoactor.core.accumulator.Accumulator
    • clear

      public void clear()
      Specified by:
      clear in interface com.scivicslab.pojoactor.core.accumulator.Accumulator
    • close

      public void close()
      Closes the underlying file writer. After this method returns, subsequent calls to add(java.lang.String, java.lang.String, java.lang.String) will no longer produce file output. Calling close() multiple times has no additional effect.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable