package org.lilyproject.testclientfw;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Words {
    public enum WordList {
        SMALL_LIST("wordlist-10.txt", 5000), BIG_LIST("wordlist-50.txt", 100000);

        private String file;

        private int estimatedSize;

        WordList(String file, int estimatedSize) {
            this.file = file;
            this.estimatedSize = estimatedSize;
        }
    };

    private static Map<WordList, List<String>> WORDS = new HashMap<WordList, List<String>>(100000);

    static {
        try {
            for (WordList list : WordList.values()) {
                List<String> result = new ArrayList<String>(list.estimatedSize);
                InputStream is = Words.class.getResourceAsStream(list.file);
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                String word;
                while ((word = reader.readLine()) != null) {
                    if (word.length() > 0) {
                        // The lowerCase is a simple trick to avoid that words like OR, AND, NOT cause
                        // the SOLR query parser to fail.
                        result.add(word.toLowerCase());
                    }
                }
                is.close();
                WORDS.put(list, result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static String get(WordList list) {
        return WORDS.get(list).get((int)(Math.floor(Math.random() * WORDS.size())));
    }

    public static String get() {
        return WORDS.get(WordList.BIG_LIST).get((int)(Math.floor(Math.random() * WORDS.size())));
    }

    /**
     * Returns a space-separated string containing the specified amount of words.
     */
    public static String get(WordList list, int amount) {
        StringBuffer buffer = new StringBuffer(20 * amount);
        for (int i = 0; i < amount; i++) {
            buffer.append(get());
        }
        return buffer.toString();
    }
}

@@ -1622,30 +1622,66 @@
     
-return WORDS.get(list)
+List%3CString%3E words = WORDS.get(list);%0A        return words
 .get
@@ -1706,37 +1706,37 @@
 Math.random() * 
-WORDS
+words
 .size())));%0A    
@@ -1779,22 +1779,36 @@
         
-return
+List%3CString%3E words =
  WORDS.g
@@ -1828,16 +1828,38 @@
 IG_LIST)
+;%0A        return words
 .get((in
@@ -1888,21 +1888,21 @@
 dom() * 
-WORDS
+words
 .size())
@@ -2168,24 +2168,83 @@
 unt; i++) %7B%0A
+            if (i %3E 0)%0A                buffer.append(' ');%0A
             
@@ -2261,16 +2261,20 @@
 end(get(
+list
 ));%0A    

fixing some obvious errors.