X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Futil%2FSetUtils.java;fp=src%2Fjalview%2Futil%2FSetUtils.java;h=06b627dac9b46e79ead2ef102519a926d4c20de3;hb=f063821ed0be9c1581af74643a1aa5798731af65;hp=0000000000000000000000000000000000000000;hpb=fd18e2c73cd015d4e38ad91da0e5d7532ff0ef42;p=jalview.git diff --git a/src/jalview/util/SetUtils.java b/src/jalview/util/SetUtils.java new file mode 100644 index 0000000..06b627d --- /dev/null +++ b/src/jalview/util/SetUtils.java @@ -0,0 +1,63 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ +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; + } +}