X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FSetUtils.java;fp=src%2Fjalview%2Futil%2FSetUtils.java;h=381d9f698a472dc21a30cd3d34edace72962da46;hb=4d890895e6aabfc36d45d3b53781ee2cf5cfafdc;hp=0000000000000000000000000000000000000000;hpb=c55981a5c90618c23b5bedd7ff419d9cae7eef96;p=jalview.git diff --git a/src/jalview/util/SetUtils.java b/src/jalview/util/SetUtils.java new file mode 100644 index 0000000..381d9f6 --- /dev/null +++ b/src/jalview/util/SetUtils.java @@ -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 set1, + Set 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 smallerSet = size1 < size2 ? set1 : set2; + Set 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; + } +}