<commit_before>package eu.goodlike.io;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardOpenOption.CREATE_NEW;

/**
 * <pre>
 * Synchronized appended for files
 *
 * In most cases a properly configured logger is preferred, but for simple file operations, this should suffice
 * </pre>
 */
public final class FileAppender implements AutoCloseable {

    /**
     * Appends given string to the file of this appender
     */
    public synchronized void append(String string) throws IOException {
        bufferedWriter.append(string);
    }

    /**
     * Appends given string and a new line symbol to the file of this appender
     */
    public void appendLine(String string) throws IOException {
        append(string + System.lineSeparator());
    }

    @Override
    public void close() throws Exception {
        bufferedWriter.close();
    }

    // CONSTRUCTORS

    public Optional<FileAppender> ofFile(String filename) {
        Path path = Paths.get(filename);
        if (!Files.isWritable(path))
            return Optional.empty();

        BufferedWriter bufferedWriter;
        try {
            bufferedWriter = Files.newBufferedWriter(path, UTF_8, CREATE_NEW);
        } catch (IOException e) {
            return Optional.empty();
        }
        return Optional.of(new FileAppender(bufferedWriter));
    }

    private FileAppender(BufferedWriter bufferedWriter) {
        this.bufferedWriter = bufferedWriter;
    }

    // PRIVATE<commit_msg>Fixed a bug where static method was not static<commit_after>package eu.goodlike.io;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardOpenOption.CREATE_NEW;

/**
 * <pre>
 * Synchronized appended for files
 *
 * In most cases a properly configured logger is preferred, but for simple file operations, this should suffice
 * </pre>
 */
public final class FileAppender implements AutoCloseable {

    /**
     * Appends given string to the file of this appender
     */
    public synchronized void append(String string) throws IOException {
        bufferedWriter.append(string);
    }

    /**
     * Appends given string and a new line symbol to the file of this appender
     */
    public void appendLine(String string) throws IOException {
        append(string + System.lineSeparator());
    }

    @Override
    public void close() throws Exception {
        bufferedWriter.close();
    }

    // CONSTRUCTORS

    public static Optional<FileAppender> ofFile(String filename) {
        Path path = Paths.get(filename);
        if (!Files.isWritable(path))
            return Optional.empty();

        BufferedWriter bufferedWriter;
        try {
            bufferedWriter = Files.newBufferedWriter(path, UTF_8, CREATE_NEW);
        } catch (IOException e) {
            return Optional.empty();
        }
        return Optional.of(new FileAppender(bufferedWriter));
    }

    private FileAppender(BufferedWriter bufferedWriter) {
        this.bufferedWriter = bufferedWriter;
    }

    // PRIVATE
{'fraction_non_alphanumeric': 0.088875809299588, 'fraction_numerical': 0.0011771630370806356, 'mean_word_length': 2.9249422632794455, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '126180', 'n_tokens_mistral': 940, 'n_tokens_neox': 899, 'n_words': 264}