package boa.datagen.forges.github;

import java.io.File;
import java.util.ArrayList;
import java.util.Random;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import boa.datagen.util.FileIO;

public class ListSplitter {
	public static void main(String[] args) {
		final int NUM_OF_LISTS = 22;
		ArrayList<String> names = new ArrayList<String>();
		Random rand = new Random();
		String in = args[0];
		String out = args[1];
		int shareSize;
		File inDir = new File(in);
		File[] files = inDir.listFiles();
		System.out.println(files.length + " files");
		Gson parser = new Gson();
		JsonArray repos;
		JsonObject repo;
		for (int i = 0; i < files.length; i++) {
			System.out.println("proccessing page " + files[i].getName());
			String content = FileIO.readFileContents(files[i]);
			repos = parser.fromJson(content, JsonElement.class).getAsJsonArray();
			for (JsonElement repoE : repos) {
				repo = repoE.getAsJsonObject();
				names.add(repo.get("full_name").getAsString());
			}
		}
		System.out.println(names.size() + " names");
		shareSize = (names.size())/NUM_OF_LISTS;
		System.out.println("share size = " + shareSize);
		for (int i = 0; i < NUM_OF_LISTS; i++){
			System.out.println("writting list " + i);
			for (int j = 0; j < shareSize; j++){
				int k = rand.nextInt(names.size());
				String name = names.get(k);
				FileIO.writeFileContents( new File(out + "/list-" + i) , name + "\n", true);
				System.out.println(names.remove(name));
				System.out.println(name + " removed " + !names.contains(name));
			}
		}
		System.out.println("names remaning " + names.size());
		while (!names.isEmpty()){
			System.out.println("writting list " + 22);
			FileIO.writeFileContents( new File(out + "/list-" + 22) , names.get(0) + "\n", true);
			names.remove(0);
		}
	}
}

@@ -270,16 +270,52 @@
 .FileIO;
+%0Aimport gnu.trove.set.hash.THashSet;
 %0A%0Apublic
@@ -384,14 +384,8 @@
 %7B%0A%09%09
-final 
 int 
@@ -399,18 +399,19 @@
 LISTS = 
-22
+125
 ;%0A%09%09Arra
@@ -740,25 +740,25 @@
 or (int i = 
-0
+1
 ; i %3C files.
@@ -1046,18 +1046,22 @@
 %09%09%09%09
-names.add(
+String name = 
 repo
@@ -1091,16 +1091,36 @@
 String()
+;%0A%09%09%09%09names.add(name
 );%0A%09%09%09%7D%0A
@@ -1298,16 +1298,19 @@
 OF_LISTS
+ -1
 ; i++)%7B%0A
@@ -1454,24 +1454,25 @@
 ame = names.
+ 
 get(k);%0A%09%09%09%09
@@ -1488,33 +1488,32 @@
 iteFileContents(
- 
 new File(out + %22
@@ -1523,16 +1523,25 @@
 st-%22 + i
+ + %22.txt%22
 ) , name
@@ -1564,113 +1564,22 @@
 %09%09%09%09
-System.out.println(names.remove(name));%0A%09%09%09%09System.out.println(name + %22 removed %22 + !names.contains(name)
+names.remove(k
 );%0A%09
@@ -1711,18 +1711,33 @@
 ist %22 + 
-22
+(NUM_OF_LISTS -1)
 );%0A%09%09%09Fi
@@ -1790,10 +1790,26 @@
 %22 + 
-22
+(NUM_OF_LISTS - 1)
 ) , 

clean up