/**
 * 
 */
package traffic;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.Map;

import com.google.common.collect.Maps;


/**
 * Persists collected time-series traffic data 
 * @author alan
 */
public class DataLogger {
	private Map<Integer, FileWriter> datafileWriters;
	private int dataFileCount;
	
	private SimpleDateFormat fileNameFormat = new SimpleDateFormat("MM-yyyy");
	
	private DataLogger(int fileCount) {
		this.dataFileCount = fileCount;
		this.datafileWriters = Maps.newHashMapWithExpectedSize(fileCount);
	}
	
	static DataLogger getLogger(int fileCount) {
		return new DataLogger(fileCount);
	}
	
	public boolean initialize() {
		boolean allInitialized = true;
		String filePrefix = getFilePrefix();
		for (int i = 0; i < dataFileCount; ++i) {
			try {
				File file = getFile(filePrefix, i);
				FileWriter fw = new FileWriter(file, true);
				datafileWriters.put(i, fw);
			} catch (IOException e) {
				allInitialized = false;
			}
		}
		return allInitialized;
	}
	
	public void logData(LinkedList<String> dataSet) throws IOException {
		int id = 0;
		for (String s : dataSet) {
			FileWriter fw = datafileWriters.get(id);
			fw.write(s);
			fw.write(System.lineSeparator());
			++id;
		}
		closeAll(datafileWriters);
	}
	
	private void closeAll(Map<Integer, FileWriter> writers) {
		for (FileWriter fw : writers.values()) {
			try {
				fw.close();
			} catch (IOException ioe) {
				// keep trying to close the other writers
			}
		}
	}
	
	private String getFilePrefix() {
		Date now = new Date();
		return fileNameFormat.format(now);
	}
	
	private File getFile(String prefix, int uniqueId) throws IOException {
		StringBuilder fileNameBuilder = new StringBuilder(prefix)
		.append("-").append(uniqueId).append(".csv");
		File f = new File(fileNameBuilder.toString());
		if (f.createNewFile()) {
			FileWriter headerWriter = new FileWriter(f);
			headerWriter.write(String.format("%s,%s", "time", "speed"));
			headerWriter.write(System.lineSeparator());
			headerWriter.close();
		}
		return f;
	}
}

@@ -99,16 +99,77 @@
 eption;%0A
+import java.nio.file.FileSystems;%0Aimport java.nio.file.Path;%0A
 import j
@@ -195,16 +195,16 @@
 Format;%0A
-
 import j
@@ -309,16 +309,51 @@
 t.Maps;%0A
+import com.google.common.io.Files;%0A
 %0A%0A/**%0A *
@@ -938,16 +938,36 @@
 efix();%0A
+%09%09File file = null;%0A
 %09%09for (i
@@ -1015,21 +1015,16 @@
 y %7B%0A%09%09%09%09
-File 
 file = g
@@ -1183,16 +1183,173 @@
  false;%0A
+%09%09%09%09String fname = (file != null) ? file.getAbsolutePath() : %22unknown path%22;%0A%09%09%09%09System.err.println(%22file creation error: %22 + fname + %22 %22 + e.getMessage());%0A
 %09%09%09%7D%0A%09%09%7D
@@ -2083,18 +2083,27 @@
 der(
+)%0A%09%09.append(
 prefix)
-%0A%09%09
 .app
@@ -2150,52 +2150,162 @@
 ;%0A%09%09
-File f = new File(fileNameBuilder.toString()
+final Path logFilePath = FileSystems.getDefault().getPath(%22logs%22, fileNameBuilder.toString());%0A%09%09File f = logFilePath.toFile();%0A%09%09Files.createParentDirs(f
 );%0A%09

add default 'logs' directory for storing ... log files