Merge branch 'develop' of https://source.jalview.org/git/jalview.git into develop
[jalview.git] / src / jalview / util / SetUtils.java
diff --git a/src/jalview/util/SetUtils.java b/src/jalview/util/SetUtils.java
new file mode 100644 (file)
index 0000000..381d9f6
--- /dev/null
@@ -0,0 +1,43 @@
+package jalview.util;
+
+import java.util.Set;
+
+public class SetUtils
+{
+  /**
+   * Returns the count of things that are in one or other of two sets but not in
+   * both. The sets are not modified.
+   * 
+   * @param set1
+   * @param set2
+   * @return
+   */
+  public static int countDisjunction(Set<? extends Object> set1,
+          Set<? extends Object> set2)
+  {
+    if (set1 == null)
+    {
+      return set2 == null ? 0 : set2.size();
+    }
+    if (set2 == null)
+    {
+      return set1.size();
+    }
+
+    int size1 = set1.size();
+    int size2 = set2.size();
+    Set<? extends Object> smallerSet = size1 < size2 ? set1 : set2;
+    Set<? extends Object> largerSet = (smallerSet == set1 ? set2 : set1);
+    int inCommon = 0;
+    for (Object k : smallerSet)
+    {
+      if (largerSet.contains(k))
+      {
+        inCommon++;
+      }
+    }
+
+    int notInCommon = (size1 - inCommon) + (size2 - inCommon);
+    return notInCommon;
+  }
+}