Simplify the logic of determining output groups.

--
MOS_MIGRATED_REVID=125368119
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/OutputGroupProvider.java b/src/main/java/com/google/devtools/build/lib/analysis/OutputGroupProvider.java
index dec4d40..e600550 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/OutputGroupProvider.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/OutputGroupProvider.java
@@ -16,6 +16,8 @@
 
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSortedSet;
+import com.google.common.collect.Sets;
 import com.google.devtools.build.lib.actions.Artifact;
 import com.google.devtools.build.lib.collect.nestedset.NestedSet;
 import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
@@ -136,4 +138,41 @@
     }
     return new OutputGroupProvider(resultBuilder.build());
   }
+
+  public static ImmutableSortedSet<String> determineOutputGroups(List<String> outputGroups) {
+    return determineOutputGroups(DEFAULT_GROUPS, outputGroups);
+  }
+
+  public static ImmutableSortedSet<String> determineOutputGroups(
+      Set<String> defaultOutputGroups, List<String> outputGroups) {
+
+    Set<String> current = Sets.newHashSet();
+
+    // If all of the requested output groups start with "+" or "-", then these are added or
+    // subtracted to the set of default output groups.
+    // If any of them don't start with "+" or "-", then the list of requested output groups
+    // overrides the default set of output groups.
+    boolean addDefaultOutputGroups = true;
+    for (String outputGroup : outputGroups) {
+      if (!(outputGroup.startsWith("+") || outputGroup.startsWith("-"))) {
+        addDefaultOutputGroups = false;
+        break;
+      }
+    }
+    if (addDefaultOutputGroups) {
+      current.addAll(defaultOutputGroups);
+    }
+
+    for (String outputGroup : outputGroups) {
+      if (outputGroup.startsWith("+")) {
+        current.add(outputGroup.substring(1));
+      } else if (outputGroup.startsWith("-")) {
+        current.remove(outputGroup.substring(1));
+      } else {
+        current.add(outputGroup);
+      }
+    }
+
+    return ImmutableSortedSet.copyOf(current);
+  }
 }