View Javadoc
1   package org.kuali.student.ap.coursesearch.controller;
2   
3   import java.util.*;
4   
5   public class TokenPairs
6   {
7       public static List<String> toPairs(List<String> list)
8       {
9           ArrayList<String> pairs = new ArrayList<String>();
10          int a = 0;
11          int b = 0;
12          int size = list.size();
13          while( a < size )
14          {
15              String result = null;
16              if( a == b )
17              {
18                  result = list.get( a );
19                  b++;
20              }
21              else if( a != b )
22              {
23                  result = list.get( a ) + " " + list.get( b );
24                  a++;
25              }
26  
27              if( b == size )
28              {
29                  a = b;
30              }
31  
32              pairs.add( result );
33          }
34  
35          return pairs;
36      }
37  
38      public static class LongestFirst implements Comparator<String>
39      {
40         // First sort by length, then by String's own comparison (ie alpha)
41         public int compare( String left, String right )
42         {
43            int diff = right.length() - left.length();
44            if( diff == 0 )
45            {
46                diff = left.compareTo( right );
47            }
48            return diff;
49         }
50      }
51  
52      public static void sortedLongestFirst( List<String> list )
53      {
54          Collections.sort(list, new LongestFirst());
55      }
56  }