code: public static MetricValue merge(MetricKind kind, MetricValue prior, MetricValue latest) {
      if (prior.getValueCase() != latest.getValueCase()) {
        log.atWarning().log("Could not merge different types of metric: %s, %s", prior, latest);
        throw new IllegalArgumentException(MSG_CANNOT_MERGE_DIFFERENT_TYPES);
      }
      if (kind == MetricKind.DELTA) {
        Builder builder = latest.toBuilder();
        mergeTimestamps(builder, prior, latest);
        mergeValues(builder, prior, latest);
        return builder.build();
      } else if (Timestamps.COMPARATOR.compare(prior.getEndTime(), latest.getEndTime()) < 0) {
        return latest;
      } else {
        return prior;
      }
    }
docs: Merge {@code prior} with {@code latest}.
  
  If {@code kind} is {@code MetricKind.DELTA} then the result contains a combination of values in
  prior and latest. For all other kinds, it's sufficient to return the metric with the latest end
  time.
  
  @param kind the {@code MetricKind}
  @param prior a {@code MetricValue} instance
  @param latest a {@code MetricValue}, expected to be a later version of {@code prior}
  
  @return a new {@code MetricValue} that combines prior and latest depending on {@code kind}
  @throws IllegalArgumentException if the {@code prior} and {@code latest} are have different
  types of value, or if the type is not mergeable