import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.Comparator;
import java.util.Scanner;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

/**
 * This Example illustrates the HashSet and TreeSet classes
 * @author Bill Kraynek
 */
public class HashAndTreeSetExample {
    
    public HashAndTreeSetExample() {
        try{
            Scanner in = new Scanner(new File("src/HashAndTreeSetExample.java"));
            in.useDelimiter("[^a-zA-Z]+");
            String file = "";
            while( in.hasNext() ) {
                file += in.next() + " ";
            } // end while
            Set<String> words;
            words  = new HashSet<String>();
            getWords(words,file);
            displaySet(words,"Hash Set");
            words = new TreeSet<String>();
            getWords(words,file);
            displaySet(words,"Tree Set using the Natural Comparator");
            words = new TreeSet<String>(new IgnoreCaseComparator());
            getWords(words,file);
            displaySet(words,"Tree Set using the IgnoreCase Comparator");
            words = new TreeSet<String>(new CompareByLength());
            getWords(words,file);
            displaySet(words,"Tree Set using the CompareBylength Comparator");
        } catch(Exception e) {
            System.out.println(e);
        }
    }
    /**
     * Parse the String file for 'words' and store unique words in the List words
     * in sorted order
     * @param file is the String
     * @ @return the List of sorted words
     */
    public void getWords(Set<String> words,String file) {
        Scanner parser = new Scanner(file);
        while( parser.hasNext() ) {
            words.add(parser.next());
        }
    } // end getWords
    
    /**
     * Display a Set in a JTextArea
     * @param aSet is the Set to display
     */
    public void displaySet(Set aSet, String title) {
        String out = "   " + title + "\n\n";
        for( Object x : aSet ) out += x + "\n";
        JTextArea outArea = new JTextArea(40,40);
        outArea.setText(out);
        JOptionPane.showMessageDialog(null,new JScrollPane(outArea));
    } // displaySet
    
    class IgnoreCaseComparator implements Comparator<String> {
        public int compare(String a, String b) {
            return a.compareToIgnoreCase(b);
        } // end compare
    } // end IgnoreCase
    
    class CompareByLength implements Comparator<String> {
        public int compare( String left, String right ){
            int difference = right.length() - left.length();
            if( difference != 0 ) return difference;
            return left.compareTo(right);
        } // end compare
    } // end CompareByLength
    
    
    public static void main(String args[]) throws IOException {
        new HashAndTreeSetExample();
        System.exit(0);
    } // end main
    
} // end HashAndTreeSetExample


