Refactor the Event class; always construct through static methods.

--
MOS_MIGRATED_REVID=120418505
diff --git a/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java b/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java
index c7aaa32..968c721 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java
@@ -25,7 +25,6 @@
 import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
 import com.google.devtools.build.lib.events.Event;
 import com.google.devtools.build.lib.events.EventHandler;
-import com.google.devtools.build.lib.events.EventKind;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
 import com.google.devtools.build.lib.syntax.Printer;
 import com.google.devtools.build.lib.util.Preconditions;
@@ -374,10 +373,12 @@
       Path path = output.getPath();
       String ownerString = Label.print(getOwner().getLabel());
       if (path.isDirectory()) {
-        eventHandler.handle(new Event(EventKind.WARNING, getOwner().getLocation(),
-            "output '" + output.prettyPrint() + "' of " + ownerString
-                  + " is a directory; dependency checking of directories is unsound",
-                  ownerString));
+        eventHandler.handle(
+            Event.warn(
+                getOwner().getLocation(),
+                "output '" + output.prettyPrint() + "' of " + ownerString
+                    + " is a directory; dependency checking of directories is unsound")
+                .withTag(ownerString));
       }
     }
   }
diff --git a/src/main/java/com/google/devtools/build/lib/actions/ActionCacheChecker.java b/src/main/java/com/google/devtools/build/lib/actions/ActionCacheChecker.java
index 72d5756..4fd06a9 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/ActionCacheChecker.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/ActionCacheChecker.java
@@ -327,7 +327,7 @@
   private static void reportRebuild(@Nullable EventHandler handler, Action action, String message) {
     // For MiddlemanAction, do not report rebuild.
     if (handler != null && !action.getActionType().isMiddleman()) {
-      handler.handle(new Event(
+      handler.handle(Event.of(
           EventKind.DEPCHECKER, null, "Executing " + action.prettyPrint() + ": " + message + "."));
     }
   }
diff --git a/src/main/java/com/google/devtools/build/lib/actions/BlazeExecutor.java b/src/main/java/com/google/devtools/build/lib/actions/BlazeExecutor.java
index 88dc9a3..2513cec 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/BlazeExecutor.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/BlazeExecutor.java
@@ -146,7 +146,7 @@
    */
   @Override
   public void reportSubcommand(String reason, String message) {
-    reporter.handle(new Event(EventKind.SUBCOMMAND, null, "# " + reason + "\n" + message));
+    reporter.handle(Event.of(EventKind.SUBCOMMAND, null, "# " + reason + "\n" + message));
   }
 
   /**
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/Runfiles.java b/src/main/java/com/google/devtools/build/lib/analysis/Runfiles.java
index 425a3ee..e9de51b 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/Runfiles.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/Runfiles.java
@@ -746,7 +746,7 @@
                   path.getSafePathString(),
                   previousStr,
                   artifactStr);
-          eventHandler.handle(new Event(eventKind, location, message));
+          eventHandler.handle(Event.of(eventKind, location, message));
         }
       }
     }
diff --git a/src/main/java/com/google/devtools/build/lib/events/Event.java b/src/main/java/com/google/devtools/build/lib/events/Event.java
index 2e35b2e..1a74ad2 100644
--- a/src/main/java/com/google/devtools/build/lib/events/Event.java
+++ b/src/main/java/com/google/devtools/build/lib/events/Event.java
@@ -47,39 +47,31 @@
   @Nullable
   private final String tag;
 
-  public Event withTag(String tag) {
-    if (this.message != null) {
-      return new Event(this.kind, this.location, this.message, tag);
-    } else {
-      return new Event(this.kind, this.location, this.messageBytes, tag);
-    }
-  }
-
-  public Event(EventKind kind, @Nullable Location location, String message) {
-    this(kind, location, message, null);
-  }
-
-  public Event(EventKind kind, @Nullable Location location, String message, @Nullable String tag) {
-    this.kind = kind;
+  private Event(EventKind kind, @Nullable Location location, String message, @Nullable String tag) {
+    this.kind = Preconditions.checkNotNull(kind);
     this.location = location;
     this.message = Preconditions.checkNotNull(message);
     this.messageBytes = null;
     this.tag = tag;
   }
 
-  public Event(EventKind kind, @Nullable Location location, byte[] messageBytes) {
-    this(kind, location, messageBytes, null);
-  }
-
-  public Event(
+  private Event(
       EventKind kind, @Nullable Location location, byte[] messageBytes, @Nullable String tag) {
-    this.kind = kind;
+    this.kind = Preconditions.checkNotNull(kind);
     this.location = location;
     this.message = null;
     this.messageBytes = Preconditions.checkNotNull(messageBytes);
     this.tag = tag;
   }
 
+  public Event withTag(String tag) {
+    if (this.message != null) {
+      return new Event(this.kind, this.location, this.message, tag);
+    } else {
+      return new Event(this.kind, this.location, this.messageBytes, tag);
+    }
+  }
+
   public String getMessage() {
     return message != null ? message : new String(messageBytes);
   }
@@ -146,32 +138,40 @@
     }
   }
 
+  public static Event of(EventKind kind, @Nullable Location location, String message) {
+    return new Event(kind, location, message, null);
+  }
+
+  public static Event of(EventKind kind, @Nullable Location location, byte[] messageBytes) {
+    return new Event(kind, location, messageBytes, null);
+  }
+
   /**
    * Reports a warning.
    */
-  public static Event warn(Location location, String message) {
-    return new Event(EventKind.WARNING, location, message);
+  public static Event warn(@Nullable Location location, String message) {
+    return new Event(EventKind.WARNING, location, message, null);
   }
 
   /**
    * Reports an error.
    */
-  public static Event error(Location location, String message){
-    return new Event(EventKind.ERROR, location, message);
+  public static Event error(@Nullable Location location, String message){
+    return new Event(EventKind.ERROR, location, message, null);
   }
 
   /**
    * Reports atemporal statements about the build, i.e. they're true for the duration of execution.
    */
-  public static Event info(Location location, String message) {
-    return new Event(EventKind.INFO, location, message);
+  public static Event info(@Nullable Location location, String message) {
+    return new Event(EventKind.INFO, location, message, null);
   }
 
   /**
    * Reports a temporal statement about the build.
    */
-  public static Event progress(Location location, String message) {
-    return new Event(EventKind.PROGRESS, location, message);
+  public static Event progress(@Nullable Location location, String message) {
+    return new Event(EventKind.PROGRESS, location, message, null);
   }
 
   /**
diff --git a/src/main/java/com/google/devtools/build/lib/events/Reporter.java b/src/main/java/com/google/devtools/build/lib/events/Reporter.java
index a14e827..5ecb58b 100644
--- a/src/main/java/com/google/devtools/build/lib/events/Reporter.java
+++ b/src/main/java/com/google/devtools/build/lib/events/Reporter.java
@@ -119,7 +119,7 @@
    * progress indicator (if any) in the message may differ.
    */
   public void startTask(Location location, String message) {
-    handle(new Event(EventKind.START, location, message));
+    handle(Event.of(EventKind.START, location, message));
   }
 
   /**
@@ -130,12 +130,12 @@
    * progress indicator (if any) in the message may differ.
    */
   public void finishTask(Location location, String message) {
-    handle(new Event(EventKind.FINISH, location, message));
+    handle(Event.of(EventKind.FINISH, location, message));
   }
 
   @Override
   public void error(Location location, String message, Throwable error) {
-    handle(new Event(EventKind.ERROR, location, message));
+    handle(Event.error(location, message));
     error.printStackTrace(new PrintStream(getOutErr().getErrorStream()));
   }
 
@@ -178,5 +178,4 @@
       ansiAllowingHandlerRegistered = false;
     }
   }
-
 }
diff --git a/src/main/java/com/google/devtools/build/lib/events/ReporterStream.java b/src/main/java/com/google/devtools/build/lib/events/ReporterStream.java
index 3cd1d9f..e72f4a9 100644
--- a/src/main/java/com/google/devtools/build/lib/events/ReporterStream.java
+++ b/src/main/java/com/google/devtools/build/lib/events/ReporterStream.java
@@ -16,19 +16,20 @@
 
 import static java.nio.charset.StandardCharsets.ISO_8859_1;
 
+import com.google.devtools.build.lib.util.Preconditions;
+
 import java.io.OutputStream;
 
 /**
  * An OutputStream that delegates all writes to a Reporter.
  */
 public final class ReporterStream extends OutputStream {
-
   private final EventHandler reporter;
   private final EventKind eventKind;
 
   public ReporterStream(EventHandler reporter, EventKind eventKind) {
-    this.reporter = reporter;
-    this.eventKind = eventKind;
+    this.reporter = Preconditions.checkNotNull(reporter);
+    this.eventKind = Preconditions.checkNotNull(eventKind);
   }
 
   @Override
@@ -43,16 +44,16 @@
 
   @Override
   public void write(int b) {
-    reporter.handle(new Event(eventKind, null, new byte[] { (byte) b }));
+    reporter.handle(Event.of(eventKind, null, new byte[] { (byte) b }));
   }
 
   @Override
   public void write(byte[] bytes) {
-    reporter.handle(new Event(eventKind, null, bytes));
+    reporter.handle(Event.of(eventKind, null, bytes));
   }
 
   @Override
   public void write(byte[] bytes, int offset, int len) {
-    reporter.handle(new Event(eventKind, null, new String(bytes, offset, len, ISO_8859_1)));
+    reporter.handle(Event.of(eventKind, null, new String(bytes, offset, len, ISO_8859_1)));
   }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java
index 5323d09..97bd13f 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java
@@ -46,7 +46,6 @@
 import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible;
 import com.google.devtools.build.lib.events.Event;
 import com.google.devtools.build.lib.events.EventHandler;
-import com.google.devtools.build.lib.events.EventKind;
 import com.google.devtools.build.lib.profiler.Profiler;
 import com.google.devtools.build.lib.profiler.ProfilerTask;
 import com.google.devtools.build.lib.rules.apple.AppleConfiguration;
@@ -802,9 +801,10 @@
 
     if (warnings.hasProblems()) {
       eventHandler.handle(
-          new Event(EventKind.WARNING,
-              getOwner().getLocation(), warnings.getMessage(this, getSourceFile()),
-          Label.print(getOwner().getLabel())));
+          Event.warn(
+              getOwner().getLocation(),
+              warnings.getMessage(this, getSourceFile()))
+              .withTag(Label.print(getOwner().getLabel())));
     }
     errors.assertProblemFree(this, getSourceFile());
   }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/test/StandaloneTestStrategy.java b/src/main/java/com/google/devtools/build/lib/rules/test/StandaloneTestStrategy.java
index a12084a..72ff4e8 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/test/StandaloneTestStrategy.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/test/StandaloneTestStrategy.java
@@ -236,15 +236,15 @@
       }
     } finally {
       if (isPassed) {
-        executor.getEventHandler().handle(new Event(EventKind.PASS, null, result.getTestName()));
+        executor.getEventHandler().handle(Event.of(EventKind.PASS, null, result.getTestName()));
       } else {
         if (result.getData().getStatus() == BlazeTestStatus.TIMEOUT) {
           executor.getEventHandler().handle(
-              new Event(EventKind.TIMEOUT, null, result.getTestName()
+              Event.of(EventKind.TIMEOUT, null, result.getTestName()
                   + " (see " + testOutput + ")"));
         } else {
           executor.getEventHandler().handle(
-              new Event(EventKind.FAIL, null, result.getTestName() + " (see " + testOutput + ")"));
+              Event.of(EventKind.FAIL, null, result.getTestName() + " (see " + testOutput + ")"));
         }
       }
     }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/RecursiveFilesystemTraversalFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/RecursiveFilesystemTraversalFunction.java
index ec0b37a..b7f7b92 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/RecursiveFilesystemTraversalFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/RecursiveFilesystemTraversalFunction.java
@@ -19,7 +19,6 @@
 import com.google.devtools.build.lib.collect.nestedset.NestedSet;
 import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
 import com.google.devtools.build.lib.events.Event;
-import com.google.devtools.build.lib.events.EventKind;
 import com.google.devtools.build.lib.skyframe.RecursiveFilesystemTraversalValue.ResolvedFile;
 import com.google.devtools.build.lib.skyframe.RecursiveFilesystemTraversalValue.ResolvedFileFactory;
 import com.google.devtools.build.lib.skyframe.RecursiveFilesystemTraversalValue.TraversalRequest;
@@ -152,7 +151,7 @@
         switch (traversal.crossPkgBoundaries) {
           case CROSS:
             // We are free to traverse the subpackage but we need to display a warning.
-            env.getListener().handle(new Event(EventKind.WARNING, null, msg));
+            env.getListener().handle(Event.warn(null, msg));
             break;
           case DONT_CROSS:
             // We cannot traverse the subpackage and should skip it silently. Return empty results.