Merge branch 'develop' into features/JAL-2393customMatrices
[jalview.git] / src / jalview / util / SetUtils.java
1 package jalview.util;
2
3 import java.util.Set;
4
5 public class SetUtils
6 {
7   /**
8    * Returns the count of things that are in one or other of two sets but not in
9    * both. The sets are not modified.
10    * 
11    * @param set1
12    * @param set2
13    * @return
14    */
15   public static int countDisjunction(Set<? extends Object> set1,
16           Set<? extends Object> set2)
17   {
18     if (set1 == null)
19     {
20       return set2 == null ? 0 : set2.size();
21     }
22     if (set2 == null)
23     {
24       return set1.size();
25     }
26
27     int size1 = set1.size();
28     int size2 = set2.size();
29     Set<? extends Object> smallerSet = size1 < size2 ? set1 : set2;
30     Set<? extends Object> largerSet = (smallerSet == set1 ? set2 : set1);
31     int inCommon = 0;
32     for (Object k : smallerSet)
33     {
34       if (largerSet.contains(k))
35       {
36         inCommon++;
37       }
38     }
39
40     int notInCommon = (size1 - inCommon) + (size2 - inCommon);
41     return notInCommon;
42   }
43 }