Heavily refactor ProfileCommand to separate output and statistics generation and enable their reuse

--
MOS_MIGRATED_REVID=103634406
diff --git a/src/main/java/com/google/devtools/build/lib/profiler/ProfilerTask.java b/src/main/java/com/google/devtools/build/lib/profiler/ProfilerTask.java
index 37349b1..00e2557 100644
--- a/src/main/java/com/google/devtools/build/lib/profiler/ProfilerTask.java
+++ b/src/main/java/com/google/devtools/build/lib/profiler/ProfilerTask.java
@@ -13,6 +13,10 @@
 // limitations under the License.
 package com.google.devtools.build.lib.profiler;
 
+import com.google.common.base.Predicate;
+
+import java.util.EnumSet;
+
 /**
  * All possible types of profiler tasks. Each type also defines description and
  * minimum duration in nanoseconds for it to be recorded as separate event and
@@ -115,4 +119,17 @@
   public boolean collectsSlowestInstances() {
     return slowestInstancesCount > 0;
   }
+
+  /**
+   * Build a set containing all ProfilerTasks for which the given predicate is true.
+   */
+  public static EnumSet<ProfilerTask> allSatisfying(Predicate<ProfilerTask> predicate) {
+    EnumSet<ProfilerTask> set = EnumSet.noneOf(ProfilerTask.class);
+    for (ProfilerTask taskType : values()) {
+      if (predicate.apply(taskType)) {
+        set.add(taskType);
+      }
+    }
+    return set;
+  }
 }