/*
 * Copyright 2020 Johan Walles <johan.walles@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gmail.walles.johan.exactype.stats;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import timber.log.Timber;

class StatsTracker {
    private final File backingFile;

    public StatsTracker(File backingFile) {
        this.backingFile = backingFile;
    }

    public void countCharacter(char character) {
        Map<Character, Integer> counts;
        try {
            counts = readCountsFromFile(backingFile);
        } catch (IOException e) {
            Timber.w(e,
                "Failed reading stats from file: %s",
                backingFile.getAbsolutePath());
            return;
        }

        // Bump the count
        Integer count = counts.get(character);
        if (count == null) {
            count = 0;
        }
        counts.put(character, count + 1);

        writeCountsToFile(counts);
    }

    public static Map<Character, Integer> readCountsFromFile(File countsFile) throws IOException {
        // FIXME: Write code here
    }

    private void writeCountsToFile(Map<Character, Integer> counts) {
        // FIXME: Write code here
    }
}

/*
 * Copyright 2020 Johan Walles <johan.walles@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gmail.walles.johan.exactype.stats;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import timber.log.Timber;

class StatsTracker {
    private final File backingFile;

    public StatsTracker(File backingFile) {
        this.backingFile = backingFile;
    }

    public void countCharacter(char character) {
        Map<Character, Integer> counts;
        try {
            counts = readCountsFromFile(backingFile);
        } catch (IOException e) {
            Timber.w(e,
                "Failed reading stats from file: %s",
                backingFile.getAbsolutePath());
            return;
        }

        // Bump the count
        Integer count = counts.get(character);
        if (count == null) {
            count = 0;
        }
        counts.put(character, count + 1);

        try {
            writeCountsToFile(counts);
        } catch (IOException e) {
            Timber.w(e,
                "Failed writing stats to file: %s",
                backingFile.getAbsolutePath());
        }
    }

    public static Map<Character, Integer> readCountsFromFile(File countsFile) throws IOException {
        // FIXME: Write code here
    }

    private void writeCountsToFile(Map<Character, Integer> counts) throws IOException {
        File tempFile = new File(backingFile.getAbsolutePath(), ".tmp");
        try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
             BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
             PrintWriter out = new PrintWriter(bufferedOutputStream)) {
            for (Map.Entry<Character, Integer> entry: counts.entrySet()) {
                out.print(entry.getKey());
                out.print(": ");
                out.println(entry.getValue());
            }
        }

        if (!tempFile.renameTo(backingFile)) {
            throw new IOException(
                "Rename failed: " + tempFile.getAbsolutePath() + "->" + backingFile.getAbsolutePath());
        }
    }
}

Implement writing stats to file