The Java Program: WordSort.java

  1 import java.text.Collator;
  2 import java.util.*;
  3 import java.io.*;
  4 
  5 public class WordSort {
  6 
  7    static final Integer decomp = Integer.getInteger ("decomp");
  8    static final Integer streng = Integer.getInteger ("streng");
  9 
 10    public static void main (String[] args) throws IOException {
 11       final BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
 12 
 13       
 14       final TreeSet list;
 15 
 16       if (args.length<1) {
 17          list = new TreeSet ();   // comparator is default String compartor: lexicographic on code points.
 18       } else if (args.length<2) {
 19          if (args[0].equalsIgnoreCase ("latin1")) {
 20             list = new TreeSet (Collator.getInstance());
 21          } else {
 22             list = new TreeSet (Collator.getInstance(new Locale (args[0], "")));
 23          }
 24       } else if (args.length<3) {
 25          list = new TreeSet (Collator.getInstance(new Locale (args[0],args[1])));
 26       } else {
 27          list = new TreeSet (Collator.getInstance(new Locale (args[0],args[1],args[2])));
 28       }
 29 
 30       final Object o = list.comparator();
 31       if (o == null) {
 32          System.out.println ("Sorted by default String comparator.");
 33       } else if (o instanceof Collator) {
 34          final Collator c = (Collator) o;
 35          // Does this affect the collator already in the TreeSet?
 36          if (streng!=null) c.setStrength (streng.intValue());
 37          if (decomp!=null) c.setDecomposition (decomp.intValue());
 38          System.out.println ("Sorted by: " + c);
 39          System.out.println ("  strength " + c.getStrength());
 40          System.out.println ("  decomposition " + c.getDecomposition());
 41       } else {
 42       }
 43       for (;;) {
 44          final String line = br.readLine();
 45          if (line==null) break;
 46          list.add (line);
 47       }
 48 
 49       for (Iterator i=list.iterator();i.hasNext();) {
 50          System.out.println (i.next());
 51       }
 52 
 53    }
 54 
 55 }