1 package org.unicode.cldr.util;
2 
3 import java.util.Comparator;
4 import java.util.Iterator;
5 import java.util.Set;
6 
7 public class SetComparator<T extends Comparable<T>> implements Comparator<Set<T>> {
compare(Set<T> o1, Set<T> o2)8     public int compare(Set<T> o1, Set<T> o2) {
9         int size1 = o1.size();
10         int size2 = o2.size();
11         int diff = size1 - size2;
12         if (diff != 0) {
13             return diff;
14         }
15         Iterator<T> i1 = o1.iterator();
16         Iterator<T> i2 = o2.iterator();
17         while (i1.hasNext() && i2.hasNext()) {
18             T item1 = i1.next();
19             T item2 = i2.next();
20             diff = item1.compareTo(item2);
21             if (diff != 0) {
22                 return diff;
23             }
24         }
25         // we know that they are the same length at this point, so if we
26         // make it through the gauntlet, we're done
27         return 0;
28     }
29 }