/***************************************************************************
* Anastasios Monachos - anastasiosm@gmail.com
* 
* Compile: javac LineShuffler.java
*
* Usage: java LineShuffler inputfile 
*
* The program will accept as argument the name of file, which will load its contents 
* in a List and randomly will shuffle its data, saving the results in a new file called
* inputfile.shuffled.txt
***************************************************************************/
import java.io.*;
import java.lang.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

public class LineShuffler
{
	private static int TOTAL_LINES_IN_INPUT_FILE = 0;
	private static String outputFile = ".shuffled.txt";
	private static List<String> lines = new ArrayList<String>();
	
public static void main(String args[])
{
	// Check the arguments
    if ((args.length!=1))
	{
		help();
	}
	else
	{   
		new LineShuffler(args[0]);
		System.out.println("Shuffled data must have been saved into: "+args[0]+outputFile);
	}
}//end of main

public LineShuffler(String inputfile)
{
//first find how many lines the file has got
count(inputfile);

//Load all lines of file in ArrayList
	try
	{
        FileReader file = new FileReader(inputfile);
        BufferedReader buffer = new BufferedReader(file);
        
		String line = null;
		while ((line = buffer.readLine()) != null)
		{
			lines.add(line);
        }
        buffer.close();
	 }
	catch (Exception e)
	{
        System.out.println("Error: " + e.getMessage());
    }
//Now do shuffling
	Collections.shuffle(lines);
	//System.out.println("After shuffling, ArrayList contains : " + lines);//ok in terms of size		

//Now attempt to save shuffled data to output file
	try
	{
	    File f = new File(inputfile+outputFile);
        FileWriter fwriter = new FileWriter(f, true);//true, so to append
		BufferedWriter bwriter = new BufferedWriter(fwriter);
	
		for (int i=0; i<lines.size(); i++)
		{
			System.out.println(lines.get(i));
			bwriter.write(lines.get(i));
			bwriter.newLine();
		}
        bwriter.close();
    }
    catch (IOException e)
	{
         System.out.println(e); 
    }
}//end of constructor LineShuffler


private static void count(String fileName)
{
	BufferedReader in = null;
	try
	{
		FileReader fileReader = new FileReader(fileName);
		in = new BufferedReader(fileReader);
		count(fileName, in);
	}
	catch (IOException ioe)
	{
		ioe.printStackTrace();
	}
	finally
	{
		if (in != null)
		{
			try
			{
				in.close();
			}
			catch (IOException ioe)
			{
				ioe.printStackTrace();
			}
		}
	}
}//end of private static void count(String fileName)

private static void count(String name, BufferedReader in) throws IOException
{
		String line;
		do {
			line = in.readLine();
			if (line != null)
			{
				TOTAL_LINES_IN_INPUT_FILE++;
			}
		}
		while (line != null);
		System.out.println("File: " + name + "\t has got: " + TOTAL_LINES_IN_INPUT_FILE + " lines\t\n");
}//end of private static void count(String name, BufferedReader in) throws IOException

private static void help()
{
	System.out.println("\n\tUsage: java LineShuffler input_file_name\n\n\t"+
			"For example: java LineShuffler numbers\n"+
			"\t\t     will shuffle input_file's lines and it will generate\n"+
			"\t\t     a new file called input_file_name.shuffled.txt\n\n"+
			"\tNote, that the input_file should have less than 2,000,000,000 lines in total\n\n");
			System.exit(0);
}//end of private static void help()

}//end of public class LineShuffler