<commit_before>package etomica.data;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import etomica.action.activity.ControllerEvent;
import etomica.action.activity.ControllerListener;

/**
 * DataSink that manages a FileWriter and also listens to non-interval 
 * integrator events and sends appropriate data to a DataWriter.
 */
public class DataLogger extends DataProcessor implements ControllerListener, java.io.Serializable {
    
    public DataLogger(){
        super();
        setWriteInterval(100);
    }
    
    public void setDataSink(DataSink dataSink) {
        if (!(dataSink instanceof DataWriter)) {
            throw new IllegalArgumentException("data sink must be a DataWriter");
        }
        super.setDataSink(dataSink);
    }
    
    /**
     * Gives data to DataSink for writing
     */
    public Data processData(Data data) {
        if (--count == 0) {
            count = writeInterval;
            doWrite(data);
        }
        return data;
    }
    
    public IDataInfo processDataInfo(IDataInfo newDataInfo) {
        dataInfo = newDataInfo.getFactory().makeDataInfo();
        dataInfo.addTag(tag);
        return dataInfo;
    }
    
    public DataPipe getDataCaster(IDataInfo incomingDataInfo) {
        // we don't care about the type although the DataWriter might
        return null;
    }
    
    /**
     * Close file when integrator is done.
     */
    public void actionPerformed(ControllerEvent evt){
        if(dataSink != null && (evt.getType() == ControllerEvent.NO_MORE_ACTIONS ||
            evt.getType() == ControllerEvent.HALTED)) {
            if (writeOnFinishSource != null) {
                doWrite(writeOnFinishSource.getData());
            }
            closeFile(); //close the file when finished
        }
    }
    
    /**
     * Performs the open/write/close file actions in accordance with the
     * settings of the Logger.  Called by intervalAction and by the
     * actionPerformed method of any Actions made by the writeAction method.
     */
    private void doWrite(Data data) {
        openFile();
        ((DataWriter)dataSink).setFileWriter(fileWriter);
        dataSink.putData(data);
        if(closeFileEachTime || !appending) {
            closeFile();
        }
    }
    
    /**
     * Define how many number of interval events received between writings to
     * file.
     */
    public void setWriteInterval(int newWriteInterval){
        writeInterval = newWriteInterval;
        count = writeInterval;
    }
    
    public int getWriteInterval() {
        return writeInterval;
    }
    
    
    private void openFile(){
        try { 
            if(sameFileEachTime && fileIsOpen) return;
            if(fileName == "") fileName = defaultFileName(); //if fileName is not defined yet, use the current date to be the fileName.
            fileWriter = new FileWriter(fileName + fileNameSuffix, appending);
            if (!appending) {
                ((DataWriter)dataSink).reset();
            }
            fileIsOpen = true;
        }
        catch(IOException e) {
            System.err.println("Cannot open a file, caught IOException: " + e.getMessage());
        }
    }
    
    protected String defaultFileName() {//now use the current date to be the fileName, subclass could change this.
        SimpleDateFormat formatter = new SimpleDateFormat("MMMd_H_mm_ss", new Locale("en","US"));
        Date now = new Date();
        String currentTime = formatter.format(now);
        return currentTime;
    }
    
    public void closeFile(){ 
        try {
            if(fileIsOpen) { fileWriter.close(); fileIsOpen = false;}
            if(!sameFileEachTime) fileName = "";
        }catch(IOException e) {
            System.err.println("Cannot close a file, caught IOException: " + e.getMessage());
        }
    }
    
    /**<commit_msg>ask the superclass DataProcessor to put the data<commit_after>package etomica.data;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import etomica.action.activity.ControllerEvent;
import etomica.action.activity.ControllerListener;

/**
 * DataSink that manages a FileWriter and also listens to non-interval 
 * integrator events and sends appropriate data to a DataWriter.
 */
public class DataLogger extends DataProcessor implements ControllerListener, java.io.Serializable {
    
    public DataLogger(){
        super();
        setWriteInterval(100);
    }
    
    public void setDataSink(DataSink dataSink) {
        if (!(dataSink instanceof DataWriter)) {
            throw new IllegalArgumentException("data sink must be a DataWriter");
        }
        super.setDataSink(dataSink);
    }
    
    /**
     * Gives data to DataSink for writing
     */
    public Data processData(Data data) {
 
        return data;
    }
    
    public IDataInfo processDataInfo(IDataInfo newDataInfo) {
        dataInfo = newDataInfo.getFactory().makeDataInfo();
        dataInfo.addTag(tag);
        return dataInfo;
    }
    
    public DataPipe getDataCaster(IDataInfo incomingDataInfo) {
        // we don't care about the type although the DataWriter might
        return null;
    }
    
    /**
     * Close file when integrator is done.
     */
    public void actionPerformed(ControllerEvent evt){
        if(dataSink != null && (evt.getType() == ControllerEvent.NO_MORE_ACTIONS ||
            evt.getType() == ControllerEvent.HALTED)) {
            if (writeOnFinishSource != null) {
                putData(writeOnFinishSource.getData());
            }
            closeFile(); //close the file when finished
        }
    }
    
    /**
     * Performs the open/write/close file actions in accordance with the
     * settings of the Logger.  Called by intervalAction and by the
     * actionPerformed method of any Actions made by the writeAction method.
     */
    public void putData(Data data) {
        openFile();
        ((DataWriter)trueDataSink).setFileWriter(fileWriter);
        super.putData(data);
        if(closeFileEachTime || !appending) {
            closeFile();
        }
    }
    
    /**
     * Define how many number of interval events received between writings to
     * file.
     */
    public void setWriteInterval(int newWriteInterval){
        writeInterval = newWriteInterval;
        count = writeInterval;
    }
    
    public int getWriteInterval() {
        return writeInterval;
    }
    
    
    private void openFile(){
        try { 
            if(sameFileEachTime && fileIsOpen) return;
            if(fileName == "") fileName = defaultFileName(); //if fileName is not defined yet, use the current date to be the fileName.
            fileWriter = new FileWriter(fileName + fileNameSuffix, appending);
            if (!appending) {
                ((DataWriter)trueDataSink).reset();
            }
            fileIsOpen = true;
        }
        catch(IOException e) {
            System.err.println("Cannot open a file, caught IOException: " + e.getMessage());
        }
    }
    
    protected String defaultFileName() {//now use the current date to be the fileName, subclass could change this.
        SimpleDateFormat formatter = new SimpleDateFormat("MMMd_H_mm_ss", new Locale("en","US"));
        Date now = new Date();
        String currentTime = formatter.format(now);
        return currentTime;
    }
    
    public void closeFile(){ 
        try {
            if(fileIsOpen) { fileWriter.close(); fileIsOpen = false;}
            if(!sameFileEachTime) fileName = "";
        }catch(IOException e) {
            System.err.println("Cannot close a file, caught IOException: " + e.getMessage());
        }
    }
    
    /**
{'fraction_non_alphanumeric': 0.09349021614017138, 'fraction_numerical': 0.0008952551477170994, 'mean_word_length': 2.266499582289056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6173255', 'n_tokens_mistral': 2069, 'n_tokens_neox': 2090, 'n_words': 581}