/* * StringWordOrganizer.java * * Created on October 15, 2005, 1:37 PM */ package Current.search; import java.lang.String; import java.lang.StringBuffer; import java.util.Comparator; import java.util.TreeSet; /** * This class holds a set of string and orders them canonically. * In particular, it will not hold duplicates. *

* This class is essentially a wrapper for a TreeSet for handling strings. * I have used naming conventions from the SortedSet class. *

* It also comes with some utility function for organizing Strings canonically. * * @see java.lang.String * @see java.lang.TreeSet * @see java.lang.SortedSet * @author pat */ public class StringWordOrganizer implements Comparator { TreeSet ts; /** Creates a new instance of StringWordOrganizer */ public StringWordOrganizer() { ts=new TreeSet(this); } /** Compare two strings first by length and second lexigraphically. * @return -1 if the first is less than the second, 0 if they are equal * and 1 if the first is greater **/ public int compare(Object obj, Object obj1) { if ( ((String)obj).length()<((String)obj1).length()) return -1; if ( ((String)obj).length()>((String)obj1).length()) return 1; return ((String)obj).compareTo((String)obj1); } /** Returns a canonical version of the string. Returns null if the * string is a power of an even word. */ public static String canonicalize(String str) { int l=str.length(); StringBuffer ret=new StringBuffer(l); int i,j; /* find the least lexigraphical forward ordered word */ int best=0; for (i=1; i0)&&(str.charAt((best2+j)%l)==str.charAt((i+j)%l)); j--) {} if ( (j!=l) && (str.charAt((i+j)%l)