package es.predictia.util;

import static com.google.common.io.Files.copy;
import static es.predictia.util.Streams.writeLinesToFile;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class FileUpdater {

	private FileUpdater(){}
	
	public interface LinesProcessor{
		List<String> newLines(Reader fileReader) throws IOException; 
	}
	
	public static void updateFile(File destination, Charset charset, LinesProcessor linesProcessor) throws IOException{
		LOGGER.info("Updating file: " + destination);
		File tmpFile = File.createTempFile("temp-", ".properties");
		copy(destination, tmpFile);
		FileReader reader = new FileReader(tmpFile);
		try{
			writeLinesToFile(linesProcessor.newLines(reader), destination, charset);
		}finally{
			reader.close();
			tmpFile.delete();
		}
	}
	
	private static final transient Logger LOGGER = LoggerFactory.getLogger(FileUpdater.class);
	
}

@@ -609,16 +609,45 @@
 eption%7B%0A
+%09%09if(destination.exists())%7B%0A%09
 %09%09LOGGER
@@ -686,16 +686,115 @@
 ation);%0A
+%09%09%7Delse%7B%0A%09%09%09destination.createNewFile();%0A%09%09%09LOGGER.info(%22Writing to file: %22 + destination);%0A%09%09%7D%0A%09%09%0A
 %09%09File t

update works now on not existing files